【问题标题】:Chartjs Stacked bar chart not displaying correctlyChartjs 堆积条形图显示不正确
【发布时间】:2017-12-22 21:54:07
【问题描述】:

我正在尝试使用 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/

【问题讨论】:

    标签: chart.js


    【解决方案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"
    }];
    

    【讨论】:

    • 我认为你是对的,但是当你从数据库中获取值时这很烦人,在这种情况下你必须为没有数据生成假值。我以为它会自动处理
    猜你喜欢
    • 1970-01-01
    • 2021-03-19
    • 2017-03-06
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多