当前位置:网站首页>canvas--饼状图
canvas--饼状图
2022-08-02 03:22:00 【cjx177187】

<body>
<style>
#box {
border: 1px solid black;
}
</style>
<canvas id="box" width="600" height="600"></canvas>
<script>
var canvas = document.querySelector("#box")
var pen = canvas.getContext("2d")
var deg = Math.PI / 180
//假数据
var arr = [{
name: "clothes",
money: 8000
},
{
name: "car",
money: 1580
},
{
name: "eat",
money: 5790
},
{
name: "cash",
money: 4090
},
{
name: "foot",
money: 2439
},
]
//总价
arr.tatol = 0
for (let i = 0; i < arr.length; i++) {
arr.tatol = arr.tatol + arr[i].money
}
var stardeg = 0
arr.forEach(el => {
pen.beginPath()
var r1 = 200
//随机颜色
var r = parseInt(Math.random() * 255)
var g = parseInt(Math.random() * 255)
var b = parseInt(Math.random() * 255)
pen.fillStyle = `rgb(${ r},${ g},${ b})`
//求出每个money的占比
var angle = (el.money / arr.tatol) * 360
//利用占比来画圆弧
pen.arc(300, 300, r1, stardeg * deg, (stardeg + angle) * deg)
//将圆弧与圆心相连接,形成扇形
pen.lineTo(300, 300)
//给每个扇形添加数组的name
var y1 = 300 + Math.sin((stardeg + angle) * deg-angle*deg/2 ) *( r1+30)
var x1 = 300 + Math.cos((stardeg + angle) * deg-angle*deg/2 ) * (r1+30)
pen.fillText(`${ el.name}`, x1, y1)
stardeg = stardeg + angle
pen.fill()
pen.stroke()
});
</script>
</body>
边栏推荐
- mysql中如何查看表是否被锁
- 化学试剂磷脂-聚乙二醇-羟基,DSPE-PEG-OH,DSPE-PEG-Hydroxyl,MW:5000
- 远程调试、无cuDnn、自定义模块无法导入问题记录
- cross-domain problem solving
- mysql创建表
- DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
- MySQL两阶段提交串讲
- debian 10 nat 与路由转发
- js 取字符串中某位置某特征的值,如华为(Huawei)=>华为
- String comparison size in MySQL (date string comparison problem)
猜你喜欢

啃瓜记录又一天

Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案

解决glob()返回文件排序不一致问题&onnx本地按照安装方法

MySQL分库分表

mysql中如何查看表是否被锁

Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000

ModuleNotFoundError No module named 'xxx' possible solutions

Redis笔记进阶篇:万字长文-整理Redis,各种知识点,建议收藏

AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
[email protected]在static属性上的使用"/>@Autowired详解[email protected]在static属性上的使用
随机推荐
Mysql8.0安装教程
basic operator
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
Redis笔记进阶篇:万字长文-整理Redis,各种知识点,建议收藏
C语言 void和void *(无类型指针)
动态代理工具类
np.unique()函数
Amazon sellers how to improve the conversion
知识工程作业2:知识工程相关领域介绍
[Mianjing] Mihayou data development on one side and two sides
Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
磷脂-聚乙二醇-酰肼,DSPE-PEG-Hydrazide,DSPE-PEG-HZ,MW:5000
The usage of json type field in mysql
C语言 结构体定义方法
Week 7 Review
pyppeteer使用样例脚本
The difference between the knowledge question and answer session with the knowledge
Deveco studio 鸿蒙app访问网络详细过程(js)
np.isnan()
Debian 10 NTP Service Configuration