如何在Google Chart上设置最大值和最小值?
我试过这个没有成功:
vAxis: {
viewWindowMode:'explicit',
viewWindow: {
max:3000,
min:500
}
}
这是我正在使用的所有代码:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1.1", {packages:["bar"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses', 'Profit'],
['2014', 1000, 400, 200],
['2015', 1170, 460, 250],
['2016', 660, 1120, 300],
['2017', 1030, 540, 350]
]);
var options = {
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017',
},
vAxis: {
viewWindowMode:'explicit',
viewWindow: {
max:3000,
min:500
}
},
bars: 'vertical' // Required for Material Bar Charts.
};
var chart = new google.charts.Bar(document.getElementById('barchart_material'));
chart.draw(data, options);
}
</script>
(来自https://developers.google.com/chart/interactive/docs/gallery/barchart的例子)
事先说。
答案 0 :(得分:28)
随着Material Charts的发布,Google正在修改指定选项的方式。这些选项的结构尚未最终确定,因此Google为新版本提供了经典选项结构的转换器功能,建议您使用它而不是使用可能在将来更改的选项。
因此,您可以通过以下两种方式之一解决问题:
google.load("visualization", "1.0", {packages:["bar"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses', 'Profit'],
['2014', 1000, 400, 200],
['2015', 1170, 460, 250],
['2016', 660, 1120, 300],
['2017', 1030, 540, 350]
]);
var options = {
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017',
},
vAxis: {
viewWindowMode:'explicit',
viewWindow: {
max:3000,
min:500
}
},
bars: 'vertical', // Required for Material Bar Charts.
width: 800,
height: 600
};
var chart = new google.charts.Bar(document.getElementById('barchart_material'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
<div id="barchart_material"></div>
google.load("visualization", "1.0", {
packages: ["bar"]
});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses', 'Profit'],
['2014', 1000, 400, 200],
['2015', 1170, 460, 250],
['2016', 660, 1120, 300],
['2017', 1030, 540, 350]
]);
var options = {
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017',
},
axes: {
y: {
all: {
range: {
max: 3000,
min: 500
}
}
}
},
bars: 'vertical', // Required for Material Bar Charts.
width: 800,
height: 600
};
var chart = new google.charts.Bar(document.getElementById('barchart_material'));
chart.draw(data, options);
}
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
<div id="barchart_material"></div>
来源:我在Google Charts上工作。