当前位置: 首页 > news >正文

react + G2(v4) 应用

记录一下react中使用G2("@antv/g2": "^4.0.15"),折线图标

完整代码

import React, { Component } from "react";
import { Chart } from '@antv/g2'export default class page extends Component {constructor(props) {super(props);this.chartRef = React.createRef();this.chart = null;}// // 组件挂载componentDidMount() {this.createChart()}// 组件卸载时销毁图表componentWillUnmount() {if (this.chart) {this.chart.destroy();}}// 创建表格createChart() {// 图表数据let data = [{"date": "2025-08-14","value": "453","name": "心率"},{"date": "2025-08-14","value": "43","name": "心率"},{"date": "2025-08-14","value": "80","name": "心率"}]// 确保容器存在if (!this.chartRef.current) return;// 销毁已存在的图表if (this.chart) {this.chart.destroy();}const container = this.chartRef.current;this.chart = new Chart({container: this.chartRef.current,width: '100%',height: 500,autoFit: true  // 开启自适应});this.chart.data(data);this.chart.axis('value', {label: {textStyle: { fontSize: 12 }  // 调整标签字体大小},title: {text: '次'// 轴标题也可包含单位}});this.chart.scale('value', {nice: true,formatter: (val) => `${val} `});this.chart.line().position('date*value').color('name').shape('smooth');this.chart.point().position("date*value").style({fill: 'white',// stroke: '#5B8FF9',lineWidth: 2}).tooltip('date*name*value', (date,name,value)=>{return {title: date,name: name,value: value}})this.chart.legend("name")this.chart.render();}render() {return (<div style={{visibility: currTag==='1'? 'visible':'hidden',width: '100%',height: '500px'}} className="chart" ref={this.chartRef}></div>)}
}

图表数据

在这里插入图片描述
name 如果有多种,就会显示多条折线
比如:name的值可以是舒张压、收缩压,则图表会显示两条线。一条是舒张压的,一条是收缩压

tooltip

在这里插入图片描述
一开始没有写上面那个配置的时候,鼠标悬浮上去的时候,只显示这样。本来是心率的,却显示了value
在这里插入图片描述
后面加上配置后就显示正常了:
date对应title
在这里插入图片描述

图表显示隐藏

在这里插入图片描述
我的需求是切换 tab 的时候,图表要跟着显示隐藏。试了直接用display设置blocknone无效,图表会一直显示在那里,然后用visibility就可以

http://www.dtcms.com/a/353899.html

相关文章:

  • 【C++游记】模板升级
  • 飞腾 D2000 八核处理器板卡深度解析:全国产化硬件方案与丰富扩展能力
  • Linux中创建SFTP账号
  • Netty:现代网络应用的利器
  • 软件定义汽车(SDV)调试——如何做到 适配软件定义汽车(SDV)?(中)
  • 造作AI-你的人工智能创作助手
  • 某中医院信息化能力提升:智能组网设备助力网络架构优化
  • 【日常学习】2025-8-27 测开框架设计模式探索04
  • Element整体操作样式
  • 数据分析编程第五步:数据准备与整理
  • DDD之事件机制(9)
  • 沃丰科技出海客服系统对接沃尔玛全球电商平台,赋能中企出海
  • 升级DrRacket8.10到8.18版本@Ubuntu24.04
  • GitLab 导入/导出仓库
  • 金融 IT 运维痛点突围:用网络管理工具筑牢业务稳定防线(附 OpManager Plus 实践)
  • 【51单片机按键按下数码管秒增计时并LED亮释放停计时LED熄】2022-11-12
  • Android -第二十一次技术总结
  • 使用LLAMA_cpp_python进行qwen2.5-vl-7b-instruct进行推理
  • 【URP】Unity Shader Tags
  • IT66122替代IT66121-富利威
  • Day12 Gin框架学习
  • .NET周刊【8月第3期 2025-08-17】
  • 【C#】获取不重复的编码(递增,非GUID)
  • (LeetCode 面试经典 150 题) 102. 二叉树的层序遍历(广度优先搜索bfs)
  • Miniforge3替代Anaconda的一些经验总结
  • STL库——vector(类模拟实现)
  • 旧物二手回收小程序系统:让闲置旧物焕发新生,创造无限价值
  • Leetcode 深度优先搜索 (14)
  • 胶水研究记录学习1
  • 回顾websocket心跳机制以及断线重连(服务端为node)