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>
使用内嵌script对js文件进行引入,用于后期图表使用。
2. 知识点B - 如何定义数据
const data = [
{ x: 95, y: 95, z: 13.8, name: 'BE', country: 'Belgium' },
{ x: 86.5, y: 102.9, z: 14.7, name: 'DE', country: 'Germany' },
{ x: 80.8, y: 91.5, z: 15.8, name: 'FI', country: 'Finland' },
{ x: 80.4, y: 102.5, z: 12, name: 'NL', country: 'Netherlands' },
{ x: 80.3, y: 86.1, z: 11.8, name: 'SE', country: 'Sweden' },
{ x: 78.4, y: 70.1, z: 16.6, name: 'ES', country: 'Spain' },
{ x: 74.2, y: 68.5, z: 14.5, name: 'FR', country: 'France' },
{ x: 73.5, y: 83.1, z: 10, name: 'NO', country: 'Norway' },
{ x: 71, y: 93.2, z: 24.7, name: 'UK', country: 'United Kingdom' },
{ x: 69.2, y: 57.6, z: 10.4, name: 'IT', country: 'Italy' },
{ x: 68.6, y: 20, z: 16, name: 'RU', country: 'Russia' },
{ x: 65.5, y: 126.4, z: 35.3, name: 'US', country: 'United States' },
{ x: 65.4, y: 50.8, z: 28.5, name: 'HU', country: 'Hungary' },
{ x: 63.4, y: 51.8, z: 15.4, name: 'PT', country: 'Portugal' },
{ x: 64, y: 82.9, z: 31.3, name: 'NZ', country: 'New Zealand' },
];
- data:使用数组形式定义数据。
- name:用户气泡图标识。
3. 知识点C - 如何引用数据
const chart = new G2.Chart({
container: 'BubbleChart',
forceFit: true,
height: window.innerHeight,
padding: [20, 20, 50, 80],
plotBackground: {
stroke: '#ccc',
lineWidth: 1,
},
});
chart.source(data, {
x: {
alias: 'Daily fat intake',
tickInterval: 5,
nice: false,
max: 96,
min: 62,
},
y: {
alias: 'Daily sugar intake',
tickInterval: 50,
nice: false,
max: 165,
min: 0,
},
z: {
alias: 'Obesity(adults) %',
},
});
- container:定于数据从BubbleChart数组取值。
- forceFit: 定义图表的宽度自适应开关,默认为 false,设置为 true 时表示自动取 dom(实例容器)的宽度。
- height: 定义图表高度。
- plotBackground:图表绘图区域的边框和背景样式。
- stroke:定义边颜色
- lineWidth:定义边框粗细
- source:定义为chart装载数据,返回chart对象。
- alias:定义别名
- tickInterval:自定义刻度间距
- nice:不对最大最小值优化
- max:自定义最大值
- min:自定义最小值
4. 知识点D - 如何定义坐标轴
chart.axis('x', {
label: {
formatter: function(val) {
return val + ' gr';
}
},
grid: {
lineStyle: {
stroke: '#d9d9d9',
lineWidth: 1,
lineDash: [2, 2],
},
}
});
chart.axis('y', {
title: {
offset: 64,
},
label: {
formatter: function(val) {
if (val > 0) {
return val + ' gr';
}
}
}
});
- formatter:格式化坐标轴显示文本。
- stroke:定义边颜色
- lineWidth:定义边框粗细
5. 知识点E - 如定义提示框
chart.tooltip({
title: 'country',
});
chart
.point()
.position('x*y')
.color('#1890ff')
.size('z', [ 10, 40 ])
.label('name*country', {
offset: 0, // 文本距离图形的距离
textStyle: {
fill: '#1890FF'
},
})
.opacity(0.3)
.shape('circle')
.tooltip('x*y*z')
.style({
lineWidth: 1,
stroke: '#1890ff'
});
- tooltip:图表的 tooltip 配置,G2 图表的 tooltip 使用 html 渲染。
- point:创建点图图,返回一个 geom 对象。
- color:定义颜色。
- size:传入数字常量
- opacity:将数据值映射到图形的透明度上的方法。
6. 知识点F - 如何渲染
chart.guide().line({
top: true,
start: [65, 'min'],
end: [65, 'max'],
text: {
content: 'Safe fat intake 65g/day',
position: 'end',
autoRotate: false,
style: {
textAlign: 'start'
}
},
});
chart.guide().line({
top: true,
start: ['min', 50],
end: ['max', 50],
text: {
content: 'Safe sugar intake 50g/day',
position: 'end',
style: {
textAlign: 'end'
}
},
});
chart.render();
- guide:用于绘制图表的辅助元素,该方法的返回值不为 chart 对象,而是一个 guide 对应的控制类。
- line:创建线图,返回一个 geom 对象。
- start:辅助线起始位置,值为原始数据值,支持 callback
- end:辅助线结束位置,值为原始数据值,支持 callback
- content:显示文字
- position:对齐方式
- render:图表绘制的最后一步,用于将图表渲染至画布。
完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>带文本的气泡图</title>
</head>
<body>
<div id="BubbleChart"></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 = [
{ x: 95, y: 95, z: 13.8, name: 'BE', country: 'Belgium' },
{ x: 86.5, y: 102.9, z: 14.7, name: 'DE', country: 'Germany' },
{ x: 80.8, y: 91.5, z: 15.8, name: 'FI', country: 'Finland' },
{ x: 80.4, y: 102.5, z: 12, name: 'NL', country: 'Netherlands' },
{ x: 80.3, y: 86.1, z: 11.8, name: 'SE', country: 'Sweden' },
{ x: 78.4, y: 70.1, z: 16.6, name: 'ES', country: 'Spain' },
{ x: 74.2, y: 68.5, z: 14.5, name: 'FR', country: 'France' },
{ x: 73.5, y: 83.1, z: 10, name: 'NO', country: 'Norway' },
{ x: 71, y: 93.2, z: 24.7, name: 'UK', country: 'United Kingdom' },
{ x: 69.2, y: 57.6, z: 10.4, name: 'IT', country: 'Italy' },
{ x: 68.6, y: 20, z: 16, name: 'RU', country: 'Russia' },
{ x: 65.5, y: 126.4, z: 35.3, name: 'US', country: 'United States' },
{ x: 65.4, y: 50.8, z: 28.5, name: 'HU', country: 'Hungary' },
{ x: 63.4, y: 51.8, z: 15.4, name: 'PT', country: 'Portugal' },
{ x: 64, y: 82.9, z: 31.3, name: 'NZ', country: 'New Zealand' },
];
const chart = new G2.Chart({
container: 'BubbleChart',
forceFit: true,
height: window.innerHeight,
padding: [20, 20, 50, 80],
plotBackground: {
stroke: '#ccc',
lineWidth: 1,
},
});
chart.source(data, {
x: {
alias: 'Daily fat intake',
tickInterval: 5,
nice: false,
max: 96,
min: 62,
},
y: {
alias: 'Daily sugar intake',
tickInterval: 50,
nice: false,
max: 165,
min: 0,
},
z: {
alias: 'Obesity(adults) %',
},
});
chart.axis('x', {
label: {
formatter: function(val) {
return val + ' gr';
}
},
grid: {
lineStyle: {
stroke: '#d9d9d9',
lineWidth: 1,
lineDash: [2, 2],
},
}
});
chart.axis('y', {
title: {
offset: 64,
},
label: {
formatter: function(val) {
if (val > 0) {
return val + ' gr';
}
}
}
});
chart.legend(false);
chart.tooltip({
title: 'country',
});
chart
.point()
.position('x*y')
.color('#1890ff')
.size('z', [ 10, 40 ])
.label('name*country', {
offset: 0, // 文本距离图形的距离
textStyle: {
fill: '#1890FF'
},
})
.opacity(0.3)
.shape('circle')
.tooltip('x*y*z')
.style({
lineWidth: 1,
stroke: '#1890ff'
});
chart.guide().line({
top: true,
start: [65, 'min'],
end: [65, 'max'],
text: {
content: 'Safe fat intake 65g/day',
position: 'end',
autoRotate: false,
style: {
textAlign: 'start'
}
},
});
chart.guide().line({
top: true,
start: ['min', 50],
end: ['max', 50],
text: {
content: 'Safe sugar intake 50g/day',
position: 'end',
style: {
textAlign: 'end'
}
},
});
chart.render();
</script>
</body>
</html>
最终效果
系列课程
如果您喜欢我的教程,可以在我的个人档案页面,获取更多信息。
您可以使用zqz-tutorial标签快速查看我发布的所有教程。
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
The contribution cannot be approved because it does not follow the Utopian Rules.
It is taken from official examples. https://antv.alipay.com/zh-cn/g2/3.x/demo/other/bubble-text.html
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit