react echarts 绘制图表 EchartsLine

7 篇文章 0 订阅
import React, { useRef, useEffect, memo } from 'react';
import * as echarts from 'echarts';
// import echarts from './echarts.js';
//  react echarts 绘制图表 EchartsLine

const EchartsLine:React.FC<unknown> = ( ) => {
  const lineRef = useRef<HTMLDivElement | null>(null);
  useEffect(() => {

    //检测是否已经存在echarts实例,如果不存在,则不再去初始化
    let myChart = echarts.getInstanceByDom(lineRef.current as HTMLDivElement);
    if (myChart == null) {
      myChart = echarts.init(lineRef.current as HTMLDivElement);
    }
    let timer: NodeJS.Timeout | null = null;


    const option = {
      title: {
        bottom: 0,
        right: 'center',
        subtext: '主标题的属性',
        // textStyle: {//主标题的属性
        // },
        subtextStyle: {
          //副标题的属性
          color: '#8C8C8C',
          fontSize: 12, //大小
        },
      },
      tooltip: {
        trigger: 'item',
        formatter: `
                {b}<div >UV&nbsp;<span style='float:right;'>{c}</span><div>
                `,
      },
      dataZoom: [
        {
          bottom: 30,
          height: 8,
          type: 'slider',
          xAxisIndex: 0,
          filterMode: 'none',
        },
        {
          bottom: 30,
          height: 8,
          type: 'inside',
          xAxisIndex: 0,
          filterMode: 'none',
        },
        {
          right: 20,
          width: 8,
          type: 'inside',
          yAxisIndex: 0,
          filterMode: 'none',
        },
        {
          right: 20,
          width: 8,
          type: 'slider',
          yAxisIndex: 0,
          filterMode: 'none',
        },
      ] , // 放大缩小
      grid: {
        top: 20,
        left: '5%',
        right: '8%',
        bottom: 45,
        containLabel: true,
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
        axisLabel: {
          //坐标轴刻度标签的相关设置
          show: true,
          textStyle: {
            color: '#8C8C8C',
            fontSize: 12,
rotate:40, // 文本旋转角度
          },
          lineStyle: {
            color: '#e3e3e3',
            type: 'slider',
          },
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: '#e3e3e3',
          },
        },
        axisTick: [
          {
            //坐标轴小标记
            show: false,
          },
        ],
        // splitLine: {
        //     show: false,
        //     lineStyle: {
        //         color: '#e3e3e3',
        //         type: 'slider'
        //     }
        // },
      },
      yAxis: {
        type: 'value',
        axisLabel: {
          //坐标轴刻度标签的相关设置
          show: true,
          textStyle: {
            color: '#8C8C8C',
            fontSize: 12,
          },
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: '#e3e3e3',
            type: 'slider',
          },
        },
        // max: 1500,
      },
      series: [
        {
          type: 'line',
          smooth: false,
          // symbol: 'circle',
          // sampling: 'average',
          symbolSize: 3,
          itemStyle: {
            color: '#5B8FF9',
          },
          stack: 'a',
          data: [820, 932, 901, 934, 1290, 1330, 1320],
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: '#5B8FF9',
              },
              {
                offset: 1,
                color: '#ffe',
              },
            ]),
          },
        },
      ],
    };
    myChart.setOption(option);
  }, []);

  return <div ref={lineRef} style={{ height: '270px', width: '100%' }} />;
};
export default memo(EchartsLine);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值