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.
以上是Donut Chart With visx in React的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript核心数据类型在浏览器和Node.js中一致,但处理方式和额外类型有所不同。1)全局对象在浏览器中为window,在Node.js中为global。2)Node.js独有Buffer对象,用于处理二进制数据。3)性能和时间处理在两者间也有差异,需根据环境调整代码。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版
中文版,非常好用

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境