Skip to main content
St Louis

Back to all posts

How to Draw Multiseries Donut Chart Using D3.js?

Published on
8 min read
How to Draw Multiseries Donut Chart Using D3.js? image

Best Data Visualization Tools to Buy in November 2025

1 Data Visualization with Microsoft Power BI: How to Design Savvy Dashboards

Data Visualization with Microsoft Power BI: How to Design Savvy Dashboards

BUY & SAVE
$41.33 $59.99
Save 31%
Data Visualization with Microsoft Power BI: How to Design Savvy Dashboards
2 Data Points: Visualization That Means Something

Data Points: Visualization That Means Something

BUY & SAVE
$25.00 $42.00
Save 40%
Data Points: Visualization That Means Something
3 Good Charts Workbook: Tips, Tools, and Exercises for Making Better Data Visualizations

Good Charts Workbook: Tips, Tools, and Exercises for Making Better Data Visualizations

BUY & SAVE
$17.58 $35.00
Save 50%
Good Charts Workbook: Tips, Tools, and Exercises for Making Better Data Visualizations
4 Python Data Science Handbook: Essential Tools for Working with Data

Python Data Science Handbook: Essential Tools for Working with Data

BUY & SAVE
$44.18 $79.99
Save 45%
Python Data Science Handbook: Essential Tools for Working with Data
5 Good Charts, Updated and Expanded: The HBR Guide to Making Smarter, More Persuasive Data Visualizations

Good Charts, Updated and Expanded: The HBR Guide to Making Smarter, More Persuasive Data Visualizations

BUY & SAVE
$24.87 $35.00
Save 29%
Good Charts, Updated and Expanded: The HBR Guide to Making Smarter, More Persuasive Data Visualizations
6 Storytelling with Data: A Data Visualization Guide for Business Professionals, 10th Anniversary Edition

Storytelling with Data: A Data Visualization Guide for Business Professionals, 10th Anniversary Edition

BUY & SAVE
$49.95 $59.95
Save 17%
Storytelling with Data: A Data Visualization Guide for Business Professionals, 10th Anniversary Edition
7 Data Visualization with Excel Dashboards and Reports

Data Visualization with Excel Dashboards and Reports

BUY & SAVE
$23.39 $42.00
Save 44%
Data Visualization with Excel Dashboards and Reports
8 Interactive Data Visualization for the Web: An Introduction to Designing with D3

Interactive Data Visualization for the Web: An Introduction to Designing with D3

BUY & SAVE
$26.25 $54.99
Save 52%
Interactive Data Visualization for the Web: An Introduction to Designing with D3
9 Data Analytics, Data Visualization & Communicating Data: 3 books in 1: Learn the Processes of Data Analytics and Data Science, Create Engaging Data ... Present Data Effectively (All Things Data)

Data Analytics, Data Visualization & Communicating Data: 3 books in 1: Learn the Processes of Data Analytics and Data Science, Create Engaging Data ... Present Data Effectively (All Things Data)

BUY & SAVE
$19.99
Data Analytics, Data Visualization & Communicating Data: 3 books in 1: Learn the Processes of Data Analytics and Data Science, Create Engaging Data ... Present Data Effectively (All Things Data)
+
ONE MORE?

To draw a multiseries donut chart using d3.js, you can follow the steps below:

  1. Set up your HTML page with the necessary elements, like an SVG container to hold the chart.
  2. Include the d3.js library by adding a script tag in your HTML.
  3. Create an array of data that represents the different series of your donut chart. Each series should have a label and a value.
  4. Define the dimensions of your chart, such as the width and height of the SVG container.
  5. Use d3.js to create a pie generator by calling the d3.pie() function. This function will convert your data into pie angles.
  6. Create an arc generator using the d3.arc() function. This will be used to draw the individual arcs for each series in the donut chart.
  7. Append a g element to the SVG container and translate it to the center of the chart using svg.append("g").attr("transform", "translate(" + width / 2 + "," + height / 2 + ")").
  8. Bind the pie data to the g element using selection.data() and create a group for each series using selection.enter().append("g").
  9. Within each group, generate the paths for the arcs using the arc generator, and set the fill color based on the series using style("fill", function(d, i) { return color(i); }).
  10. Add tooltips to the arcs by appending title elements to the path selections, which will display the label and value of each series when hovered over.
  11. Finally, add a legend to the chart by creating another g element, and within it, create group elements for each series. Use rectangles or circles to represent each series and add corresponding labels.
  12. Apply any necessary styling to the chart and legend elements using CSS.

By following these steps, you should be able to draw a multiseries donut chart using d3.js. Remember to customize and adapt the code to fit your specific requirements and design preferences.

What are the options for adding legends to a donut chart in d3.js?

There are several options for adding legends to a donut chart in d3.js. Here are a few commonly used approaches:

  1. Using HTML and CSS: You can create a separate HTML element to hold the legend and style it using CSS. Generally, this involves creating a

    or

    container and manually adding color-coded legend items with labels. You can assign CSS classes or IDs to each item and use them to style the legend as desired.

  2. Utilizing data binding: In d3.js, you can use data binding to dynamically generate legend items based on the data used for the donut chart. By mapping a separate dataset for the legend, you can create legend elements that correspond to the chart slices. This involves mapping data values to colors and labels, and then appending legend items to an SVG container.

  3. Utilizing d3-legend library: D3.js has a library called d3-legend that provides pre-built legend functionality. This library allows you to easily generate legends for various types of charts, including donut charts. You can customize the appearance and placement of the legend using the library's available options.

  4. Manual creation using SVG: Another option is to manually create the legend using SVG elements. You can create rectangles or circles with corresponding labels and position them appropriately on the SVG container. You would need to set the fill or stroke properties of the legend elements based on the data used for the donut chart.

These options provide flexibility in terms of customization and design. Which one to choose depends on the specific requirements of your project and your familiarity with d3.js and web development technologies.

What is the role of scales in a donut chart using d3.js?

Scales in a donut chart using d3.js are used to map data values to visual attributes such as the radius or angle of the chart. The role of scales is to provide a way to transform the range of data values into a suitable range of feasible values for the chart.

For example, in a donut chart, you may want to use the data values to determine the radius of each slice. In this case, you can use a scale, such as d3.scaleLinear(), to map the data values to a range of suitable radius values that can be used to draw the chart.

Scales can also be used to determine the start and end angles of each slice in the donut chart, allowing you to represent the proportional values of the data. Using scales ensures that the chart accurately represents the data values in a visually meaningful way.

Additionally, scales can be used to assign colors to each slice based on the data values, creating visual differentiation and aiding in the understanding of the chart.

Overall, with the help of scales, a donut chart in d3.js can effectively represent the data values in a visually appealing and informative manner.

How to handle click events on a donut chart using d3.js?

To handle click events on a donut chart using d3.js, you can follow these steps:

  1. Define the data and configure the donut chart using d3.js. Here's an example:

// Define the data var data = [ { name: "Slice 1", value: 30 }, { name: "Slice 2", value: 50 }, { name: "Slice 3", value: 20 } ];

// Configure the donut chart var width = 300, height = 300, radius = Math.min(width, height) / 2;

var color = d3.scaleOrdinal(d3.schemeCategory10);

var svg = d3 .select("#chart") .append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var arc = d3.arc().innerRadius(radius * 0.6).outerRadius(radius * 0.8);

var pie = d3 .pie() .value(function(d) { return d.value; }) .sort(null);

var path = svg .selectAll("path") .data(pie(data)) .enter() .append("path") .attr("d", arc) .attr("fill", function(d) { return color(d.data.name); });

  1. Add a click event handler to the donut slices. You can use the .on("click", callback) method to handle click events. Inside the callback function, you can access the data associated with the clicked slice. Here's an example:

path.on("click", function(d) { console.log("Clicked on:", d.data.name); });

  1. Customize the click event handler according to your needs. For example, you can update the chart or perform any other action based on the clicked slice's data.

Here's the full example with the click event handler:

// Define the data var data = [ { name: "Slice 1", value: 30 }, { name: "Slice 2", value: 50 }, { name: "Slice 3", value: 20 } ];

// Configure the donut chart var width = 300, height = 300, radius = Math.min(width, height) / 2;

var color = d3.scaleOrdinal(d3.schemeCategory10);

var svg = d3 .select("#chart") .append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var arc = d3.arc().innerRadius(radius * 0.6).outerRadius(radius * 0.8);

var pie = d3 .pie() .value(function(d) { return d.value; }) .sort(null);

var path = svg .selectAll("path") .data(pie(data)) .enter() .append("path") .attr("d", arc) .attr("fill", function(d) { return color(d.data.name); }) .on("click", function(d) { console.log("Clicked on:", d.data.name); });

By using the above code, you should be able to handle click events on a donut chart using d3.js. The console will log the name of the clicked slice.

How to draw a donut chart using d3.js?

To draw a donut chart using d3.js, you can follow these steps:

  1. Import the required libraries:
  1. Create a container for the chart:
  1. Set the dimensions and margins for the chart:

var width = 400; var height = 400; var margin = 40; var radius = Math.min(width, height) / 2 - margin;

  1. Create an SVG element within the chart container:

var svg = d3.select("#chart") .append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

  1. Define the colors for the chart:

var color = d3.scaleOrdinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

  1. Prepare the data for the chart:

var data = [ { label: "Label 1", value: 20 }, { label: "Label 2", value: 30 }, { label: "Label 3", value: 40 }, // Add more data as needed ];

  1. Set up the chart arcs:

var pie = d3.pie() .value(function(d) { return d.value; }) .sort(null);

var arc = d3.arc() .innerRadius(radius * 0.5) .outerRadius(radius * 0.8);

  1. Draw the chart:

var g = svg.selectAll(".arc") .data(pie(data)) .enter().append("g") .attr("class", "arc");

g.append("path") .attr("d", arc) .style("fill", function(d) { return color(d.data.label); });

g.append("text") .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; }) .attr("dy", ".35em") .text(function(d) { return d.data.label; });

By following these steps, you should be able to draw a donut chart using d3.js with the provided data.