[英]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.