首页 > 解决方案 > 如何将 3 种颜色设置为同一个 chartjs 栏?

问题描述

我的想法很简单:我需要一个 chartjs,而只需要一种颜色,我需要在同一个栏上绘制不同的颜色,例如:

示例不同的颜色

标签: javascriptgraphicschart.js

解决方案


我不确定您是否可以将三种颜色应用于同一个“条”(这是一个相对术语)。但我知道你有能力像这里的这个例子一样创建堆叠图表:

https://www.chartjs.org/samples/latest/charts/bar/stacked.html

关键是在设置图表选项时将stacked 属性作为true 传递。例子:

...
options: {
    title: {
        display: true,
        text: 'Chart.js Bar Chart - Stacked'
    },
    tooltips: {
        mode: 'index',
        intersect: false
    },
    responsive: true,
    scales: {
        xAxes: [{
            // MAGIC HERE
            stacked: true,
        }],
        yAxes: [{
            // AND HERE
            stacked: true
        }]
    }
}

我希望这可以帮助你。


推荐阅读