当前位置:网站首页>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>
边栏推荐
- STM32 触发HardFault_Handler如何查找原因
- np.isnan()
- FreeRTOS内核详解(1) —— 临界段保护原理
- basic operator
- ImportError: libGL.so.1: cannot open shared object file: No such file or directory
- 【面试】失败的一次面试
- AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
- Mysql8.0安装教程
- 科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
- Mysql8创建用户以及赋权操作
猜你喜欢

C语言的变长数组

语义分割标签即像素值的巨坑,transforms.ToTensor()的错误使用

Usage of JOIN in MySQL

通过PS 2021 将网页图标抠下来

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘

PCL—point cloud data segmentation

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

网址URL

MySql创建数据表

MySQL分组后取最大一条数据【最优解】
随机推荐
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
C语言 void和void *(无类型指针)
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/yolov5-5.0/models/commo
小程序 van-cell 换行能左对齐问题
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000
【面试】失败的一次面试
【面经】米哈游数据开发一面二面面经
COCO数据集训练TPH-YoloV5
np.unique() function
排序学习笔记(二)堆排序
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰活性基团
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group
磷脂-聚乙二醇-叠氮,DSPE-PEG-Azide,DSPE-PEG-N3,MW:5000
URL模块
【深度学习】从LeNet-5识别手写数字入门深度学习
Source Insight 使用教程(2)——常用功能
nucleo stm32 h743 FREERTOS CUBE MX配置小记录
【装机】老毛桃的安装及使用
@Autowired详解[email protected]在static属性上的使用
JJWT tool class