I am encountering an issue with my charts where all of them are positioned in the center of a div when drawn by the user, except for the donut charts. They seem to be placed outside of the top-left corner instead. Can anyone provide insights as to why this might be happening? I have created a JS Fiddle to demonstrate this.
Essentially, I have three functions at play here. The first one is a generic drawChart() function that determines which chart to draw based on the button clicked. Then there's chartTwo(), a simple two-line illustration of how that particular chart should be centered within the div. Lastly, there's chartOne(), responsible for drawing the problematic donut chart that is not behaving as expected.
Generic chart builder func
function drawChart(int){
var $chartarea = $('#chartarea'),
ca_w = $chartarea.innerWidth(),
ca_h = $chartarea.innerHeight();
if ($chartarea.find('svg').length > 0) {
$chartarea.find('svg').remove();
}
var margin = {top: 20, right: 20, bottom: 20, left: 20};
var width = ca_w - margin.left - margin.right,
height = ca_h - margin.top - margin.bottom;
var g = d3.select('#chartarea').append('svg')
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append('g')
.style('position', 'relative')
.style('left', '0')
.attr('height', height)
.attr('width', width)
.attr('transform', 'translate('+margin.left+', '+margin.top+')');
switch (int) {
case 0:
chartOne(g, width, height);
break;
case 1:
chartTwo(g, width, height);
break;
default:
chartOne(g, width, height);
}
}
Donut chart func
function chartOne(g, width, height) {
var data = [
{name: "USA", value: 40},
{name: "UK", value: 20},
{name: "Canada", value: 30},
{name: "Maxico", value: 10},
];
var text = "";
var thickness = 40;
var radius = Math.min(width, height) / 2;
var color = d3.scaleOrdinal(d3.schemeCategory10);
var arc = d3.arc()
.innerRadius(radius - thickness)
.outerRadius(radius);
var pie = d3.pie()
.value(function(d) { return d.value; })
.sort(null);
g.selectAll('path')
.data(pie(data))
.enter()
.append("g")
.on("mouseover", function(d) {
let g = d3.select(this)
.style("cursor", "pointer")
.style("fill", "black")
.append("g")
.attr("class", "text-group");
g.append("text")
.attr("class", "name-text")
.text(d.data.name)
.attr('text-anchor', 'middle')
.attr('dy', '-1.2em');
g.append("text")
.attr("class", "value-text")
.text(d.data.value)
.attr('text-anchor', 'middle')
.attr('dy', '.6em');
})
.on("mouseout", function() {
d3.select(this)
.style("cursor", "none")
.style("fill", color(this._current))
.select(".text-group").remove();
})
.append('path')
.attr('d', arc)
.attr('fill', (d,i) => color(i))
.on("mouseover", function() {
d3.select(this)
.style("cursor", "pointer")
.style("fill", "black");
})
.on("mouseout", function() {
d3.select(this)
.style("cursor", "none")
.style("fill", color(this._current));
})
.each(function(d, i) { this._current = i; });
g.append('text')
.attr('text-anchor', 'middle')
.attr('dy', '.35em')
.text(text);
}