2

I have a Chart.js with values from a Javascript array which looks exactly like that:

var obj = JSON.parse('{"0":"8.4113","2":"9.5231","3":"9.0655","4":"7.8400"}');

And here I give the "obj" array to the chartjs which works fine and fills out the bars:

var barChartData = {
        labels : obj1,

        datasets : [
            {
                fillColor: "rgba(151,187,205,0.5)",
            strokeColor: "rgba(151,187,205,0.8)",
            highlightFill: "rgba(151,187,205,0.75)",
            highlightStroke: "rgba(151,187,205,1)",
                data : obj

            }
        ]

    }
    window.onload = function(){
        var ctx = document.getElementById("canvas").getContext("2d");
        window.myBar = new Chart(ctx).Bar(barChartData, {
            responsive : true
        });
    }

    </script>

But as you can see there is a second array called "obj1" which stores the names for the labels. This one looks exactly like that:

 var obj1 = JSON.parse('{"0":"name1","2":"name2","3":"name3","4":"name4"}');

This second array does not fill out the labels like the other array, the labels are still empty. I have no idea why it does not work like the data:"".

1 Answer 1

1

This seems to work:

function convertToArray(obj) {
    return Object.keys(obj).map(function(key) {
        return obj[key];
    });
}

var obj = JSON.parse('{"0":"8.4113","2":"9.5231","3":"9.0655","4":"7.8400"}');
var obj1 = JSON.parse('{"0":"name1","2":"name2","3":"name3","4":"name4"}');

obj = convertToArray(obj);
obj1 = convertToArray(obj1);

var barChartData = {
    labels : obj1,
    datasets : [
        {
            fillColor: "rgba(151,187,205,0.5)",
            strokeColor: "rgba(151,187,205,0.8)",
            highlightFill: "rgba(151,187,205,0.75)",
            highlightStroke: "rgba(151,187,205,1)",
            data : obj

        }
    ]
};
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartData, {
    responsive : true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script>
<canvas id="canvas"></canvas>

Object.keys(obj) returns an array of object's properties and then we use the Array#map() function to convert these keys to actual values. The Array#map() then returns a new array containing just the values.

Sign up to request clarification or add additional context in comments.

6 Comments

I might should say, that the arrays are dynamic so I do not want to add Data manually. Obj and Obj1 are from a Database...
I think I need to get obj1 without the key just the names in an array like you said: ['name1', 'name2', 'name3', 'name4'] but didn't figure out how to do that.
I understand, use the code I added in my answer below to convert your data model to an array.
I tried that now it displays the chart but every lable is called "NaN" instead of "name1","name2" and so on...
ok i upated the answer again, the culprit was parseFloat() in convert() function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.