Chart.js Doughnut with rounded edges(Chart.js 圆角甜甜圈)
问题描述
我用 Chart.js 创建了一个圆环图,我希望它的两端都有圆形边缘.我希望它是这样的:
但我有这样的,边缘锋利:
我找到的最好的答案是:
Chart.defaults.RoundedDoughnut = Chart.helpers.clone(Chart.defaults.doughnut);Chart.controllers.RoundedDoughnut = Chart.controllers.doughnut.extend({绘制:功能(轻松){var ctx = this.chart.ctx;var easingDecimal = 轻松 ||1个;var arcs = this.getMeta().data;Chart.helpers.each(arcs, function(arc, i) {arc.transition(easingDecimal).draw();var pArc = 弧[i === 0 ?arcs.length - 1 : i - 1];var pColor = pArc._view.backgroundColor;var vm = arc._view;变量半径 = (vm.outerRadius + vm.innerRadius)/2;var 厚度 = (vm.outerRadius - vm.innerRadius)/2;var startAngle = Math.PI - vm.startAngle - Math.PI/2;变角 = Math.PI - vm.endAngle - Math.PI/2;ctx.save();ctx.translate(vm.x, vm.y);ctx.fillStyle = i === 0 ?vm.backgroundColor : pColor;ctx.beginPath();ctx.arc(半径 * Math.sin(startAngle), 半径 * Math.cos(startAngle), 厚度, 0, 2 * Math.PI);ctx.fill();ctx.fillStyle = vm.backgroundColor;ctx.beginPath();ctx.arc(半径 * Math.sin(角度), 半径 * Math.cos(角度), 厚度, 0, 2 * Math.PI);ctx.fill();ctx.restore();});}});window.onload = 函数(){新图表(document.getElementById('usersChart'),{类型:'圆形甜甜圈',数据 : {数据集:[{数据:[40、20、20、20],背景颜色: ['#e77099','#5da4e7','#8f75e7','#8fe768'],边框宽度:0}]},选项: {剪裁百分比:70}});};
<script src="https://github.com/chartjs/Chart.js/releases/download/v2.9.3/Chart.min.js"></脚本><link rel="stylesheet" href="https://github.com/chartjs/Chart.js/releases/download/v2.9.3/Chart.min.css"><div style="width: 200px; height: 200px;"><canvas id="usersChart" width="1" height="1"></canvas></div>
I created a donut chart with Chart.js and I want it to have rounded edges at both ends. I want it to be like this:
But I have it like this, with sharp edges:
The best I found was this answer: How to put rounded corners on a Chart.js Bar chart, but it is for bar charts, and I have no clue of how to adapt it for doughnuts..
Here is my code:
HTML
<div class="modal-div-canvas js-chart">
<div class="chart-canvas">
<canvas id="openedCanvas" width="1" height="1"></canvas>
<div class="chart-background"></div>
<span class="chart-unique-value">
<span class="js-count">
85
</span>
<span class="cuv-percent">%</span>
</span>
</div>
</div>
JS
var deliveredData = {
labels: [
"Value"
],
datasets: [
{
data: [85, 15)],
backgroundColor: [
"#3ec556",
"rgba(0,0,0,0)"
],
hoverBackgroundColor: [
"#3ec556",
"rgba(0,0,0,0)"
],
borderWidth: [
0, 0
]
}]
};
var deliveredOpt = {
cutoutPercentage: 88,
animation: {
animationRotate: true,
duration: 2000
},
legend: {
display: false
},
tooltips: {
enabled: false
}
};
var chart = new Chart($('#openedCanvas'), {
type: 'doughnut',
data: deliveredData,
options: deliveredOpt
});
}};
Someone know how to do this?
I made some changes in the @potatopeeling snippet, I made compatibility with the newer (2.9.x) version of chart.js also fixed where the "startArc" should be rendered and the color from the previous segment to match this "startArc", so we can have more than 2 segments. This is the result:
Chart.defaults.RoundedDoughnut = Chart.helpers.clone(Chart.defaults.doughnut);
Chart.controllers.RoundedDoughnut = Chart.controllers.doughnut.extend({
draw: function(ease) {
var ctx = this.chart.ctx;
var easingDecimal = ease || 1;
var arcs = this.getMeta().data;
Chart.helpers.each(arcs, function(arc, i) {
arc.transition(easingDecimal).draw();
var pArc = arcs[i === 0 ? arcs.length - 1 : i - 1];
var pColor = pArc._view.backgroundColor;
var vm = arc._view;
var radius = (vm.outerRadius + vm.innerRadius) / 2;
var thickness = (vm.outerRadius - vm.innerRadius) / 2;
var startAngle = Math.PI - vm.startAngle - Math.PI / 2;
var angle = Math.PI - vm.endAngle - Math.PI / 2;
ctx.save();
ctx.translate(vm.x, vm.y);
ctx.fillStyle = i === 0 ? vm.backgroundColor : pColor;
ctx.beginPath();
ctx.arc(radius * Math.sin(startAngle), radius * Math.cos(startAngle), thickness, 0, 2 * Math.PI);
ctx.fill();
ctx.fillStyle = vm.backgroundColor;
ctx.beginPath();
ctx.arc(radius * Math.sin(angle), radius * Math.cos(angle), thickness, 0, 2 * Math.PI);
ctx.fill();
ctx.restore();
});
}
});
window.onload = function() {
new Chart(document.getElementById('usersChart'), {
type : 'RoundedDoughnut',
data : {
datasets: [
{
data : [40, 20, 20, 20],
backgroundColor: [
'#e77099',
'#5da4e7',
'#8f75e7',
'#8fe768'
],
borderWidth : 0
}]
},
options: {
cutoutPercentage: 70
}
});
};
<script src="https://github.com/chartjs/Chart.js/releases/download/v2.9.3/Chart.min.js"></script>
<link rel="stylesheet" href="https://github.com/chartjs/Chart.js/releases/download/v2.9.3/Chart.min.css">
<div style="width: 200px; height: 200px;">
<canvas id="usersChart" width="1" height="1"></canvas>
</div>
这篇关于Chart.js 圆角甜甜圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Chart.js 圆角甜甜圈
基础教程推荐
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01