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)

概念理解

一、视觉通道

二、视觉属性

相关推荐
Byron070730 分钟前
Vue 中使用 Tiptap 富文本编辑器的完整指南
前端·javascript·vue.js
一只小小的芙厨1 小时前
寒假集训笔记·树上背包
c++·笔记·算法·动态规划
盐焗西兰花1 小时前
鸿蒙学习实战之路-Reader Kit构建阅读器最佳实践
学习·华为·harmonyos
Mr Xu_2 小时前
告别硬编码:前端项目中配置驱动的实战优化指南
前端·javascript·数据结构
深蓝海拓2 小时前
PySide6从0开始学习的笔记(二十七) 日志管理
笔记·python·学习·pyqt
xqqxqxxq2 小时前
Java Thread 类核心技术笔记
java·笔记
Byron07072 小时前
从 0 到 1 搭建 Vue 前端工程化体系:提效、提质、降本实战落地
前端·javascript·vue.js
慎独4132 小时前
科学赋能,让孩子专注高效爱上学习
学习
LGL6030A2 小时前
Java学习历程26——线程安全
java·开发语言·学习
老师用之于民2 小时前
【DAY21】Linux软件编程基础&Shell 命令、脚本及系统管理实操
linux·运维·chrome·经验分享·笔记·ubuntu