wingskush
wingskush

Reputation: 554

Write a custom array out of json object for JQplot

I m trying to make a bar chart graph using Jqplot. on my json request the data being sent is

[{"count":"3","value":"Value A", "data": "data 1"},{"count":"1","value":"Value B", , "data": "data 2"}]

below is the script

var url = 'someurl';
    $.getJSON(url, function(json) {
        var count = new Array();
        var count2 = [2,3];
        var ticks= new Array();
        var myarray = [];
        $.each(json, function(key, val) {
            count.push(val.count);
            ticks.push(val.data);
            var item = {label: val.value};
            myarray.push(item);
        }); 

        myJSON = JSON.stringify(myarray);

        var plot1 = $.jqplot('barchart', [count, count2], {
            seriesDefaults: {
                renderer: $.jqplot.BarRenderer,
                rendererOptions: {fillToZero: true},
                lineWidth: 2.5
            },
            title: 'Some title',
            series: [{label: "label A"}, {label: "label B"}],
            legend: {
                show: true,
                placement: 'outsideGrid'
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: ticks
                },
                yaxis: {
                    pad: 1.05,
                    tickOptions: {formatString: '%d Values'}
                }
            }
        });
    });

So my question is that, for "series" in the plot i want to display the data returned by "value" object of the josn encoded data. ie. i want an array as [{label: "Value A"}, {label : "Value B"}] Note that there should not be " " for label. In the above code i tried to stringify the myarray and it returns

[{"label":"Value A"},{"label":"Value B"}]

how do i change "label" to just label ???

Upvotes: 1

Views: 601

Answers (1)

Teo
Teo

Reputation: 614

Try parsing the string returning a list of objects:

var listObj = JSON.parse('[{"label":"Value A"},{"label":"Value B"}]');

Upvotes: 1

Related Questions