vue3 tsx 项目中使用 Antv/G2 实现多线折线图

Antv/G2 文档
Antv/G2 双折线图

安装 antV-G2

通过 npm 安装

项目中安装 antv/g2 依赖库:

javascript 复制代码
npm install @antv/g2 --save

安装成功:

浏览器引入

可以将脚本下载到本地,也可以直接引入在线资源。

引入在线资源

javascript 复制代码
<!-- 引入在线资源,选择需要的 g2 版本以替换 version 变量 -->
<script src="https://gw.alipayobjects.com/os/lib/antv/g2/{{version}}/dist/g2.min.js"></script>
<!-- 浏览器引入,请使用全局命名空间 G2,如 new Chart() 改为 new G2.Chart,即可运行。 -->

引入本地脚本

javascript 复制代码
<!-- 引入本地脚本 -->
<script src="./g2.js"></script>

使用 script 标签引入 G2 资源时,挂载在 window 上的变量名为 G2。所以,实例中需要加上 G2 的前缀。如下:

javascript 复制代码
const chart = new G2.Chart({
})

项目使用

新建文件 IndicatorTrend.tsx:

javascript 复制代码
import { defineComponent, PropType, onMounted, ref } from 'vue'
import { useChartAutoResize } from '@/hooks/chart'
import styled from '@/styled-components'
import { Chart } from '@antv/g2';

export interface TrendListItem {
  date: string
  city: string
  tempvalue: number
}

interface Props {
  dataList?: TrendListItem[]
}

const Container = styled.div`  
  width: 100%;
  height: 100%;
`

const TitleBox = styled.h3`
  margin-bottom: 10px;
`

const ChartContainer = styled.div`
  height:100%;
`

export default defineComponent({
  props: {
    dataList: {
      type: Array as PropType<TrendListItem[]>,
      default: () => []
    }
  },
  setup() {
    const dataList = ref<TrendListItem[]>([])
    const canvasRef = ref<null | HTMLElement>(null)
    const chartRef = ref<null | InstanceType<typeof Chart>>(null)

    onMounted(() => {
      if (canvasRef.value) {
        const chart = new Chart({
          container: canvasRef.value,
          autoFit: true
        })

        chartRef.value = chart
      }

      refreshChartView()
    })

    useChartAutoResize(canvasRef, chartRef)

    function refreshChartView(){      
      const chart: any = chartRef.value
      chart.clear()
      setTimeout(() => {
        chart.data(dataList.value)
        
        chart.scale({
          date: {
            range: [0, 1],
          },
          tempvalue: {
            nice: true,
          },
        });
        
        chart.tooltip({
          showCrosshairs: true,
          shared: true,
        });
        
        chart.axis('tempvalue', {
          label: {
            formatter: (val:number) => {
              return val + ' °C';
            },
          },
        });
        
        chart
          .line()
          .position('date*tempvalue')
          .color('city')
          .shape('smooth');
        
        chart
          .point()
          .position('date*tempvalue')
          .color('city')
          .shape('circle')
          .style({
            stroke: '#fff',
            lineWidth: 1,
          });
        
        chart.render()
      })
    }

    return (props: Props) => {
      dataList.value = props.dataList || []
      return (
        <Container>
          <TitleBox>总趋势</TitleBox> 
          <ChartContainer>      
          <div ref={canvasRef} />     
          </ChartContainer>   
        </Container>
      )
    }
  }
})

其中,引用了公共方法 hooks/chart:

javascript 复制代码
import { ref, onUnmounted, watchEffect } from 'vue'
import { changeSizeAfterCanvasResize, deleteGlobalChartItem } from '@/utils/chart'

// 没找到ref的类型
export const useChartAutoResize = (canvasRef: any, chartRef: any): void => {
  const queueIndex = ref<number>(-1)
  const isCreated = ref(false)

  function clearThisChart() {
    queueIndex.value > 0 && deleteGlobalChartItem(queueIndex.value)
  }

  // 后续如果需要重复绑定,可以返回一个更新的方法
  watchEffect(() => {
    if (!isCreated.value && canvasRef.value && chartRef.value) {
      clearThisChart()
      isCreated.value = true
      queueIndex.value = changeSizeAfterCanvasResize(canvasRef.value, chartRef.value)
    }
  }, {
    flush: 'post'
  })

  onUnmounted(() => {
    clearThisChart()
  })
}

utils/chart 文件:

javascript 复制代码
import { Chart } from '@antv/g2'
import { ChartResizeQueueItem } from '@/globalType'

const getChartIndex: () => number = createChartIndex()

export function getGlobalChartQueue(): ChartResizeQueueItem[] {
  return window.chartResizeQueue
}

export function setGlobalChartQueue(arr: ChartResizeQueueItem[]): ChartResizeQueueItem[] {
  window.chartResizeQueue = arr
  return window.chartResizeQueue
}

export function deleteGlobalChartItem(index: number): void {
  const queue = getGlobalChartQueue()
  setGlobalChartQueue(queue.filter(item => item.index !== index))
}

// canvas适应父元素的大小,并刷新图表宽高
export function refreshChartSize(canvas: HTMLElement, chart: Chart): void {
  let width: number = 0
  let height: number = 0

  if (canvas.parentNode && getComputedStyle) {
    const styles = getComputedStyle(canvas.parentNode as HTMLElement)
    width = Number(styles.width.split('px')[0])
    height = Number(styles.height.split('px')[0])
  } else if (canvas.parentNode) {
    width = (canvas.parentNode as HTMLElement).offsetWidth
    height = (canvas.parentNode as HTMLElement).offsetHeight
  }

  canvas.setAttribute('width', `${width}px`)
  canvas.setAttribute('height', `${height}px`)
  chart.changeSize(width, height)
}

// 添加到全局图表队列,并且自动更新宽高
export function changeSizeAfterCanvasResize(canvas: HTMLElement, chart: InstanceType<typeof Chart>): number {
  const queue = getGlobalChartQueue()
  const index: number = getChartIndex()

  refreshChartSize(canvas, chart)
  setGlobalChartQueue(queue.concat([{ index, canvas, chart }]))

  return index
}

function createChartIndex() {
  let index: number = 0

  return (): number => {
    index++
    return index
  }
}

globalType.ts 文件:

javascript 复制代码
export interface ChartResizeQueueItem {
  index: number
  canvas: HTMLElement,
  chart: any
}

在父组件中引用 IndicatorTrend.tsx 组件:

javascript 复制代码
<IndicatorTrend dataList={totalTrendList.value}></IndicatorTrend>         

数据源为:

javascript 复制代码
totalTrendList.value = [{
        date: '2023/8/1',
        city: 'bily',
        tempvalue: 4623
      }, {
        date: '2023/8/1',
        city: 'cily',
        tempvalue: 2208
      }, {
        date: '2023/8/1',
        city: 'bill',
        tempvalue: 182
      }, {
        date: '2023/8/2',
        city: 'bily',
        tempvalue: 6145
      }, {
        date: '2023/8/2',
        city: 'cily',
        tempvalue: 2016
      }, {
        date: '2023/8/2',
        city: 'bill',
        tempvalue: 257
      }, {
        date: '2023/8/3',
        city: 'bily',
      
        tempvalue: 508
      }, {
        date: '2023/8/3',
        city: 'cily',
        tempvalue: 2916
      }, {
        date: '2023/8/3',
        city: 'bill',
        tempvalue: 289
      }, {
      
        date: '2023/8/4',
        city: 'bily',
        tempvalue: 6268
      }, {
        date: '2023/8/4',
        city: 'cily',
        tempvalue: 4512
      }, {
        date: '2023/8/4',
        city: 'bill',
        tempvalue: 428
      }, {
        date: '2023/8/5',
        city: 'bily',
        tempvalue: 6411
      }, {
        date: '2023/8/5',
        city: 'cily',
        tempvalue: 8281
      }, {
        date: '2023/8/5',
        city: 'bill',
        tempvalue: 619
      }, {
        date: '2023/8/6',
        city: 'bily',
        tempvalue: 1890
      }, {
      
        date: '2023/8/6',
        city: 'cily',
        tempvalue: 2008
      }, {
        date: '2023/8/6',
        city: 'bill',
        tempvalue: 87
      }, {
        date: '2023/8/7',
        city: 'bily',
        tempvalue: 4251
      }, {
        date: '2023/8/7',
        city: 'cily',
        tempvalue: 1963
      }, {
        date: '2023/8/7',
        city: 'bill',
        tempvalue: 706
      }, {
        date: '2023/8/8',
        city: 'bily',
        tempvalue: 2978
      }, {
        date: '2023/8/8',
        city: 'cily',
        tempvalue: 2367
      }, {
      
        date: '2023/8/8',
        city: 'bill',
        tempvalue: 387
      }, {
        date: '2023/8/9',
        city: 'bily',
        tempvalue: 3880
      }, {
        date: '2023/8/9',
        city: 'cily',
        tempvalue: 2956
      }, {
        date: '2023/8/9',
        city: 'bill',
        tempvalue: 488
      }, {
        date: '2023/8/10',
        city: 'bily',
      
        tempvalue: 3606
      }, {
        date: '2023/8/10',
        city: 'cily',
        tempvalue: 678
      }, {
        date: '2023/8/10',
        city: 'bill',
        tempvalue: 507
      }, {
      
        date: '2023/8/11',
        city: 'bily',
        tempvalue: 4311
      }, {
        date: '2023/8/11',
        city: 'cily',
        tempvalue: 3188
      }, {
        date: '2023/8/11',
        city: 'bill',
        tempvalue: 548
      }, {
        date: '2023/8/12',
        city: 'bily',
        tempvalue: 4116
      }, {
        date: '2023/8/12',
        city: 'cily',
        tempvalue: 3491
      }, {
        date: '2023/8/12',
        city: 'bill',
        tempvalue: 456
      }, {
        date: '2023/8/13',
        city: 'bily',
        tempvalue: 6419
      }, {
      
        date: '2023/8/13',
        city: 'cily',
        tempvalue: 2852
      }, {
        date: '2023/8/13',
        city: 'bill',
        tempvalue: 689
      }, {
        date: '2023/8/14',
      
        city: 'bily',
        tempvalue: 1643
      }, {
        date: '2023/8/14',
        city: 'cily',
        tempvalue: 4788
      }, {
        date: '2023/8/14',
        city: 'bill',
        tempvalue: 280
      }, {
        date: '2023/8/15',
        city: 'bily',
        tempvalue: 445
      }, {
        date: '2023/8/15',
        city: 'cily',
        tempvalue: 4319
      }, {
      
        date: '2023/8/15',
        city: 'bill',
        tempvalue: 176
      }]

页面效果:

相关推荐
北城笑笑10 小时前
Vue 112 ,下篇:前端 dist 还原成 Vue3 + Vite 项目( krpano 全景接入与页面适配实战,Vue2 → Vue3 )
前端·javascript·vue.js·3d·前端框架·vue·webgl
我叫黑大帅10 小时前
搜“张三”把备注也带出来了?不用加高级筛选,如何让一个搜索框实现「选项直达」!
前端·javascript·面试
如夏倾城12 小时前
WAF (Web Application Firewall,Web应用防火墙)
前端·网络·安全·web安全·web app
图扑可视化13 小时前
基于图扑软件智慧制硅厂 Web SCADA 开发实践
前端·三维可视化·scada·hmi·组态软件
前端fighter14 小时前
2026年前端面试题总结(pdf版)
前端·pdf
谢小飞14 小时前
运维加了个参数,我们的网页挂掉了
前端·浏览器
CoderYanger15 小时前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis
用户693717500138417 小时前
大模型蒸馏到底在蒸什么?一篇文章搞懂知识蒸馏概念
前端·后端·github
徐小夕17 小时前
实测 10 个前端 UI 生成开源 Skill|AI 输出直达上线标准
前端·vue.js·github
tong_li17 小时前
给娃做个 AI 陪写作业助手:LangChain4j 入门(八):AI 知道这字哪课学的(SQLite 结构化查询入门)
前端·langchain