Donut Chart With visx in React
Hello, In this guide, we will learn how to create a Progress Donut chart using visx. A Donut chart is a variant of a pie chart featuring a central hole, resembling a donut.
Understanding the Math
To effectively implement the features of our chart, it is essential to grasp the mathematical principles behind it. The chart is a circle with 360 degrees or 2 * Pi radians. Here's how we determine the angles for each progress segment :
2 * PI / (number of progress data points)
The starting angle for each progress segment is derived by multiplying the index by 2 * Pi divided by the total number of progress data points :
(index) * 2 * PI / (number of progress data points )
The ending angle of a progress segment is calculated by adding the progress percentage to the index and then multiplying by 2 * Pi divided by the total number of progress data points :
(index + (progress / 100)) * 2 * PI / (number of progress data points )
For the track bar representing remaining progress, the start angle is the same as the end angle of the progress segment, while the end angle is the start angle of the progress segment plus the total progress of that segment.
(index + (progress / 100)) * 2 * PI / (number of progress data points )
the track bar endAngle :
(index + 1) * 2 * PI / (number of progress data points)
Donut Chart Code
The first step in developing the chart is to organize the necessary data. In the data.js file, you will define symbols for progress data, the progress amount, and corresponding colors.
export const coins = [ { symbol: "r", color: "#121212", progress: 30, }, { symbol: "l", color: "#91235d", progress: 37, }, { symbol: "s", color: "#5ef13f", progress: 90, }, { symbol: "w", color: "#643dfe", progress: 50, }, { symbol: "d", color: "#ef0de6", progress: 45, }, ];
Next, let's implement the Donut Chart Component. Utilize the math calculations described above to dynamically generate each progress segment's angles and accompanying track bar.
import { Pie } from "@visx/shape"; import { Group } from "@visx/group"; import { scaleOrdinal } from "@visx/scale"; import { Text } from "@visx/text"; const margin = { top: 10, right: 10, bottom: 10, left: 10 }; const thickness = 25; export default function Donut({ width, height, data, title, }: { width: number; height: number; data: { symbol: string; progress: number; color: string }[]; title: string; }) { const innerWidth = width - margin.left - margin.right; const innerHeight = height - margin.top - margin.bottom; const radius = Math.min(innerWidth, innerHeight) / 2; const centerY = innerHeight / 2; const centerX = innerWidth / 2; const getBrowserColor = scaleOrdinal({ domain: data.map((d) => d.symbol), range: data.map(item => item.color), }); return ( <svg width={width} height={height}> <Group top={centerY + margin.top} left={centerX + margin.left}> <Pie data={data} pieValue={(d) => d.progress / 100} outerRadius={radius} innerRadius={radius - thickness + 21} > {({ arcs, path }) => { arcs = arcs.map((item, index) => { return ({ ...item, startAngle: (index) * (Math.PI * 2 / data.length), endAngle: (((index + (item.data.progress / 100)) * (Math.PI * 2 / data.length))), }) }) return ( <g > {arcs.map((arc, i) => { const firstArc = { ...arc, startAngle: arc.startAngle, endAngle: arc.endAngle } const second = { ...arc, startAngle: arc.endAngle, endAngle: arc.startAngle + Math.PI * 2 /data.length} return ( <> <g key={`pie-arc-${i}+1`}> <path className={`arc${i}`} d={path(firstArc)} fill={getBrowserColor(arc.data.symbol)} /> </g> <g key={`pie-arc-${i}+2`}> <path className={`arc${i}`} d={path(second)} fill={'#E4E4E4'} /> </g> </> ) })} </g> ) }} </Pie> <Text className="whitespace-wrap" textAnchor="middle" verticalAnchor={'middle'} fill="black" scaleToFit fontFamily="sans-serif" > {title} </Text> </Group> </svg>) }
Please don't hesitate to reach out if you require further clarification or assistance with constructing the Donut Chart Component. Thank you for reading this article the live demo is here.
The above is the detailed content of Donut Chart With visx in React. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.
