react+echart選中+雙擊恢復(fù)

在項目開發(fā)中經(jīng)常會用到各種圖表,會對圖表進行一些操作,此dome以Echarts為例

// charts.tsx
import React, { FC, useRef, useMemo, useState, useEffect } from 'react'
import * as echarts from 'echarts'
import { useMount, useUpdateEffect } from 'ahooks'

export const Charts:FC<any> = (props) => {
  const chartContainer = useRef<HTMLDivElement>(null)
  const [myChart, setMyChart] = useState<echarts.ECharts | null>(null)
  
  const defaultOptions = useMemo(
    () => ({
      tooltip: {
        trigger: 'axis',
      },
      color:['red','blue'],
      legend: {
        data: [],
      },
      grid: {
        top: '30px',
        left: '40px',
        right: '15px',
        bottom: '30px',
        containLabel: true,  // 邊距自適應(yīng)
      },
      xAxis: {
        type: 'time',
        boundaryGap: false,
      },
      yAxis: {
        type: 'value',
      },
      series: [{
          type: 'line',
          data: [1,2,3,4,5,6],
          sampling: 'lttb', // 數(shù)據(jù)采集,大數(shù)據(jù)量需要配置
          connectNulls: true, //斷線連接
          emphasis: {
              focus: 'series',
            }
          }]
    }), [])

  useMount(() => {
    const echart = echarts.init(chartContainer.current!)
    setMyChart(echart)
  })

// 獲取xy軸最大最小值
useEffect(() => {
        if (!myChart) return
        const chartInstance = echarts.getInstanceByDom(ChartRef.current!) as any
        setTimeout(() => {
            const yAxisExtent = chartInstance.getModel().getComponent('yAxis').axis.scale._extent;
            const xAxisExtent = chartInstance.getModel().getComponent('xAxis').axis.scale._extent;
            console.log('====================================');
            console.log(xAxisExtent, yAxisExtent);
            console.log('====================================');
        }, 0);
    }, [myChart])

  // 設(shè)置 選中chart區(qū)域 展示詳情 + 雙擊恢復(fù)
  useEffect(() => {
    if (myChart) {
      myChart.setOption({
        toolbox: {
          feature: {
            dataZoom: {
              show: true,
              iconStyle: {
                opacity: 0,
              },
              title: {
                zoom: '',
                back: ''
              },
              yAxisIndex: 'none',
              brushStyle: {
                color: '#1890ff',
                opacity: 0.2
              },
            },
          },
        },
      });

      // 獲取x軸位置
      myChart.getZr().on('mousemove', (params) => {
            const pointInPixel = [params.offsetX, params.offsetY]
            if (myChart.containPixel('grid', pointInPixel)) {
                const pointInGrid = myChart.convertFromPixel({
                    seriesIndex: 0
                }, pointInPixel)
                const xIndex = pointInGrid[0]
                const data = myChart.getOption() as any
                console.log(data.xAxis[0].data[Number(xIndex)])
            }
       })
      // echarts畫布縮放
      myChart.dispatchAction({
        type: 'takeGlobalCursor',
        key: 'dataZoomSelect',
        dataZoomSelectActive: true,
      });
      // 雙擊恢復(fù)
      myChart.getZr().on('dblclick', () => {
        myChart.dispatchAction({
          type: 'dataZoom',
          // startValue: 0,
          // endValue: 100,
          start: 0,
          end: 100
        });
      });
    }
  },[])

  return <div ref="chartContainer"></div>
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,041評論 2 59
  • 寫在前面的話第一次接觸到大屏可視化的時候,毫無頭緒,習(xí)慣性打開簡書和掘金,在上面搜索優(yōu)秀的大佬們的解決方案,看過多...
    原始l閱讀 6,518評論 0 2
  • 關(guān)于Mongodb的全面總結(jié) MongoDB的內(nèi)部構(gòu)造《MongoDB The Definitive Guide》...
    中v中閱讀 32,311評論 2 89
  • 這是之前接觸過的圖標(biāo)庫,最近項目又重新用到關(guān)于圖表方面。由于之前沒有整理出,單獨的相關(guān)內(nèi)容,每次需要重新查閱,所以...
    EasyZ閱讀 284評論 0 0
  • 在我們的日常開發(fā)中,我們經(jīng)常需要引入各種各樣的第三方模塊來幫助我們提升開發(fā)速度。但是有時候這些模塊里面又包含了許多...
    西麥smile閱讀 1,531評論 0 1

友情鏈接更多精彩內(nèi)容