我正在试验Google图表,但我遇到了一个问题。我非常感谢你的任何帮助。
我有2个谷歌图表,折线图和时间表,如下所示:https://jsfiddle.net/bygo33n8/7/
代码如下:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
<div id="timeline" style="height: 180px;"></div>
google.load('visualization', '1', {packages: ['corechart', 'line', 'timeline']});
google.setOnLoadCallback(drawBasic);
google.setOnLoadCallback(drawChart);
function drawBasic() {
var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'date', id: 'Start' });
dataTable.addColumn({ type: 'number', id: 'X' });
dataTable.addColumn({ type: 'number', id: 'Y' });
dataTable.addRows([
[new Date(0,0,0,00,0,0), 0, 0],
[new Date(0,0,0,01,0,0), 148, 177],
[new Date(0,0,0,02,0,0), 214, 270],
[new Date(0,0,0,03,0,0), 0, 0]
]);
var options = {
hAxis: {
title: 'Time'
// textPosition: 'none'
},
vAxis: {
title: 'Traffic'
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(dataTable, options);
}
function drawChart() {
var container = document.getElementById('timeline');
var chart = new google.visualization.Timeline(container);
var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'string', id: 'President' });
dataTable.addColumn({ type: 'date', id: 'Start' });
dataTable.addColumn({ type: 'date', id: 'End' });
//specify only the time active
dataTable.addRows([
[ 'Activity', new Date(0,0,0,00,0,0), new Date(0,0,0,00,20,0) ],
[ 'Activity', new Date(0,0,0,01,0,0), new Date(0,0,0,02,00,0) ],
[ 'Activity', new Date(0,0,0,02,00,0), new Date(0,0,0,03,00,0) ],
]);
var options = {
timeline: { showRowLabels: false },
colors: ['green'],
avoidOverlappingGridLines: true,
//backgroundColor: '#FF0000'
};
chart.draw(dataTable, options);
}
我希望折线图的水平轴与时间轴的水平轴相匹配。虽然我使用相同的数据类型并以相同的方式写入数据,但折线图的水平轴输出随机日期。
答案 0 :(得分:0)
日期不是随机的。使用第一列的timeofday
- 类型来获得所需的结果:
dataTable.addColumn({ type: 'timeofday', id: 'Start' });
dataTable.addColumn({ type: 'number', id: 'X' });
dataTable.addColumn({ type: 'number', id: 'Y' });
dataTable.addRows([
[[0,0,0], 0, 0],
[[1,0,0], 148, 177],
[[2,0,0], 214, 270],
[[3,0,0], 0, 0]
]);