Chart.js - Draw bar chart with multiple labels
Asked Answered
I

1

8

I am trying to draw a bar chart with chart.js that uses multiple labels so that I can manipulate them. I have managed to do it with a line chart and tried extending what I did there with no luck. I am getting this error. what could be causing it?

"Uncaught TypeError: Object.defineProperty called on non-object".

The issue is somewhere in the data: {} because when I had data as a simple 2 index array it was working fine.

edit: when stepping through 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
                            }
                        }]
                    }
                }
        });
Instep answered 6/10, 2017 at 14:54 Comment(7)
Did you try stepping through your code? Then you might be able to narrow it down closer than "somewhere in the data".Biggers
I just stepped through the code and it's showing that dataset is null. I'm having a tough time figuring out why.Instep
That is the sort of detail that ought to be in the body of the question.Biggers
console.log(com_passed, com_failed) ??Humbuggery
I will edit it @jdv console.log(com_passed, com_failed)Instep
@GRUNT is not defined because this is within a callback function but the actual numbers are right when stepping throughInstep
I figured out the error! it was treating com_passed and failed as integers rather than arrays. First time using js and the abstraction seems to have bit me. Thanks for the help!Instep
I
9

for those having a similar issue. I needed to make the data com_passed and com_failed as an array instead of an integer like so.

    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
                        }
                    }]
                }
            }
    });
Instep answered 6/10, 2017 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.