我正在尝试使用文本文件中的外部数据在Google图表中创建动态折线图。我能够获得一个单一系列的图表,但是没有其他系列的成功。我在尝试什么:
X轴:时间(不是必需的数据点,可以根据需要删除) Y轴:客户与价值
更新:在玩完代码之后,下面的屏幕截图是我的住处。我已经更新了以下代码和源文本/ csv数据,以在屏幕截图中反映出结果:
我的文本文件示例:
12:00 1 -63
12:00 2 -57
1:00 1 -65
1:00 2 -51
2:00 1 -53
2:00 2 -47
3:00 1 -63
3:00 2 -57
4:00 1 -65
4:00 2 -51
5:00 1 -53
5:00 2 -47
HTML / PHP:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<script src="https://www.google.com/jsapi"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart () {
function datarefresh () {
$.ajax({
url: '/hsd/tmp/24SignalStrength.csv',
type: 'get',
success: function (txt) {
var dataArray = [['Time','Client','Value']];
var txtArray = txt.split('\n');
for (var i = 0; i < txtArray.length; i++) {
var tmpData = txtArray[i].split(/\s+/);
dataArray.push([(tmpData[0]), parseInt(tmpData[1]), parseInt(tmpData[2])]);
}
var data = google.visualization.arrayToDataTable(dataArray);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
var options = {
'title':'2.4GHz Connected Clients Signal Strength',
'width':800,'height':600,
'curveType':'function',
// legend: 'none'
};
chart.draw(data,options);
}
});
}
setInterval(datarefresh, 1000);
}
google.load('visualization', '2', {packages:['corechart'], callback: drawChart});
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>