[D3.js advanced series-6.2] drag and drop a pie chart, d3.js6.2
This article describes a more complex drag-and-drop application, that is, the drag-and-drop Pie Chart Section.
In Chapter 9.1, I will explain how to create a pie chart. Each part of a pie chart is represented by an arc with width. When interacting with users, it is interesting to drag and drop each part.
1. Creating a pie chart
Unlike [Entry-Chapter 9.1], we use a g label to enclose each area of a pie chart for translation.
Var gAll = svg. append ("g "). attr ("transform", "translate (" + outerRadius + "," + outerRadius + ")"); var arcs = gAll. selectAll (". arcs_g "). data (pie (dataset )). enter (). append ("g "). each (function (d) {// specify the translation volume of the current region d. dx = 0; d. dy = 0 ;}). call (drag); // call the drag Function
In translation, you only need to use transform for the g of each part. When a drag event occurs, the offset can be calculated based on the mouse parameters. The above uses an each () function to add two variables for each region: dx and dy. Used to save the offset.
2. drag event Definition
Every time a drag event is triggered, we only need to get the offset of the mouse and add it to the original offset dx and dy.
Then use d3.select (this) to select the current element and apply transform to it to complete the translation operation.
var drag = d3.behavior.drag().origin(function(d) { return d; }).on("drag", dragmove);function dragmove(d) {d.dx += d3.event.dx;d.dy += d3.event.dy;d3.select(this).attr("transform","translate("+d.dx+","+d.dy+")");}
3. Results
The result is as follows:
Click the following link to view the source code:
Http://www.ourd3js.com/demo/J-6.2/dragpie.html
Thank you for reading.
Document Information
- Copyright Disclaimer: BY-non-commercial (NC)-deduction prohibited (ND)
- Published on: February 1, January 6, 2015
- More content: OUR D3.JS-data visualization special site and CSDN personal blog
- Note: This article is published in OUR D3.JS. For more information, see the source. Thank you.