[D3.js advanced series-6.2] drag and drop a pie chart, d3.js6.2

Source: Internet
Author: User

[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.