Antv-G2中数据更新但是不展示或者创建一个新的没有销毁原有的Chart问题

该文章展示了一种在React应用中通过父组件向子组件传递数据来更新G2图表的方法。当接收到新数据时,通过`useEffect`钩子判断并刷新图表。同时,在组件卸载时,使用`useEffect`的清理函数销毁图表实例,避免内存泄漏。文章还详细展示了创建和配置图表的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这边我的数据传递是通过父穿子数据来更新chart图标,在同一个模块用接口获取数据同理

是通过对g2可视化的挂在和current属性来判断是否需要destroy操作


import { Chart } from '@antv/g2'

import React, { useEffect,useRef } from 'react'
const Temperature = (props) => {
  
  const {data}= props
  //重点
  const chart = useRef()
  
  useEffect(() => {
//判断是否有数据传递过来进行刷新
    if(data){
      getChart(data)
    }
  }, [data])
  useEffect(()=>{
    return () => {
      if(chart.current){
        chart.current.destroy()
      }
    }
  },[])

   const getChart =(data)=>{
    if(!chart.current){
        chart.current = new Chart({
        container: 'temperature',
        autoFit: true,
        height: 180,
        width: 480,
        // width: 'auto',
      })
    }
  const {current:_chart} = chart
    _chart.clear()
    _chart.data(data)
//下方配置就看个人需求,主要是改的是上面部分代码
    _chart.scale('value', {
      alias: 'mg/m³',
      nice: true,
    })
    _chart.scale({
      date: {
        nice: true,
        range: [0, 1],
      },
      value: {
        // alias: '进入次数',
        // min: 2,
        // max: 6,
        sync: true, // 将 合格率 字段数值同 废品率 字段数值进行同步
        nice: true,
      },
    })

    _chart.axis('value', {
      grid: {
        line: {
          type: 'line',
          style: {
            // fill:'#ff0000',
            stroke: '#482daf ',
            // opacity:0.3,
            // lineDash:[1,3],//虚线
          },
        },
      },
      label: {
        style: {
          fill: '#fff', //文字颜色
          // fontFamily: "Microsoft YaHei",//文字字体
          fontWeight: 400, //文字粗细
          fontSize: 20, //文字大小
        },
        formatter: (text) => {
          return text + '℃'
        },
      },
      line: {
        style: {
          stroke: '#fff', //坐标轴颜色
        },
      },
    })
    _chart.axis('time', {
      grid: {
        line: {
          type: 'line',
          style: {
            // fill:'#ff0000',
            stroke: '#482daf ',
            // opacity:0.3,
            // lineDash:[1,3],//虚线
          },
        },
      },
      label: {
        style: {
          fill: '#fff', //文字颜色
          // fontFamily: "Microsoft YaHei",//文字字体
          // fontWeight: 400, //文字粗细
          marginTop: 10,
          fontSize: 20, //文字大小
        },
      },
      line: {
        style: {
          stroke: '#fff', //坐标轴颜色
        },
      },
    })

    _chart.tooltip({
      showMarkers: false,
    })
    _chart.line().shape('smooth').position('time*value').color('#01ffff')
    _chart.interaction('element-active')

    // 添加文本标注
    data.forEach((item) => {
      _chart.annotation().text({
        position: [item.time, item.value],
        content: item.value,
        style: {
          textAlign: 'center',
          fill: '#fff',
          fontSize: 20,
        },
        offsetY: -10,
      })
    })
    _chart.render()
    return _chart
   }


  return (
    <div>
      <div id="temperature"></div>
  
    </div>
  )
}

export default Temperature
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值