简体   繁体   English

浮选条形图数据转换

[英]Flot Bar Chart Data Conversion

I am trying to make a simple bar chart but my data is not appearing 我正在尝试制作一个简单的条形图,但是我的数据没有出现

var data = [["item1",277],["item2",635],["item3",133]] var data = [[“” item1“,277],[” item2“,635],[” item3“,133]]

Can you please show me how to convert the string above to the correct format for Flot? 您能告诉我如何将上面的字符串转换为Flot的正确格式吗?

Please see fiddle http://jsfiddle.net/q7R68/ 请参阅小提琴http://jsfiddle.net/q7R68/

var data = [["item1",277],["item2",635],["item3",133]] 

var options = {
                    series: {
                        stack: 0,
                        lines: { show: false, steps: false },
                        bars: { show: true, barWidth: 0.9, align: 'center', },
                    }
                };

alert(data);
$.plot($("#statsChart2"), data, options);

Thanks 谢谢

EDIT: I should have been more clear in my question that I would like to convert the data from its string representation to what is needed by Flot through code. 编辑:我应该在我的问题中更清楚地表明,我想通过代码将数据从其字符串表示形式转换为Flot所需的形式。 It represents a JSON array that has been converted to string format to be graphed 它表示一个JSON数组,该数组已转换为要绘制图形的字符串格式

Your configuration is all confused. 您的配置非常混乱。 The bars and lines config belongs in the data while your axis labels ("item1", etc...) belong in your options. barslines配置属于数据,而轴标签(“ item1”等)属于选项。

Finally, I don't see a stack option in the API 最后,我在API中看不到stack选项

var data = [[0,277],[1,635],[2,133]];

var aSeries = {
    data: data,
    bars: { show: true, barWidth: 0.9, align: 'center' }
};

var someOptions = {
    xaxis: {
        ticks: [[0,"item1"],[1,"item2"],[2,"item3"]]   
     }       
};

$.plot($("#statsChart2"), [aSeries], someOptions);

Fiddle here . 在这里摆弄。

EDITS 编辑

I guess I'm feeling generous today here's a quick snippet of code to do the conversion: 我想今天我很慷慨,这里有一段简短的代码可以进行转换:

var data = [["item1",277],["item2",635],["item3",133]];

var ticks = [];
for (var i = 0; i < data.length; i++) {
    ticks.push([i,data[i][0]]);
    data[i][0] = i;   
}

Updated, updated fiddle . 更新了,更新了小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM