是否可以将日期设置为谷歌折线图的最小/最大值?
Here is some sample code证明了这个问题:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]
]);
var options = {
title: 'Company Performance',
hAxis: {viewWindow: {min: new Date(2005,1,1)}}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<div id="chart_div"></div>
也许问题出现了:
hAxis: {viewWindow: {min: new Date(2005,1,1)}}
答案 0 :(得分:3)
您的x轴数据不是Date对象,这就是为什么将hAxis.viewWindow.min
选项设置为'new Date(2005,1,1)`不起作用的原因。如果您将域列切换为“日期”类型,它将起作用:
var data = new google.visualization.DataTable();
data.addColumn('date', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows([
[new Date(2004, 0, 1), 1000, 400],
[new Date(2005, 0, 1), 1170, 460],
[new Date(2006, 0, 1), 660, 1120],
[new Date(2007, 0, 1), 1030, 540]
]);
如果您的数据只是几年(并且与一年中的任何特定日期无关),那么最好使用“数字”类型列:
var data = new google.visualization.DataTable();
data.addColumn('number', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows([
[2004, 1000, 400],
[2005, 1170, 460],
[2006, 660, 1120],
[2007, 1030, 540]
]);
并将hAxis.viewWindow.min
选项设置为数字:
hAxis: {viewWindow: {min: 2005}}