【问题标题】:Half Doughnut Chart with Chart.js带有 Chart.js 的半圆环图
【发布时间】:2021-11-23 18:49:44
【问题描述】:

是否可以为 chart.js 圆环图实现动画指标?我要完成的工作如下所示:

我已经完成了图表的甜甜圈部分,但似乎找不到添加值(大文本:89%(动态))或指标的点的方法。

我使用的代码如下:

HTML

<canvas id="dashboardChart" width="400" height="400"></canvas>

JS

var ctx = document.getElementById("dashboardChart");
    var dashboardChart = new Chart(ctx, {
        type: 'doughnut',
        data: {
            labels: ["Red", "Orange", "Green"],
            datasets: [{
                label: '# of Votes',
                data: [33, 33, 33],
                backgroundColor: [
                    'rgba(231, 76, 60, 1)',
                    'rgba(255, 164, 46, 1)',
                    'rgba(46, 204, 113, 1)'
                ],
                borderColor: [
                    'rgba(255, 255, 255 ,1)',
                    'rgba(255, 255, 255 ,1)',
                    'rgba(255, 255, 255 ,1)'
                ],
                borderWidth: 5
            }]

        },
        options: {
            rotation: 1 * Math.PI,
            circumference: 1 * Math.PI,
            legend: {
                display: false
            },
            tooltip: {
                enabled: false
            },
            cutoutPercentage: 95
        }
    });

非常感谢任何帮助!

【问题讨论】:

  • 我构建了一个 CSS 时钟,之前使用指针。只是一个建议。如果你能写出数学,手尖就是点的中心。 (或者你可以做一个手,像一个很酷的速度计)代码在这里:steveaolsen.com/pages/clock.html ...也是画布,可能值得一试。对于动画来说真的很方便。

标签: javascript jquery html css


【解决方案1】:

原答案:

我不确定是否有快速简单的解决方案。

也许您可以直接在现有饼图的顶部创建第二个饼图,并让该饼图只有几个像素的周长,但以 X% 旋转,在本例中为 89%。需要一点数学才能算出 89% 的半圆在哪里。根据上面的图片,这不会给你一个很好的圆形标记。它会给你一个小的彩色部分,那个圆形标记应该是,在一些 css 的帮助下,第二个饼图部分可以四舍五入,看起来像你想要的。

第二个饼图可能看起来像这样......

var ctx2 = document.getElementById("dashboardChart2");
    var dashboardChart2 = new Chart(ctx2, {
        type: 'doughnut',
        data: {
            labels: ["Purple"],
            datasets: [{
                label: '# of Votes',
                data: [5],
                backgroundColor: [
                   'rgba(159, 90, 253, 1)'
                ],
                borderColor: [
                    'rgba(255, 255, 255 ,1)',
                ],
                borderWidth: 2
            }]

        },
        options: {
            rotation: 1 * Math.PI,/** This is where you need to work out where 89% is */
            circumference: 1 * Math.PI,/** put in a much smaller amount  so it does not take up an entire semi circle */
            legend: {
                display: false
            },
            tooltip: {
                enabled: false
            },
            cutoutPercentage: 95
        }
    });

至于大 89% 可能会涉及 css。将文本直接定位在饼图的“前面”(绝对涉及 z-index 和位置等内容)

新答案:

也许您可以直接在现有的圆环图之上创建一个圆环图,并让该圆环图的第一个和第三个“条”不透明度为 0,因此它们不能被看到。如果第一个柱的值为 88.5,第二个柱的值为 1,第三个柱的值为 10.5,那么您将有效地将第二个柱置于 89%,宽度为 1% (88.5 + 1 + 10.5 = 100)。

datasets: [{
   data: [88.5, 1,10.5],// how much space each bar should take 
   backgroundColor: [
      "rgba(0,0,0,0)", // bar 1: opacity 0
      "rgba(255,255,255,1)", // bar 2 is white
      "rgba(0,0,0,0)", // bar 3: opacity 0
   ],
   borderColor: [
      'rgba(0, 0, 0 ,0)',// bar 1 border opacity 0
      'rgba(46, 204, 113, 1)',// bar 2 border is green
      'rgba(0, 0, 0 ,0)'// bar 3 border opacity 0
   ],
   borderWidth: 3
}]

至于大 89% 可能会涉及 css。将文本直接定位在饼图的“前面”

.percent {
 position: absolute;
    left: 50%;
    transform: translate(-50%, 0);
    font-size: 80px;
    bottom: 0;
}

在尝试了一把小提琴之后,我有了这个......

此处示例:

https://jsfiddle.net/rjtsbeLc/3/

请注意,通过相对定位和绝对定位,我将第二个圆环图放在现有圆环图的顶部,并将百分比文本放在它们顶部的中间底部。

.outer {
  position: relative;
  width: 600px;
  height: 400px;
}
canvas {
  position: absolute;
}

这不是您要寻找的,因为“圆”是矩形的,但我遇到了这个,它可能会帮助您弄清楚如何将矩形四舍五入成圆形......

Chart.js 圆角甜甜圈https://stackoverflow.com/a/36964890/5178301

【讨论】:

  • 我不确定我是否完全遵循这一点。是否可以将其放入 sn-p 中进行预览?
  • @Nick 我已添加到我的原始答案跃点中,这有帮助:)
【解决方案2】:

为了让您开始,我能够使用以下选项配置获得半个甜甜圈,或者我称之为速度计图表:

var chart = new Chart(canvas, {
  type: 'doughnut',
  data: ['400', '200'],
  options: {
    rotation: -90,
    circumference: 180,
}

chartjs 版本 3.5.1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    相关资源
    最近更新 更多