Chart.js - 绘制带有多个标签的条形图

时间:2017-10-06 14:54:13

标签: javascript charts chart.js papaparse

我正在尝试使用chart.js绘制条形图,该图表使用多个标签,以便我可以操纵它们。我已经设法用折线图做了,并尝试扩展我在那里做的没有运气。我收到了这个错误。什么可能导致它?

  

“Uncaught TypeError:在非对象上调用Object.defineProperty”。

问题出在数据中的某个地方:{}因为当我将数据作为一个简单的2索引数组时,它工作正常。

编辑:单步执行dataset = null;

        var com_passed = rows[rows.length-2]["# Common Passed"];
        var com_failed = rows[rows.length-2]["# Common Failed"];
        var ctx = document.getElementById("common_chart");
        ctx.height = 50;

        var historicalChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: ["Passed", "Failed"],
                datasets: [
                { 
                    data: com_passed,
                    label: "Passed",
                    fillColor: "red"
                },
                { 
                    data: com_failed,
                    label: "Failed",
                    fillColor: "green"                      
                }
                    ]
            },
                options: {
                    scales: {
                        yAxes: [{
                            ticks: {
                            beginAtZero:true,
                            responsive: false,
                            maintainAspectRatio: true
                            }
                        }]
                    }
                }
        });

1 个答案:

答案 0 :(得分:6)

对于那些有类似问题的人。我需要将数据com_passed和com_failed作为数组而不是像这样的整数。

    var com_passed = rows[rows.length-2]["# Common Passed"];
    var com_failed = rows[rows.length-2]["# Common Failed"];
    var ctx = document.getElementById("common_chart");
    ctx.height = 50;

    var historicalChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ["Passed", "Failed"],
            datasets: [
            { 
                data: [com_passed],
                label: "Passed",
                fillColor: "red"
            },
            { 
                data: [com_failed],
                label: "Failed",
                fillColor: "green"                      
            }
                ]
        },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                        beginAtZero:true,
                        responsive: false,
                        maintainAspectRatio: true
                        }
                    }]
                }
            }
    });