React中echarts封装,包括图例、图表的点击事件、控制tooltip显隐

1、基于echarts,在react中的封装,包括图例的取消/选中事件监听、图表的点击事件、控制显示tooltip
2、事件相关代码可根据需求,需要交互的可以直接用,只渲染图的话可以删除
3、使用时传入对应图表的option,option可根据echarts官方文档配置

有问题或者需要其他扩展的可以私信

1、echarts封装

import React, { useCallback, useEffect, useRef } from 'react';
// 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口。
import * as echarts from 'echarts/core';
// 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from 'echarts/renderers';
// 设备分辨率
const dpr = window.devicePixelRatio;

interface Props {
  options: any;
  components: any;
  renderType?: any;
  style?: React.CSSProperties;
}

const Chart = ({ renderType = 'canvas', options, style, components = [] }: Props) => {
  const chartRef = useRef();
  const chartInstance = useRef(null);

  //初始化图表,设置配置项
  const renderChart = useCallback(() => {
    const render = echarts?.getInstanceByDom(chartRef.current);
    if (render) {
      chartInstance.current = render;
    } else {
      chartInstance.current = echarts?.init(chartRef.current, null, {
        renderer: renderType,
      });
    }
    chartInstance.current?.setOption(options);
    // 图例选中/取消选中事件
    chartInstance.current?.on('legendselectchanged', (e: any) => {
      
    });
    // 图表的点击事件
    chartInstance.current?.getZr()?.on('click', (e: any) => {
      const pointInPixel = [e.offsetX, e.offsetY];
      if (chartInstance.current?.containPixel('grid', pointInPixel)) {
        
      }
      // 显示tooltip
      chartInstance.current?.dispatchAction({
        type: 'showTip',
        x: e?.event?.offsetX,
        y: e?.event?.offsetY,
      });
    });
  }, [chartRef, options, renderType]);

  useEffect(() => {
    // 注册必须的组件
    echarts?.use([CanvasRenderer, ...components]);
  }, []);

  //监听屏幕变化,重绘图表
  useEffect(() => {
    window.addEventListener('resize', handleResize);
    return () => {
      window.removeEventListener('resize', handleResize);
    };
  });

  useEffect(() => {
    renderChart();
    return () => {
      const { current } = chartInstance ?? {};
      if (current) {
        current.dispose();
      }
    };
  }, [chartInstance, renderChart]);

  const handleResize = () => {
    const chart = chartInstance?.current;
    if (chart) {
      chart.resize();
    }
  };

  return (
    <div
      ref={chartRef}
      style={{
        height: `${260 * dpr}px`,
        width: `calc(100vw - .32rem)`,
        ...style,
      }}
    />
  );
};

export default Chart;

2、使用(下边是仪表盘图表配置)

import React from 'react';
// 引入柱状图图表,图表后缀都为 Chart
import { GaugeChart } from 'echarts/charts';
// 引入封装好的组件
import Chart from '../Chart';

import styles from './index.less';

const getOption = (progress) => {
  return {
    series: {
      type: 'gauge',
      radius: '160%',
      center: ['50%', '100%'],
      startAngle: 180,
      endAngle: 0,
      min: 0,
      max: 100,
      splitNumber: 12,
      itemStyle: {
        color: {
          type: 'linear',
          x: 1,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: '#A6746B',
            },
            {
              offset: 1,
              color: '#DAABA2',
            },
          ],
        },
      },
      progress: {
        show: true,
        width: 30,
      },

      pointer: {
        show: false,
      },
      axisLine: {
        lineStyle: {
          width: 30,
          color: [[1, '#ddd']],
        },
      },
      axisTick: {
        show: false,
      },
      splitLine: {
        show: false,
      },
      axisLabel: {
        show: false,
      },
      anchor: {
        show: false,
      },
      title: {
        show: false,
      },
      detail: {
        valueAnimation: true,
        width: '60%',
        lineHeight: 40,
        height: '15%',
        borderRadius: 8,
        offsetCenter: [0, '-15%'],
        fontSize: 72,
        fontWeight: 'bolder',
        formatter: '{value}%',
        color: '#373737',
      },
      data: [
        {
          value: progress,
        },
      ],
    },
  };
};

export default ({ progress }) => {
  return (
    <Chart
        style={{ height: '40vw' }}
        options={getOption(progress)}
        components={[GaugeChart]}
    />
  );
};

3、效果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
进度.jpg

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
ECharts是一个基于JavaScript的开源可视化库,它提供了各种类型的图表,如折线图、柱状图、饼图等等。实现多个折线图和柱状图混合的方法如下: 1. 定义一个包含多个数据系列的option对象,每个数据系列对应一个折线图或柱状图。 2. 在option对象定义x轴和y轴的数据,以及图表的样式、标题等属性。 3. 使用ECharts的实例化对象将option对象传入,并将图表渲染到指定的DOM元素。 以下是一个示例代码,实现了两个折线图和一个柱状图混合显示: ```javascript // 定义数据 var data1 = [10, 20, 30, 40, 50, 60]; var data2 = [15, 25, 35, 45, 55, 65]; var data3 = [8, 18, 28, 38, 48, 58]; // 定义option对象 var option = { legend: { data: ['折线图1', '折线图2', '柱状图'] }, tooltip: { trigger: 'axis' }, xAxis: { type: 'category', data: ['周一', '周二', '周三', '周四', '周五', '周六'] }, yAxis: [ { type: 'value', name: '折线图1', axisLabel: { formatter: '{value} °C' } }, { type: 'value', name: '折线图2', axisLabel: { formatter: '{value} %' } }, { type: 'value', name: '柱状图', axisLabel: { formatter: '{value} 个' } } ], series: [ { name: '折线图1', type: 'line', data: data1 }, { name: '折线图2', type: 'line', yAxisIndex: 1, data: data2 }, { name: '柱状图', type: 'bar', yAxisIndex: 2, data: data3 } ] }; // 实例化ECharts对象 var myChart = echarts.init(document.getElementById('chart')); // 渲染图表 myChart.setOption(option); ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凉城┓.〆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值