为JQplot写一个json对象的自定义数组

时间:2014-09-18 10:24:51

标签: jquery arrays json object jqplot

我正在尝试使用Jqplot制作条形图。在我的json请求中,正在发送的数据是

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

下面是脚本

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'}
                }
            }
        });
    });

所以我的问题是,对于&#34;系列&#34;在图中我想显示&#34;值&#34;返回的数据。 josn编码数据的对象。 即。 我希望数组为[{label: "Value A"}, {label : "Value B"}] 请注意,不应该有&#34; &#34;用于标签。 在上面的代码中,我试图将myarray字符串化并返回<​​/ p>

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

我如何改变&#34;标签&#34;只是标签???

1 个答案:

答案 0 :(得分:1)

尝试解析返回对象列表的字符串:

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