Summary:
G2 is a set of graphically-based graphical syntax that is data-driven and highly user-friendly and extensible, allowing users to build a wide variety of interactive statistics without having to worry about detailed implementation details chart.
G2 是一套基于可视化编码的图形语法,以数据驱动,具有高度的易用性和扩展性,用户无需关注各种繁琐的实现细节,一条语句即可构建出各种各样的可交互的统计图表。
您将从这个教程中学到什么
- 如何引入js文件
- 如何定义容器
- 如何定义数据
- 如何引用数据
- 如何定义提示框
- 如何渲染图表
学习此教程的必备条件
教程难度
- 容易
教程内容
演示效果
1. 知识点A - 如何引入js文件
<script src="https://gw.alipayobjects.com/os/antv/assets/g2/3.0.4-beta.2/g2.min.js"></script>
<script src="https://gw.alipayobjects.com/os/antv/assets/data-set/0.8.3/data-set.min.js"></script>
使用内嵌对js文件进行引入,用于后期图表使用。
2. 知识点B - 如何定义容器
<div id="stackedarea"></div>
使用定义容器,用于展示图表。容器名:stackedarea。
3. 知识点C - 如何定义数据
const data = [
{country: 'Asia', year: '1750', value: 502},
{country: 'Asia', year: '1800', value: 635},
{country: 'Asia', year: '1850', value: 809},
{country: 'Asia', year: '1900', value: 5268},
{country: 'Asia', year: '1950', value: 4400},
{country: 'Asia', year: '1999', value: 3634},
{country: 'Asia', year: '2050', value: 947},
{country: 'Africa', year: '1750', value: 106},
{country: 'Africa', year: '1800', value: 107},
{country: 'Africa', year: '1850', value: 111},
{country: 'Africa', year: '1900', value: 1766},
{country: 'Africa', year: '1950', value: 221},
{country: 'Africa', year: '1999', value: 767},
{country: 'Africa', year: '2050', value: 133},
{country: 'Europe', year: '1750', value: 163},
{country: 'Europe', year: '1800', value: 203},
{country: 'Europe', year: '1850', value: 276},
{country: 'Europe', year: '1900', value: 628},
{country: 'Europe', year: '1950', value: 547},
{country: 'Europe', year: '1999', value: 729},
{country: 'Europe', year: '2050', value: 408},
{country: 'Oceania', year: '1750', value: 200},
{country: 'Oceania', year: '1800', value: 200},
{country: 'Oceania', year: '1850', value: 200},
{country: 'Oceania', year: '1900', value: 460},
{country: 'Oceania', year: '1950', value: 230},
{country: 'Oceania', year: '1999', value: 300},
{country: 'Oceania', year: '2050', value: 300},
];
- const: 用于定义数组。
- data:定义为数组名。
- 格式:{数据名称:'图例名称',数据名称:'数据值',数据名称:'数据值'}
4. 知识点D - 如何引用数据
const chart = new G2.Chart({
container: 'stackedarea',
forceFit: true,
height: window.innerHeight
});
chart.source(data, {
year: {
type: 'linear',
tickInterval: 50
}
});
- chart:创建一个chart实例,返回一个Chart对象,建议在单个容器上只初始化一个Chart实例。
- container:定于数据从stackedarea数组取值。
- forceFit: 定义图表的宽度自适应开关,默认为 false,设置为 true 时表示自动取 dom(实例容器)的宽度。
- height: 定义图表高度。
- window.innerHeight: 获取页面可用高度。
- source:定义为chart装载数据,返回chart对象。
- year:使用year作为y轴图表数据依据。
- type:指定数据类型,可声明的类型为:identity、linear、cat、time、timeCat、log、pow
5. 知识点E - 如何定义提示框
chart.tooltip({
crosshairs: {
type: 'line'
}
});
- tooltip:图表的 tooltip 配置,G2 图表的 tooltip 使用 html 渲染。
- crosshairs:是一个对象类型,用于设置 tooltip 的辅助线或者辅助框。
- type:默认我们为geom为line开启了垂直辅助线。
6. 知识点F - 如何渲染图表
chart.areaStack().position('year*value').color('country');
chart.lineStack().position('year*value').color('country').size(2);
chart.render();
- position:将数据值映射到图形的位置上的方法。position 属性会对多个字段进行数据的映射。以chart.interval().position('xy') 为例,interval代表柱状图,即最后需要生成柱状图,而position代表位置,position('x*y')代表数据在图形中的位置由x和y这两个维度的变量决定,(x1,y1) 这样的数值对,最后就会被转换为画布上对应的坐标点。
- render:用于将图表渲染至画布。
完整代码
<html lang="en">
<head>
<meta charset="UTF-8">
<title>堆叠面积图</title>
</head>
<body>
<div id="stackedarea"></div>
<script src="https://gw.alipayobjects.com/os/antv/assets/g2/3.0.4-beta.2/g2.min.js"></script>
<script src="https://gw.alipayobjects.com/os/antv/assets/data-set/0.8.3/data-set.min.js"></script>
<script>
const data = [
{country: 'Asia', year: '1750', value: 502},
{country: 'Asia', year: '1800', value: 635},
{country: 'Asia', year: '1850', value: 809},
{country: 'Asia', year: '1900', value: 5268},
{country: 'Asia', year: '1950', value: 4400},
{country: 'Asia', year: '1999', value: 3634},
{country: 'Asia', year: '2050', value: 947},
{country: 'Africa', year: '1750', value: 106},
{country: 'Africa', year: '1800', value: 107},
{country: 'Africa', year: '1850', value: 111},
{country: 'Africa', year: '1900', value: 1766},
{country: 'Africa', year: '1950', value: 221},
{country: 'Africa', year: '1999', value: 767},
{country: 'Africa', year: '2050', value: 133},
{country: 'Europe', year: '1750', value: 163},
{country: 'Europe', year: '1800', value: 203},
{country: 'Europe', year: '1850', value: 276},
{country: 'Europe', year: '1900', value: 628},
{country: 'Europe', year: '1950', value: 547},
{country: 'Europe', year: '1999', value: 729},
{country: 'Europe', year: '2050', value: 408},
{country: 'Oceania', year: '1750', value: 200},
{country: 'Oceania', year: '1800', value: 200},
{country: 'Oceania', year: '1850', value: 200},
{country: 'Oceania', year: '1900', value: 460},
{country: 'Oceania', year: '1950', value: 230},
{country: 'Oceania', year: '1999', value: 300},
{country: 'Oceania', year: '2050', value: 300},
];
const chart = new G2.Chart({
container: 'stackedarea',
forceFit: true,
height: window.innerHeight
});
chart.source(data, {
year: {
type: 'linear',
tickInterval: 50
}
});
chart.tooltip({
crosshairs: {
type: 'line'
}
});
chart.areaStack().position('year*value').color('country');
chart.lineStack().position('year*value').color('country').size(2);
chart.render();
</script>
</body>
</html>
最终效果
系列课程
- 如果您喜欢我的教程,可以在我的个人档案页面,获取更多信息。
If you like my tutorial , You can check out your profile for more such tutorials. - 您可以使用zqz-tutorial标签快速查看我发布的所有教程
You can use the "zqz-tutorial" tag to see all the tutorials I've posted.
Posted on Utopian.io - Rewarding Open Source Contributors
Like your contribution, upvote.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
第一个标签是什么。。。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thank you very much
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
The contribution cannot be approved because it does not follow the Utopian Rules.
Taken from https://antv.alipay.com/zh-cn/g2/3.x/demo/area/stacked.html
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit