当前位置:网站首页>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>
边栏推荐
猜你喜欢
随机推荐
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
COCO数据集训练TPH-YoloV5
【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
parser = argparse.ArgumentParser() parsing
Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
Source Insight 使用教程(2)——常用功能
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/yolov5-5.0/models/commo
AttributeError: Can't get attribute 'SPPF' on
debian 10 nat 与路由转发
MySQL分区表详解
网址URL
Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
nucleo stm32 h743 FREERTOS CUBE MX配置小记录
Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案
通过PS 2021 将网页图标抠下来
解决glob()返回文件排序不一致问题&onnx本地按照安装方法
Mysql8创建用户以及赋权操作
DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
mysql卸载详细教程
np.unique()函数









