[英]Plot txt values (Flot charts)
我对此问题有些困惑。
我有一个文件,其中要绘制的元素以列分隔,如下例所示:
ID Number Hour Name Mood Seconds Hour2
1 29.1 11:32:37 Tim Good 0954 11:32:34 PM
2 31.9 11:33:19 Tim Fine 1251 11:33:15 PM
3 32.0 11:54:16 Tim Excellent 1897 11:54:12 PM
4 36.6 12:00:43 Time Good 0997 12:00:37 PM
我想要的是能够从.txt文件中提取所有这些数据并将其存储在变量中,以便以更简单的方式(不包括第一行)使用它们。 然后,我想做的是绘制例如Seconds列与Hour 1的关系图,或者Mood列与Hour 1的关系图(由于情绪是一个字符串,因此可能更困难), FLOT Charts,但是我做不到,因为我不知道执行该命令的正确命令,并且我已经在互联网上寻找它们,但是我什么都没找到。 我确实尝试过从Stackoverflow帖子中修改文件,以查看它是否有效,但是页面只是空白。 所使用的代码是以下代码(有head标记,但是当我在此处键入内容时,该帖子变得很疯狂,当然.js文件与该代码位于同一文件夹中,datafile.txt文件也位于该文件夹中):
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- 1. Add JQuery and Highcharts in the head of your page -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<!-- 2. You can add print and export feature by adding this line -->
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<!-- 3. Add the JavaScript with the Highchart options to initialize the chart -->
<script type="text/javascript">
jQuery(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'column'
},
title: {
text: 'Tiny Tool Monthly Sales'
},
subtitle: {
text: '2014 Q1-Q2'
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Sales'
}
},
series: []
};
// JQuery function to process the csv data
$.get('datafile.txt', function(data) {
$.each(data, function(lineNo, line) {
var items = line.split(' ');
// header line contains names of categories
if (lineNo == 0) {
$.each(items, function(itemNo, item) {
//skip first item of first line
if (itemNo > 0) options.xAxis.categories.push(item);
});
}
// the rest of the lines contain data with their name in the first position
else {
var series = {
data: []
};
$.each(items, function(itemNo, item) {
series.data.push(parseFloat(item));
});
options.series.push(series);
}
});
//putting all together and create the chart
var chart = new Highcharts.Chart(options);
});
});
</script>
</head>
<body>
<!-- 3. Add the container -->
<div id="container" style="width: 600px; height: 400px; margin: 0 auto"></div>
</body>
您是否知道哪里出了问题或如何解决? 非常感谢大家!
$.get()
方法返回一个字符串,并将$.each()
方法与一个字符串一起使用$.each()
字符串中的每个字符而不是每一行调用回调函数。 将对$.each()
的调用更改为此:
$.each(data.split('\n'), function(lineNo, line) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.