【问题标题】:ChartJs Stacked Bar ChartChartJs 堆积条形图
【发布时间】:2019-11-14 22:55:18
【问题描述】:

我有不同月份的数据,如下所示

一月:[1,5,3]

二月:[10,5]

三月:[4,8]

四月:[7]

五月:[3,1,5,0,7]

我想生成如下条形图

现在,我有以下代码,我想知道如何生成如上图所示的条形图。

new Chart(document.getElementById("bar-chart"), {
type: 'bar',
data: {
  labels: ['Jan','Feb','Mar','Apr','May'],
  datasets: [{
      data: [1,5,3],
      label: 'Jan',
      backgroundColor: "#3e95cd",
    }
            , {
      data: [10,5],
      label: 'Feb',
      backgroundColor: "#8e5ea2",
    }, {
      data: [4,8],
      label: 'Mar',
      backgroundColor: "#4287f5",
    }
            , {
      data: [7],
      label: 'Apr',
      backgroundColor: "#23ebbc",
    }
            , {
      data: [3,1,5,0,7],
      label: 'May',
      backgroundColor: "#e63057",
    }
  ]
},
options: {
  title: {
    display: true,
    text: 'This is title'
  },
  backgroundColor:'#cfcfcf',
        scales: {
      xAxes: [{ stacked: true }],
      yAxes: [{ stacked: true }]
    }
}

});

谢谢

【问题讨论】:

    标签: javascript charts chart.js bar-chart stacked-chart


    【解决方案1】:

    第一个数据集是红色的[10,10,8,7,3],第二个是[5,5,8,0,1],第三个是[3,0,0,0,5],第四个是[0,0,0,0,7]

    当然,您必须使用自己的技能来转换 Web 服务发送的数据。但这是您要寻找的最终结果。

    【讨论】:

    • 在我的堆积条形图中,我每个月都有不同的开支,如食物、饮料、文具、娱乐、旅行等。是否可以在上面堆叠的条形图中显示每个费用的标签?例如:一月:食物:1,固定:5,饮料:1 |二月:食物:10,其他:5 |三月:饮料:4,娱乐:5 |
    • 选择满足您需求的示例之一chartjs.org/samples/latest。标签也由您控制,您必须使用数据构建自己的标签。
    【解决方案2】:

    我只使用过一次 ChartJS,但根据您想要的判断,我假设您创建了一个条形图表并单独添加每个数据集。例如, datasets:[{data:yourRedData}] 然后将其附加到图表中。

    就好像它们确实有您想要的示例一样,https://www.chartjs.org/samples/latest/charts/bar/stacked.html

    我检查了示例的源代码,以更好地了解它们是如何实现结果的,这就是我的发现。

    var barChartData = {
            labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
            datasets: [{
                label: 'Dataset 1',
                backgroundColor: window.chartColors.red,
                data: [
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor()
                ]
            }, {
                label: 'Dataset 2',
                backgroundColor: window.chartColors.blue,
                data: [
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor()
                ]
            }, {
                label: 'Dataset 3',
                backgroundColor: window.chartColors.green,
                data: [
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor()
                ]
            }]
    
        };
        window.onload = function() {
            var ctx = document.getElementById('canvas').getContext('2d');
            window.myBar = new Chart(ctx, {
                type: 'bar',
                data: barChartData,
                options: {
                    title: {
                        display: true,
                        text: 'Chart.js Bar Chart - Stacked'
                    },
                    tooltips: {
                        mode: 'index',
                        intersect: false
                    },
                    responsive: true,
                    scales: {
                        xAxes: [{
                            stacked: true,
                        }],
                        yAxes: [{
                            stacked: true
                        }]
                    }
                }
            });
        };
    

    【讨论】:

    • 感谢您的回答。但在我的情况下,数据集的数量应该等于柱的数量。我想将每个数据集显示为条形。谢谢
    • @SandunHarshana 您正在准备 data 错误。数据的每个索引都是一个月的数据。在您的情况下,您的数据应包含 5 个元素,每个元素。
    • @Thanh 实际上是来自网络服务的数据。我们每个月都有一些费用,每个月的费用数量不同。我将使用堆叠条形图显示每个月的费用(并希望每个条形图以不同颜色显示一个月的每项费用)。是否可以像我在问题中提到的那样在堆积条形图中显示这些数据? ,谢谢。
    • 看到上面文档的例子,我会说是的。现在您必须将数据转换为可接受的格式。从您发布的示例图像中,您需要准备 4 个数据集(因为 4 种不同的颜色),为期 5 个月(数组大小data
    猜你喜欢
    • 2017-04-30
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2014-02-09
    • 2019-04-02
    • 2020-09-14
    相关资源
    最近更新 更多