当前位置:网站首页>canvas--pie chart
canvas--pie chart
2022-08-02 03:38: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
//Use proportions to draw arcs
pen.arc(300, 300, r1, stardeg * deg, (stardeg + angle) * deg)
//Connect the arc to the center of the circle,form a fan
pen.lineTo(300, 300)
//Add an array to each sectorname
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>
边栏推荐
- 【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
- Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
- Phospholipid-polyethylene glycol-targeted neovascularization targeting peptide APRPG, DSPE-PEG-APRPG
- @Autowired详解[email protected]在static属性上的使用
- IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
- ThunderBirde无法登录问题、pycharm调试一直收集数据、RuntimeError: CUDA error: device-side assert triggered等疑难杂症解决
- Deveco studio 鸿蒙app访问网络详细过程(js)
- C语言 十六进制整数字符串转十进制整数
- parser = argparse.ArgumentParser()解析
- ssm various configuration templates
猜你喜欢

Week 7 Review

C语言入门小游戏—三子棋

Cloud server installation and deployment of Nacos 2.0.4 version

【 application 】 life many years of operations, what turned scored 12 k + annual bonus salary?

啃瓜记录第一天

npm--package.json---require

ModuleNotFoundError No module named ‘xxx‘可能的解决方案大全

新工程加载YOLOV6的预训练权重问题

C语言的变长数组

Redis simple study notes
随机推荐
Scientific research reagent DMPE-PEG-Mal dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide
The @autowired distinguished from @ the Resource
display,visibility,opacity
String comparison size in MySQL (date string comparison problem)
AttributeError: Can't get attribute 'SPPF' on
小程序 van-cell 换行能左对齐问题
PCL—点云数据分割
【我的创作纪念日】 3周年
The difference between the knowledge question and answer session with the knowledge
排序学习笔记(二)堆排序
最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
啃瓜记录第一天
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
SSM整合
配置mmdet来训练Swin-Transformer之一配置环境
如何查看一个现有的keil工程之前由什么版本的keil IDE编译
getattr()函数解析
URL模块
nucleo stm32 h743 FREERTOS CUBE MX配置小记录
客户评分控件