Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

谷歌图表

从简单的折线图到复杂的层次树状图,谷歌图表库提供了大量的现成图表类型。

  • 散点图
  • 折线图
  • 条形图/柱状图
  • 面积图
  • 饼图
  • 环形图
  • 组织结构图
  • 地图/地理图表

如何使用谷歌图表?

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. 加载图表 API,并在 API 加载后添加要运行的函数。

<script>
google.charts.load('current',{packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);

// 您的函数
function drawChart() {
...
}
</script>

条形图

源代码

function drawChart() {

// 设置数据
const data = google.visualization.arrayToDataTable([
  ['国家', 'Mhl'],
  ['意大利', 55],
  ['法国', 49],
  ['西班牙', 44],
  ['美国', 24],
  ['阿根廷', 15]
]);

// 设置选项
const options = {
  title: '全球葡萄酒产量'
};

// 绘制
const chart = new google.visualization.BarChart(document.getElementById('myChart'));
chart.draw(data, options);

}

试试看 »


饼图

要将条形图转换为饼图,只需替换

google.visualization.BarChart



google.visualization.PieChart

const chart = new google.visualization.PieChart(document.getElementById('myChart'));

试试看 »


3D 饼图

要以 3D 方式显示饼图,只需在选项中添加is3D: true

const options = {
  title: '全球葡萄酒产量',
  is3D: true
};

试试看 »


折线图

源代码

function drawChart() {
// 设置数据
const data = google.visualization.arrayToDataTable([
  ['价格', '尺寸'],
  [50,7],[60,8],[70,8],[80,9],[90,9],[100,9],
  [110,10],[120,11],[130,14],[140,14],[150,15]
  ]);
// 设置选项
const options = {
  title: '房价与尺寸',
  hAxis: {title: '平方米'},
  vAxis: {title: '价格(百万)'},
  legend: 'none'
};
// 绘制图表
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'));

试试看 »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.