【发布时间】:2016-01-26 06:46:00
【问题描述】:
我是 highchart 的新手。我已通过此帮助门户网站,但无法满足我的要求,因此需要您的帮助/指导来完成此任务。
我的任务是按照以下格式从包含 TPS 详细信息的 csv/TXT 文件中读取数据,并将其显示在动态运行图表上(如果图表会在一分钟内刷新就可以了)。 数据格式: 16:08:02,3 16:08:04,5 16:08:05,1 16:09:01,10
上面的文件每秒都在追加,将从文件中读取最后一分钟的数据并将其绘制在图表上。
我已经使用下面的代码进行了尝试。不知道我错过了什么。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TPS Example</title>
<script type="text/javascript" src="C:/Backup/SUNIL/Software/library/jquery-2.2.0.js"></script>
<style type="text/css">
${demo.css}
</style>
<script type="text/javascript">
$(function () {
$(document).ready(function () {
Highcharts.setOptions({
global: {
useUTC: false
}
});
$('#container').highcharts({
chart: {
renderTo: 'container',
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
title: {
text: 'TPS Data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 3,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
}())
}]
});
});
});
</script>
</head>
<body>
<script src="C:\Backup\SUNIL\Software\library\Highcharts-4.2.1\js\highcharts.js"></script>
<script src="C:\Backup\SUNIL\Software\library\Highcharts-4.2.1\js\highcharts.js"></script>
<div id="container" style="min-width: 50px; height: 200px; margin: 0 auto"></div>
</body>
</html>
【问题讨论】:
-
您不应该加载highcharts两次,请确保您的路径是否正确并将脚本移动到头部。
-
HI 测试了一个工作正常的样例,即使我也删除了重复的行。它不起作用,我也添加了下面的代码。 $.get('data.csv', function(data) { var lines = data.split('\n'); $.each(lines, function(lineNo, line) { var items = line.split(', '); c.push(items[0]); d.push(parseInt(items[1])); }); });
-
要使用 ajax,您需要使用 webserver,因为浏览器会阻止从本地系统加载文件。
标签: javascript jquery css csv highcharts