I am trying to use D3.js to create a pie chart with 3 pie slices. I would like to input the values with the keyboard and the transition when updating the values (when clicking the button) should be smooth, that’s why I use merge() and transition().
In oder words, what I am trying to do is to adapt the code from this given example: https://www.d3-graph-gallery.com/graph/pie_changeData.html
the difference would be that I would like to be able to input the values manually instead of having them in the code (it is fine that it is always 3 pie slices).
For some reason I cannot really adapt the code. Thanks in advance.
JavaScript
x
51
51
1
// set the dimensions and margins of the graph
2
var width = 450
3
height = 450
4
margin = 40
5
6
// The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.
7
var radius = Math.min(width, height) / 2 - margin
8
9
// append the svg object to the div called 'my_dataviz'
10
var svg = d3.select("#my_dataviz")
11
.append("svg")
12
.attr("width", width)
13
.attr("height", height)
14
.append("g")
15
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
16
17
// set the color scale
18
var color = d3.scaleOrdinal()
19
.domain(["a", "b", "c", "d"])
20
.range(d3.schemeDark2);
21
22
// A function that create / update the plot for a given variable:
23
function update() {
24
25
var data = d3.selectAll('.fuel').nodes();
26
27
var pie = d3.pie() //we create this variable, for the values to be readeable in the console
28
.value(function(d) {
29
return d.innerHTML;
30
})(data);
31
32
var u = svg.selectAll("path")
33
.data(pie)
34
35
// Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.
36
u.enter()
37
.append('path')
38
.merge(u)
39
.transition()
40
.duration(5000)
41
.attr('d', d3.arc()
42
.innerRadius(0)
43
.outerRadius(radius)
44
)
45
.attr('fill', function(d) {
46
return (color(d.data.key))
47
})
48
.attr("stroke", "white")
49
.style("stroke-width", "2px")
50
.style("opacity", 1)
51
}
JavaScript
1
26
26
1
<meta charset="utf-8">
2
3
<!-- Load d3.js -->
4
<script src="https://d3js.org/d3.v4.js"></script>
5
6
<!-- Color scale -->
7
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
8
9
10
<!-- Create 3 cells for the input -->
11
12
<td>
13
<input type="number" class="fuel" style="text-align:center">
14
</td>
15
<td>
16
<input type="number" class="fuel" style="text-align:center">
17
</td>
18
<td>
19
<input type="number" class="fuel" style="text-align:center">
20
</td>
21
22
<!-- Add button -->
23
<button onclick="update(data)">Update</button>
24
25
<!-- Create a div where the graph will take place -->
26
<div id="my_dataviz"></div>
Advertisement
Answer
To access the values in the input boxes, use this.value
, not this.innerHtml
, where this
points to the relevant DOM node:
JavaScript
1
52
52
1
// set the dimensions and margins of the graph
2
var width = 450
3
height = 450
4
margin = 40
5
6
// The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.
7
var radius = Math.min(width, height) / 2 - margin
8
9
// append the svg object to the div called 'my_dataviz'
10
var svg = d3.select("#my_dataviz")
11
.append("svg")
12
.attr("width", width)
13
.attr("height", height)
14
.append("g")
15
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
16
17
// set the color scale
18
var color = d3.scaleOrdinal()
19
.domain(["a", "b", "c", "d"])
20
.range(d3.schemeDark2);
21
22
// A function that create / update the plot for a given variable:
23
function update() {
24
25
var data = [];
26
d3.selectAll('.fuel').each(function() {
27
data.push(+this.value || 0);
28
});
29
30
var pie = d3.pie()
31
(data);
32
33
var u = svg.selectAll("path")
34
.data(pie)
35
36
// Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.
37
u.enter()
38
.append('path')
39
.merge(u)
40
.transition()
41
.duration(5000)
42
.attr('d', d3.arc()
43
.innerRadius(0)
44
.outerRadius(radius)
45
)
46
.attr('fill', function(d) {
47
return (color(d.data.key))
48
})
49
.attr("stroke", "white")
50
.style("stroke-width", "2px")
51
.style("opacity", 1)
52
}
JavaScript
1
26
26
1
<meta charset="utf-8">
2
3
<!-- Load d3.js -->
4
<script src="https://d3js.org/d3.v4.js"></script>
5
6
<!-- Color scale -->
7
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
8
9
10
<!-- Create 3 cells for the input -->
11
12
<td>
13
<input type="number" class="fuel" style="text-align:center">
14
</td>
15
<td>
16
<input type="number" class="fuel" style="text-align:center">
17
</td>
18
<td>
19
<input type="number" class="fuel" style="text-align:center">
20
</td>
21
22
<!-- Add button -->
23
<button onclick="update()">Update</button>
24
25
<!-- Create a div where the graph will take place -->
26
<div id="my_dataviz"></div>