当前位置:网站首页>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>
边栏推荐
- Redis simple study notes
- DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
- docker 安装 sqlserver中的坑点
- [Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
- cross-domain problem solving
- pyppeteer使用样例脚本
- Debian 10 NTP Service Configuration
- @Configuration详解
- Redis笔记进阶篇:万字长文-整理Redis,各种知识点,建议收藏
- Phospholipid-polyethylene glycol-targeted neovascularization targeting peptide APRPG, DSPE-PEG-APRPG
猜你喜欢
随机推荐
2022年比若依更香的开源项目
MySQL常见的索引
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000
COCO数据集训练TPH-YoloV5
Mysql8.0安装教程
DSPE-PEG-PDP,DSPE-PEG-OPSS,磷脂-聚乙二醇-巯基吡啶供应,MW:5000
云服务器安装部署Nacos2.0.4版本
针对简历上的问题
@ApiModel 和 @ApiModelProperty
【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
啃瓜记录第一天
【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
basic operator
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
nucleo stm32 h743 FREERTOS CUBE MX配置小记录
Deveco studio Hongmeng app access network detailed process (js)
js 取字符串中某位置某特征的值,如华为(Huawei)=>华为
排序学习笔记(二)堆排序
C语言 十六进制整数字符串转十进制整数
MySQL删除表数据 MySQL清空表命令 3种方法









