【金融大屏项目】—— Echarts 极坐标和柱状图实现圆角环形图(polar)

ProgressCircleChart组件代码:

import React, { PureComponent } from 'react';
import echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/pie';
import 'echarts/lib/chart/bar';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import 'echarts/lib/component/polar';
import styles from './index.less';

class ProgressCircleChart extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    const { data } = this.props;
    this.initProgressCircleChart(data);
    // 窗口改变重新加装定时器并重绘echarts
    window.addEventListener('resize', () => {
      this.initProgressCircleChart(data);
    });
  }

  componentDidUpdate() {
    const { data } = this.props;
    // 数据改变重绘echarts
    this.initProgressCircleChart(data);
    // 窗口改变重新加装定时器并重绘echarts
    window.addEventListener('resize', () => {
      this.initProgressCircleChart(data);
    });
  }

  initProgressCircleChart = (data) => {
    if (data.length > 0) {
      const { id, radius, center, color } = this.props;
      const myChart = echarts.init(document.getElementById(id));
      const option = {
        color: color,
        // tooltip: {
        //   trigger: 'item'
        // },
        grid: {
          left: 0,
          right: 0,
          top: 0,
          bottom: 0,
          containLabel: true
        },
        angleAxis: {
          max: data[1], // 总数
          clockwise: false, // 逆时针
          // 隐藏刻度线
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: false
          },
          splitLine: {
            show: false
          }
        },
        radiusAxis: {
          type: 'category',
          // 隐藏刻度线
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: false
          },
          splitLine: {
            show: false
          }
        },
        polar: {
          center: center,
          radius: radius //图形大小
        },
        series: [
          // {
          //   name: '',
          //   type: 'pie',
          //   radius: radius,
          //   center: center,
          //   avoidLabelOverlap: true,
          //   hoverAnimation:false, //悬停不放大突出
          //   labelLine: {
          //     normal: {
          //       show: false
          //     }
          //   },
          //   // minAngle: 15,//最小角度
          //   startAngle:230, //起始角度
          //   data: data
          // }
          {
            type: 'bar',
            data: [{
              name: '',
              value: data[0],
              itemStyle: {
                normal: {
                  color: color[0]
                },
              }
            }],
            coordinateSystem: 'polar',
            roundCap: true,
            barWidth: 8,
            barGap: '-100%', // 两环重叠
            z: 2,
          },{ // 灰色环
            type: 'bar',
            data: [{
              value: data[1],
              itemStyle: {
                color: color[1]
              }
            }],
            coordinateSystem: 'polar',
            roundCap: true,
            barWidth: 8,
            barGap: '-100%', // 两环重叠
            z: 1
          }
        ]
      };
      if(id==='zncChart' || id === 'zcpuChart' || id === 'zcpChart' || id === 'zyxhChart'){
        option.title = {
          show:true,
          text: data[0],
          textAlign:'center',
          x:'45%',
          // y: 22,
          y: '23.91%',
          itemGap: 4,//主副标题纵向间隔,单位px,默认为10
          textStyle: {
            fontSize: 22,
            fontWeight: 400,
            color: '#0AC3E5'
          },
          subtextStyle:{
            fontSize: 16,
            fontWeight: 400,
            color: '#ffffff'
          }
        }
      }
      if(id==='zncChart' || id === 'zcpChart' || id === 'zyxhChart'){
        option.title.subtext = 'TB';
      }
      if(id === 'zcpuChart'){
        option.title.subtext = '核';
      }
      myChart.setOption(option);
    }
  };

  render() {
    const { data, id, width, height } = this.props;
    return (
      <div className={styles.contain}>
        <div className={styles.chart} id={id} style={{ width, height }} />
      </div>
    );
  }
}

export default ProgressCircleChart;

ProgressCircleChart.defaultProps = {
  id: 'progressCircleChart',
  width: '100%',
  height: '100%',
  // radius: [19.5, 33.5],
  radius: ['56.62%', '97.1%'],
  center: ['50%', '50%'],
  color: ['#BF00FE', 'rgba(64,0,208, 0.6)'],
  data: [ 80, 100]
};

less代码:

.contain {
  display: flex;
  align-items: center;
  width: 100%;
  height: 100%;

  .chart {
    width: 100%;
    height: 100%;
  }
}

引用组件代码:

<ProgressCircleChart id='zncChart' color={zncColor} radius={middleProgressRadius} data={zncData}/>

官方 Pull requests https://github.com/apache/incubator-echarts/pull/11393

 


注:转载请注明出处 

posted @ 2022-02-21 14:02  柳洁琼Elena  阅读(915)  评论(0编辑  收藏  举报