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="./js/g2.min.js"></script>
<script src="./js/data-set.min.js"></script>
使用内嵌对js文件进行引入,用于后期图表使用。
2. 知识点B - 如何定义容器
<div id="duidiezhuzhuangtu"></div>
使用定义容器,用于展示图表。容器名:duidiezhuzhuangtu。
3. 知识点C - 如何定义数据
const data = [
{ name:'美国', '一月.': 18.9, '二月.': 28.8, '三月.' :39.3, '四月.': 81.4, '五月': 47, 'Jun.': 20.3, '七月.': 24, '八月.': 35.6, '九月.': 25, '十月.': 38, '十一月.': 45.6, '十二月.': 39.8 },
{ name:'俄罗斯', '一月.': 12.4, '二月.': 23.2, '三月.' :34.5, '四月.': 99.7, '五月': 52.6, 'Jun.': 35.5, '七月.': 37.4, '八月.': 42.4, '九月.': 46.5, '十月.': 44.4, '十一月.': 52.4, '十二月.': 63.5}
];
- data:使用数组形式定义数据。
- 格式:{名称:'对应图例名称',x轴值:y轴值,x轴值:y轴值}
4. 知识点D - 如何引用数据
const chart = new G2.Chart({
container: 'duidiezhuzhuangtu',
forceFit: true,
height: window.innerHeight
});
chart.source(dv);
- container:定于数据从duidiezhuzhuangtu数组取值。
- forceFit: 定义图表的宽度自适应开关,默认为 false,设置为 true 时表示自动取 dom(实例容器)的宽度。
- height: 定义图表高度。
- source:定义为chart装载数据,返回chart对象。
5. 知识点E - 如何定义图例
const ds = new DataSet();
const dv = ds.createView().source(data);
dv.transform({
type: 'fold',
fields: [ '一月.','二月.','三月.','四月.','五月','Jun.','七月.','八月.','九月.','十月.','十一月.','十二月.' ], // 展开字段集
key: 'yuefen',
value: 'jiangyuliang',
});
- fields:定义图例名称。格式:['图例名称','图例名称','....']
- key:定义key值。
- value:定义value值。
6. 知识点F - 如何渲染图表
chart.intervalStack()
.position('yuefen*jiangyuliang')
.color('name');
chart.render();
- render:用于将图表渲染至画布。
完整代码
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height">
<title>堆叠柱状图</title>
<style>::-webkit-scrollbar{display:none;}html,body{overflow:hidden;height:100%;}</style>
</head>
<body>
<div id="duidiezhuzhuangtu"></div>
<script>/*Fixing iframe window.innerHeight 0 issue in Safari*/document.body.clientHeight;</script>
<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 = [
{ name:'美国', '一月.': 18.9, '二月.': 28.8, '三月.' :39.3, '四月.': 81.4, '五月': 47, 'Jun.': 20.3, '七月.': 24, '八月.': 35.6, '九月.': 25, '十月.': 38, '十一月.': 45.6, '十二月.': 39.8 },
{ name:'俄罗斯', '一月.': 12.4, '二月.': 23.2, '三月.' :34.5, '四月.': 99.7, '五月': 52.6, 'Jun.': 35.5, '七月.': 37.4, '八月.': 42.4, '九月.': 46.5, '十月.': 44.4, '十一月.': 52.4, '十二月.': 63.5}
];
const ds = new DataSet();
const dv = ds.createView().source(data);
dv.transform({
type: 'fold',
fields: [ '一月.','二月.','三月.','四月.','五月','Jun.','七月.','八月.','九月.','十月.','十一月.','十二月.' ], // 展开字段集
key: 'yuefen',
value: 'jiangyuliang',
});
const chart = new G2.Chart({
container: 'duidiezhuzhuangtu',
forceFit: true,
height: window.innerHeight
});
chart.source(dv);
chart.intervalStack()
.position('yuefen*jiangyuliang')
.color('name');
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
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
Copied from https://antv.alipay.com/zh-cn/g2/3.x/demo/bar/stacked-column.html
Variable names changed.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
No matter approved or not, I upvote and support you.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thanks
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @hui.zhao I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit