AntVG2可视化学习与开发笔记-React19(持续更新)

目录

开始工作

第一步:创建画布空间

第二步:获取画布空间并挂载AntVG2

第三步:进行画布设计配置与数据挂载

第四步:完整代码

实际效果如下

参数理解

一、scale

[1. 归一化range:[0,1]](#1. 归一化range:[0,1])

2.nice、domainMin


开始工作

第一步:创建画布空间

复制代码
      <div ref={containerRef} style={{ width: '100%' }} />

第二步:获取画布空间并挂载AntVG2

复制代码
  const chartRef = useRef<Chart | null>(null)
  const containerRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    if (!containerRef.current) return
    // 创建 G2 图表实例,并存储到 ref 中
    chartRef.current = new Chart({
      container: containerRef.current,
      autoFit: true,
      height: 400,
    })

    // 组件卸载时销毁 chart 实例
    return () => {
      chartRef.current?.destroy()
      chartRef.current = null
    }
  }, [])

第三步:进行画布设计配置与数据挂载

复制代码
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 },
]


    chartRef.current
      .line()
      .data(data)
      .encode('x', 'year')
      .encode('y', 'value')
      .scale('x', { range: [0, 1], nice: true })
      .scale('y', { nice: true })
    chartRef.current.render()

第四步:完整代码

复制代码
import { Chart } from '@antv/g2'
import React, { useEffect, useRef } from 'react'

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 ChartShow: React.FC = () => {
  // 定义 ref 用来存储 chart 实例和 DOM 容器
  const chartRef = useRef<Chart | null>(null)
  const containerRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    if (!containerRef.current) return
    // 创建 G2 图表实例,并存储到 ref 中
    chartRef.current = new Chart({
      container: containerRef.current,
      autoFit: true,
      height: 400,
    })
    chartRef.current
      .line()
      .data(data)
      .encode('x', 'year')
      .encode('y', 'value')
      .scale('x', { range: [0, 1], nice: true })
      .scale('y', { nice: true })
    chartRef.current.render()
    // 组件卸载时销毁 chart 实例
    return () => {
      chartRef.current?.destroy()
      chartRef.current = null
    }
  }, [])
  const updateChart = () => {
    if (chartRef.current) {
      chartRef.current.render()
    }
  }

  return (
    <div>
      <div ref={containerRef} style={{ width: '50%' }} />
      <button onClick={updateChart}>更新图表</button>
    </div>
  )
}

export default ChartShow

实际效果如下

参数理解

一、scale

1. 归一化range:[0,1]

y轴设置归一化倒转问题

2.nice、domainMin

domainMin强制让Y从0开始渲染

nice让刻度变得更整齐

复制代码
.scale('y', {
  domainMin: 0,
  nice: true,
});

二、encode

复制代码
.encode('x', 'x')
.encode('y', 'y')
.encode('shape', 'category')
.encode('color', 'category')
.encode('size', 5)

概念理解

一、视觉通道

二、视觉属性

相关推荐
wuk99816 分钟前
梁非线性动力学方程MATLAB编程实现
前端·javascript·matlab
●VON21 分钟前
从模型到价值:MLOps 工程体系全景解析
人工智能·学习·制造·von
好奇龙猫22 分钟前
【人工智能学习-AI-MIT公开课第 18. 表示:分類、軌跡、過渡】
学习
XiaoYu200227 分钟前
第11章 LangChain
前端·javascript·langchain
慕容雪_1 小时前
运维笔记-网络共享
运维·笔记·网络共享
hhcccchh1 小时前
学习vue第八天 Vue3 模板语法和内置指令 - 简单入门
前端·vue.js·学习
yyf198905251 小时前
Vue 框架相关中文文献
前端·javascript·vue.js
浩瀚地学2 小时前
【Java】异常
java·开发语言·经验分享·笔记·学习
Groundwork Explorer2 小时前
WSL Python Kivy Buildozer APK打包笔记
笔记
行者962 小时前
Flutter适配OpenHarmony:国际化i18n实现中的常见陷阱与解决方案
开发语言·javascript·flutter·harmonyos·鸿蒙