score:1
There's many ways to do what you want. Here's a quick refactor of your second example:
<html>
<head>
<script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script>
<title></title>
<body>
<div id="duration">
<svg style="height:300px;width:100%"></svg>
<button class="random">Random Inner</button>
<button class="random">Random Outer</button>
</div>
<script type="text/javascript">
var dataset = {
apples: [13245, 28479, 11111, 11000, 3876],
oranges: [53245, 28479, 19697, 24037, 19654],
};
var width = d3.select('#duration').node().offsetWidth,
height = 300,
cwidth = 33;
var color = [
['#1352A4', '#2478E5', '#5D9CEC', '#A4C7F4', '#DBE8FB'],
['#58A53B', '#83C969', '#A8D996', '#CDE9C3', '#E6F4E1']
];
var pie = d3.layout.pie()
.sort(null);
var arcs = [d3.svg.arc()
.innerRadius(95)
.outerRadius(cwidth * 2.5),
d3.svg.arc()
.innerRadius(70 + cwidth)
.outerRadius(cwidth * 3.5)
];
var svg = d3.select("#duration svg")
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var gs = svg.selectAll("g")
.data(d3.values(dataset))
.enter()
.append("g")
.attr("class", function(d, i) {
return i === 0 ? "inner" : "outer";
});
var path = gs.selectAll("path")
.data(function(d, i) {
return pie(d);
})
.enter().append("path")
.attr("fill", function(d, i, j) {
return color[j][i];
})
.attr("d", function(d, i, j) {
return arcs[j](d);
});
d3.selectAll(".random").on("click", function(d, i) {
var slices = i === 0 ? d3.selectAll(".inner>path") : d3.selectAll(".outer>path");
var randData = pie(d3.range(5).map(function() {
return Math.random() * 1000;
}));
randData.forEach(function(d, i) {
d._current = slices.data()[i];
})
slices
.data(randData)
.transition().duration(1000)
.attrTween("d", function(d) {
var interpolate = d3.interpolate(d._current, d);
return function(t) {
return arcs[i](interpolate(t));
};
})
})
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- D3.js: randomly animate donut inside another donut
- Drag multiple items inside another item in d3
- Fill the inside of a pie donut chart d3
- D3.js Mouseover text inside donut chart
- D3.js how to make the lines not go inside the arc of a donut chart
- Donut chart: show percentage inside
- Inserting image inside donut
- drag and drop a circle inside another circle with re size and auto adjust
- how to animate the labels to flow with the donut chart with a start angle and end angle in d3.
- D3JS: Animate a 2 arc donut
- D3 function called inside another function always returns undefined
- trying to create a donut chart with labels inside the curve using d3.js
- d3 selection append element inside another
- D3 animate donut chart while updating it with different data length
- D3 - Event binding inside another event
- Acess object's attribute inside another object using Javascript / D3.js
- How do I center a donut chart inside an svg
- append a element svg inside another svg in d3.js
- How do I make a D3.js donut chart (aster plot) animate on page load with transition
- Drawing Labels using TextPath inside a donut chart
- d3js add tag inside another tag
- How to get absolute coordinates of object inside a <g> group?
- Is it possible to determine if a GeoJSON point is inside a GeoJSON polygon using JavasScript?
- D3 Selecting an element inside an SVG
- Animating D3 donut chart on load
- Can i use d3.js for creating interactive visualizations inside an android app?
- How to add drop shadow to d3.js pie or donut chart
- D3.js animate rotation
- How do you make an SVG element mouse event bubble up through another element?
- how to create labels for data in donut chart using d3.js
More Query from same tag
- cant figure out d3.js error
- d3.js v5 changing an element from an imported svg
- d3 read csv with d3.map and d3 queue() function
- d3.js, is it possible to append hyperlinks to GeoJSON file?
- D3 Force Layout - No overlapping links
- Removing node and its children from d3js tree layout
- How do I change the colour of my d3.js Bar Graph bars individually and make a Scale for them?
- Filter nested objects on client side
- D3 issue in IE9
- dc.js: Change x-axis Increments
- D3 array object format not being picked up
- Data not formatted to date properly in d3
- Both mouseleave and mouseout triggering too often
- Facing issues while grouping data for bar chart in dc js using crossfilter
- D3.js version 5 chart to PDF
- Proper way to select child nodes in D3
- getting elements in DOM in d3
- Wrong usage of d3 scale with same values in data?
- Outputting SVG Graph from D3.js Using Phantomjs
- Calculating a the length of an arc in D3
- Is there a D3 Gauge allowing multiple series/columns?
- Animate multi line plot with variable speed in D3
- How to use this data with D3js Scatter Plot?
- order a capped chart with fake group does not work in dc.js
- Using vanilla JS selectors with D3.js
- Processing nested json data into data joins
- Setting tick intervals on a D3 time axis throwing an exception
- How to instantiate scala.scalajs.js.ThisFunction3 in this example using org.singlespaced.d3js' data function
- How to prevent duplicating nested elements on update with D3.js
- dc.js : how to change circle color on mouseover