Chart.js Doughnut with rounded edges

后端 未结 2 1847
借酒劲吻你
借酒劲吻你 2020-12-15 09:23

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

2条回答
  •  抹茶落季
    2020-12-15 09:45

    You can extend the chart to do this


    Preview


    Script

    Chart.defaults.RoundedDoughnut = Chart.helpers.clone(Chart.defaults.doughnut);
    Chart.controllers.RoundedDoughnut = Chart.controllers.doughnut.extend({
        draw: function (ease) {
            var ctx = this.chart.chart.ctx;
    
            var easingDecimal = ease || 1;
            Chart.helpers.each(this.getDataset().metaData, function (arc, index) {
                arc.transition(easingDecimal).draw();
    
                var vm = arc._view;
                var radius = (vm.outerRadius + vm.innerRadius) / 2;
                var thickness = (vm.outerRadius - vm.innerRadius) / 2;
                var angle = Math.PI - vm.endAngle - Math.PI / 2;
    
                ctx.save();
                ctx.fillStyle = vm.backgroundColor;
                ctx.translate(vm.x, vm.y);
                ctx.beginPath();
                ctx.arc(radius * Math.sin(angle), radius * Math.cos(angle), thickness, 0, 2 * Math.PI);
                ctx.arc(radius * Math.sin(Math.PI), radius * Math.cos(Math.PI), thickness, 0, 2 * Math.PI);
                ctx.closePath();
                ctx.fill();
                ctx.restore();
            });
        },
    });
    

    and then

    ...
    type: 'RoundedDoughnut',
    ...
    

    Stack Snippet

    Chart.defaults.RoundedDoughnut = Chart.helpers.clone(Chart.defaults.doughnut);
            Chart.controllers.RoundedDoughnut = Chart.controllers.doughnut.extend({
                draw: function (ease) {
                		var ctx = this.chart.chart.ctx;
                    
                    var easingDecimal = ease || 1;
                    Chart.helpers.each(this.getDataset().metaData, function (arc, index) {
                        arc.transition(easingDecimal).draw();
    
                        var vm = arc._view;
                        var radius = (vm.outerRadius + vm.innerRadius) / 2;
                        var thickness = (vm.outerRadius - vm.innerRadius) / 2;
                        var angle = Math.PI - vm.endAngle - Math.PI / 2;
                        
                        ctx.save();
                        ctx.fillStyle = vm.backgroundColor;
                        ctx.translate(vm.x, vm.y);
                        ctx.beginPath();
                        ctx.arc(radius * Math.sin(angle), radius * Math.cos(angle), thickness, 0, 2 * Math.PI);
                        ctx.arc(radius * Math.sin(Math.PI), radius * Math.cos(Math.PI), thickness, 0, 2 * Math.PI);
                        ctx.closePath();
                        ctx.fill();
                        ctx.restore();
                    });
                },
            });
    
            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: 'RoundedDoughnut',
                data: deliveredData,
                options: deliveredOpt
            });
    
    
    
    

提交回复
热议问题