After working for several hours, I'm having trouble getting my d3.js donut graph to update with new data.
Here's my HTML:
<body>
<div id="pie"></div>
<script src="pie.js"></script>
</body>
And here's my JavaScript code:
var dataset = [40, 20];
var width = 460,
height = 300,
radius = Math.min(width, height) / 2;
var color = ['#000000', '#FFFFFF'];
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
function render() {
var pie = d3.pie()
.sort(null);
var arc = d3.arc()
.innerRadius(radius - 100)
.outerRadius(radius - 50);
var path = svg.selectAll("path")
.data(pie(dataset))
.enter().append("path")
.attr("fill", function(d, i) { return color[i]; })
.attr("d", arc);
}
render();
function update() {
dataset[0] = 100;
render();
}
Although the donut graph is drawn successfully, calling the update()
function doesn't refresh the graph on screen with the updated dataset.
I've looked at examples of bar charts using enter, append, and exit methods, but I'm struggling to apply them in my case.
If anyone can provide some guidance, it would be greatly appreciated. Thank you!