ReactEcharts折线图缩放与时间绑定

本文介绍了如何在React中使用Echarts实现可拖动的折线图,并与旭日图联动。当点击折线图上的点时,旭日图会同步更新。关键代码包括设置`dataZoom`属性以实现时间轴缩放,以及添加`onEvents`监听点击事件以更新旭日图数据。推荐查阅Echarts官方文档以获取更多详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这两天在做ehcarts图标的时候遇到了一些问题,总结一下~~

需求:可拖动的折线图与旭日图进行联动,点击折线图折点,旭日图同步更新,

好了直接上代码

const lineEchartsOption = (params, yAxis) => ({

    title: {

      text: '',

      textStyle: {

        color: '#666C73',

        fontSize: 18,

      },

      left: 'center',

    },

    tooltip: {

      trigger: 'axis' as 'axis' | 'none' | 'item' | undefined,

    },

    grid: {

      left: 0,

      right: '4%',

      bottom: '50px',    // 使用dataZoom的时候自己调试一下时间轴的位置

      top: 30,

      // top: 30,

      containLabel: true,

    },

    xAxis: {

      type: 'category' as 'category' | 'value' | 'time' | 'log' | undefined,

      // boundaryGap: false,

      data: params?.xAxis,

      axisLine: {

        lineStyle: {

          color: '#ACACAC',

        },

      },

    },

    yAxis: {

      name: yAxis,

      type: 'value' as 'category' | 'value' | 'time' | 'log' | undefined,

      axisLine: {

        lineStyle: {

          color: '#ACACAC',

        },

      },

    },

// 可拖拽的时间轴,简单配置

    dataZoom: [

      {

        type: 'slider',

        start: 80,

        end: 100,

        // top: 20

      }, 

      {

        start: 0,

        end: 100,

      }

    ],

    series: [

      {

        name: yAxis,

        type: 'line',

        stack: yAxis,

        data: yAxis.includes('(%)')

          ? params?.series?.map((item) => item * 100 || item)

          : params?.series,

        itemStyle: {

          normal: {

            color: '#FFD478',

            lineStyle: {

              color: '#FFD478',

            },

          },

        },

      },

    ],

  });

const onEvents = {

    'click': (e: any) => {

        // 自己从e里边取数据请求旭日图数据

    }

}

 

<ReactEcharts

     showLoading={!datasourceLine}

     onEvents={onEvents}   // 绑定点击事件

     option={lineEchartsOption(

          {

                xAxis: dataSourceLine?.xAxis || [],

                series: dataSourceLine?.series || [],

            },

           '交易量(笔)',

          )}

         theme="Imooc"

         style={{ height: '300px', width: '100%' }}

 />

这里其实就很简单,使用dataZoom,echarts官网里都有,可以去官网看一下

echarts官网: https://echarts.baidu.com/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值