Chartjs - Stacked bar chart blocking other values

最后都变了- 提交于 2021-01-29 05:29:10

问题


My problem is that chart js is rendering the bar chart based on the order defined on the dataset. As you can see below, the values at index 0 of the arrays are 2500 and 1000. Since the 2500 was passed first, this will block the bar chart of the 1000 value.

https://jsfiddle.net/6bjy9nxh/352/

var barData = {
labels: ['Italy', 'UK', 'USA', 'Germany', 'France', 'Japan'],
datasets: [
    {
        label: '2010 customers #',
        fillColor: '#382765',
        data: [2500, 1902, 1041, 610, 1245, 952]
    },
    {
        label: '2014 customers #',
        fillColor: '#7BC225',
        data: [1000, 1689, 1318, 589, 1199, 1436]
    }
]

回答1:


As Per the example of Chartjs for Stacked bar chart stacked: true for both xAxes and Axes

var barData = {
    labels : ['Italy', 'UK', 'USA', 'Germany', 'France', 'Japan'],
    datasets : [{
        label : '2010 customers #',
        backgroundColor : '#382765',
        data : [2500, 1902, 1041, 610, 1245, 952]
    }, {
        label : '2014 customers #',
        backgroundColor : '#7BC225',
        data : [1000, 1689, 1318, 589, 1199, 1436]
    }]
};

var context = document.getElementById('clients').getContext('2d');
var clientsChart = new Chart(context, {
    type : 'bar',
    data : barData,
    options : {
        scales : {
            xAxes : [{
                stacked : true,

            }],
            yAxes : [{
                stacked : true
            }]
        }
    }
});

This may help you. Fiddle



来源:https://stackoverflow.com/questions/37535964/chartjs-stacked-bar-chart-blocking-other-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!