Chartjs堆积条形图无法正确显示

时间:2017-12-22 10:12:24

标签: chart.js

我正在尝试使用chartjs创建堆积条形图。我有时间序列与时间的差异,这意味着一些系列可以有一段时间的价值,但其他系列没有。出于这个原因,我选择直接包含数据集中的x值而不是标签数组,但图表无法正确呈现。

这是我的代码:

var config = {
        type:'bar',
        data:{
            datasets:datasets
        },
        options: {
            responsive: true,
            title:{
                display:true,
                text:"MyChart"
            },
            tooltips: {
                mode: 'index'
            },
            hover: {
                mode: 'index'
            },
            legend: {
                    position: 'bottom'
            },
            elements: { point: { radius: 0 }},
            scales: {
                xAxes: [{
                    stacked: true,
                    type: 'time',
                    time: {
                        unit: 'minutes',
                        unitStepSize: 5,
                        displayFormats:{
                            minutes:'HH:mm'
                        }

                    },
                    scaleLabel: {
                        display: true,
                        labelString: 'Date'
                    }
                }],
                yAxes: [{
                    stacked:true,
                    scaleLabel: {
                        display: true,
                        labelString: 'Resources Consumed'
                    }
                }]
            }
        }
    };

    var ctx = document.getElementById('session-sql-activity').getContext("2d");
    ctx.canvas.height = 300;
    ctx.canvas.width = 800;
    new Chart(ctx,config);

我认为问题在于x轴堆叠。

这是一个说明问题的JSFiddle。

https://jsfiddle.net/1kLyyjfp/

1 个答案:

答案 0 :(得分:1)

我认为解决方案是指定y轴的值。

{
    "y": 0,
    "x": "2017-12-22 08:59"
}

结果数据集是:

[{
    "data": [{
        "y": 2,
            "x": "2017-12-22 08:59"
    }, {
        "y": 18,
            "x": "2017-12-22 09:00"
    }],
    "backgroundColor": "#32AA09",
    "label": "7726bj0dhtnmt"
    }, {
    "data": [{
        "y": 0,
            "x": "2017-12-22 08:59"
    }, {
        "y": 2,
            "x": "2017-12-22 09:00"
    }],
    "backgroundColor": "#CF5A26",
    "label": "0ks4u5nywjbdj"
    }, {
    "data": [{
        "y": 0,
            "x": "2017-12-22 08:59"
    }, {
        "y": 4,
            "x": "2017-12-22 09:00"
    }],
    "backgroundColor": "#DA43AA",
    "label": "1mnbsygj75jvx"
    }, {
    "data": [{
        "y": 0,
            "x": "2017-12-22 08:59"
    }, {
        "y": 2,
            "x": "2017-12-22 09:00"
    }],
    "backgroundColor": "#5200A0",
    "label": "39ymdt11ybwhz"
    }, {
    "data": [{
        "y": 0,
            "x": "2017-12-22 08:59"
    }, {
        "y": 2,
            "x": "2017-12-22 09:00"
    }],
    "backgroundColor": "#925E44",
    "label": "4v1whmzv76j2z"
    }, {
    "data": [{
        "y": 0,
            "x": "2017-12-22 08:59"
    }, {
        "y": 2,
            "x": "2017-12-22 09:00"
    }],
    "backgroundColor": "#477BD4",
    "label": "5sfyujuhwbj9n"
    }, {
    "data": [{
        "y": 0,
            "x": "2017-12-22 08:59"
    }, {
        "y": 4,
            "x": "2017-12-22 09:00"
    }],
    "backgroundColor": "#272755",
    "label": "6x0zvf2mw8t6g"
    }, {
    "data": [{
        "y": 0,
            "x": "2017-12-22 08:59"
    }, {
        "y": 2,
            "x": "2017-12-22 09:00"
    }],
    "backgroundColor": "#F581E0",
    "label": "7kpk62m5n8j9q"
    }, {
    "data": [{
        "y": 0,
            "x": "2017-12-22 08:59"
    }, {
        "y": 20,
            "x": "2017-12-22 09:00"
    }],
    "backgroundColor": "#4128A8",
    "label": "7mwz4m103nn1k"
}];