In my application, I am utilizing d3js charts as widgets. When a widget is added, a function is triggered to create and draw the widget (svg element) in a specific div.
The issue arises when the same widget is added twice, causing the d3js chart (svg element) to be appended to the previously created div instead of creating a new one. Additionally, I am encountering difficulties in resizing the charts when the div is resized.
Here is the AJAX response:
<div id="chart"></div>
<script>
var m = [30, 50, 10, 30],
w = 600 - m[1] - m[3],
h = 400 - m[0] - m[2];
var format = d3.format(",.0f");
var x = d3.scale.linear().range([0, w]),
y = d3.scale.ordinal().rangeRoundBands([0, h], .1);
var xAxis = d3.svg.axis().scale(x).orient("top").tickSize(-h),
yAxis = d3.svg.axis().scale(y).orient("left").tickSize(0);
var svg = d3.select("#chart").append("svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.attr("viewBox", "0 0 600 400")
.append("g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")")
// Parsing numbers and sorting by value.
data.forEach(function(d) { d.value = +d.value; });
data.sort(function(a, b) { return b.value - a.value; });
// Setting scale domain.
x.domain([0, d3.max(data, function(d) { return d.value; })]);
y.domain(data.map(function(d) { return d.name; }));
var bar = svg.selectAll("g.bar")
.data(data)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d) { return "translate(0," + y(d.name) + ")"; });
bar.append("rect")
.attr("width", function(d) { return x(d.value); })
.attr("height", y.rangeBand());
bar.append("text")
.attr("class", "value")
.attr("x", function(d) { return x(d.value); })
.attr("y", y.rangeBand() / 2)
.attr("dx", 50)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(function(d) { return format(d.value); });
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
</script>
This code snippet is being appended:
var i = counter;
$('<div class=\"box\" >'+ response +'</div>').appendTo("#data-"+i);
Below is a cropped image depicting the issue: