从简单的折线图到复杂的分层树图,Google 图表库提供了大量即用型图表类型:
1. 在要显示图表的 HTML 中添加一个 <div> 元素(具有唯一的 id):
<div id="myChart" style="max-width:700px; height:400px"></div>
2. 添加图表加载器的链接:
<script src="https://www.gstatic.com/charts/loader.js"></script>
3. 加载Graph API,并添加加载API时运行的函数:
<script>
google.charts.load('current',{packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Your Function
function drawChart() {
...
}
</script>
function drawChart() {
// Set Data
const data = google.visualization.arrayToDataTable([
['Contry', 'Mhl'],
['Italy', 55],
['France', 49],
['Spain', 44],
['USA', 24],
['Argentina', 15]
]);
// Set Options
const options = {
title: 'World Wide Wine Production'
};
// Draw
const chart = new google.visualization.BarChart(document.getElementById('myChart'));
chart.draw(data, options);
}
要转换一个酒吧图表到一个馅饼图表,只需替换:
谷歌.可视化。条形图
和:
谷歌.可视化。饼形图
const chart = new google.visualization.
PieChart(document.getElementById('myChart'));
要以 3D 方式显示饼图,只需添加is3D:真实到选项:
const options = {
title: 'World Wide Wine Production',
is3D: true
};
function drawChart() {
// Set Data
const data = google.visualization.arrayToDataTable([
['Price', 'Size'],
[50,7],[60,8],[70,8],[80,9],[90,9],[100,9],
[110,10],[120,11],[130,14],[140,14],[150,15]
]);
// Set Options
const options = {
title: 'House Prices vs Size',
hAxis: {title: 'Square Meters'},
vAxis: {title: 'Price in Millions'},
legend: 'none'
};
// Draw Chart
const chart = new google.visualization.LineChart(document.getElementById('myChart'));
chart.draw(data, options);
}
到散点图相同的数据,将 google.visualization 更改为 ScatterChart:
const chart = new google.visualization.ScatterChart(document.getElementById('myChart'));
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!