当前位置:网站首页>[antv G2] how to add a click event to the line chart (click anywhere to get the value of the point on the line)
[antv G2] how to add a click event to the line chart (click anywhere to get the value of the point on the line)
2022-07-19 02:22:00 【yulamz】
【Antv G2】 How to add click events to a line chart ( Click anywhere to get the value of the point on the polyline )
Demand analysis
There's a demand recently , It is required to point at any position on the line chart , You can get what the user currently sees tooltip Information in . After getting the date that the user currently sees , Then send a request to get some other data at this time point to show . If there are students who play fund or stock investment, I believe they are not unfamiliar with this scene .
The essence of this requirement is to add click events to the line chart , I just searched the Internet , Most people summarize the scheme that only clicking on the circular break point of the broken line can trigger , Such operation may be too harsh for users . therefore , I'll summarize the scheme that clicking anywhere on the line chart can trigger a click event .
Expected effect

stay Antv G2 I found a basic line chart on the official website of , The effect is as follows : Move the mouse to any position on the canvas ,tooltip Can display relevant information . When you click the mouse , You can get the current tooltip Information displayed .
Realize the idea
- When the user's mouse moves on the line chart , monitor tooltip The change of , Record through a variable tooltip The current value of the .
Corresponding event name :tooltip:change - utilize G2 Provided Combined events , The drawing area of the line chart is monitored click When . here , Get tooltip Do whatever you need to do with the current value of .
Corresponding event name :plot:click
Code demonstration
import {
Chart } from '@antv/g2';
const data = [
{
year: '1991', value: 3 },
{
year: '1992', value: 4 },
{
year: '1993', value: 3.5 },
{
year: '1994', value: 5 },
{
year: '1995', value: 4.9 },
{
year: '1996', value: 6 },
{
year: '1997', value: 7 },
{
year: '1998', value: 9 },
{
year: '1999', value: 13 },
];
const chart = new Chart({
container: 'container',
autoFit: true,
height: 500,
});
let hoverData = {
}; // new Add -- If it's in vue Use in , Define the variable in data Then you can
chart.data(data);
chart.scale({
year: {
range: [0, 1],
},
value: {
min: 0,
nice: true,
},
});
chart.tooltip({
showCrosshairs: true, // Exhibition Tooltip guide
shared: true,
});
chart.line().position('year*value').label('value');
chart.point().position('year*value');
// new Add -- monitor tooltip:change event
chart.on('tooltip:change', ev => {
// You can print it on the console ev, Look at the structure inside , Find the information you need
hoverData = ev.data.items[0].data;
})
// new Add -- Monitor the drawing area plot Click events for
chart.on('plot:click', ev => {
// When the user clicks on the canvas , At this time, the user sees tooltip The data on has already existed hoverData in . Get hoverData, It is equivalent to that you get the data of relevant points on the broken line , After that, any demand can be realized !
console.log(JSON.stringify(hoverData));
})
chart.render();
边栏推荐
- 字符串全排列问题
- Gdb+vscode for debugging 3 - vscode and GDB remote debugging
- Bugku problem solution
- 攻防世界----shrine
- [unity development tips] unity packs the EXE on the PC side and compresses and packs it into an EXE file
- Visual studio 2019-qt debugging
- STL -- deque container
- 保留两位小数,并向上取值
- [unity Editor Extension] unity publishes resources to the store process unity asset store publish
- 【工具篇】Unity屏幕画线,Unity屏幕画图HSJ绘画工具
猜你喜欢

Jmeter接口测试之响应断言

Cookie和Session的区别

ENVI_ Idl: batch process the re projection of MODIS swath data and output it to GeoTIFF format + detailed analysis

Bugku---- regular matching, cookies

Logic vulnerability - login verification code security

Swagger——世界上最流行的Api框架

sqlmap的使用
![【已解决】参考了本地mysql忘记密码后, [Server] --initialize specified but the data directory has files in it. Aborti](/img/a8/2daa2c0d834f1986c8421bf5138c7e.png)
【已解决】参考了本地mysql忘记密码后, [Server] --initialize specified but the data directory has files in it. Aborti

使用JMeter测试基于WebSocket协议的服务

STL--deque容器
随机推荐
SSTI模板注入
转载:SQL注入常见绕过
JS note 1
Buaaos-lab0 experimental report
Injection de modèles ssti
leetcode力扣经典题目——82.柱形图中的最大矩形
[tools] unity2d character controller, which controls 2D players to move and jump in four directions and horizontal directions
项目性能优化实战:解决首页白屏问题,自定义 loading 动画优化首屏效果
LeetCode:动态规划中的子序列问题
Attack and defense world - easytornado notes
【工具篇】Unity快速上手制作2D和2.5D游戏的神器TileMap
ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing
Logical vulnerability - authentication vulnerability
(附word操作以及视频讲解)使用ARCGIS进行地图配准_投影变换_普通地图制作_专题地图制作
字符串转换为整数
Gdb+vscode for debugging 1 -- compile and debug using cmakelist files + attach process debugging
各种开发工具
新手如何配置多个 SSH Key(通俗易懂手把手教学)
元宇宙公链Caduceus详解:专为元宇宙应用打造的创作平台
ENVI_IDL:讀取所有OMI產品的NO2柱含量並計算月均值、季均值、年均值+解析