使用echarts遇到一些问题总结(饼图,折线图)

1、折线图配置

getLineOption() {
    const { dayDataList, averageCompletedQty } = this.props.dataList;
    const dateArr = dayDataList.map(item =>
      moment()
        .add(item.dayNum - 7, 'days')
        .format('MM.DD')
    );
    const qtyArr = dayDataList.map(item => item.completedQty);
    const averageArr = dayDataList.map(() => averageCompletedQty);
    const option = {
      tooltip: {
        trigger: 'axis',
      },
      color: ['#2db7f5', '#ff6600'], // 设置折线图的线条颜色,设置后线条和头部legend颜色一致
      legend: {
        textStyle: {
          color: '#fff',
        },
        icon: 'rectangle',
        data: ['完成数', '平均数'],
      },
      grid: {
        left: '0',
        top: '10%',
        bottom: '0',
        containLabel: true,
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: dateArr,
        axisLabel: {
          color: '#fff',
        },
        splitLine: {
          show: false, // 去除X轴网格
        },
        axisLine: {
          lineStyle: {
            color: '#fff',
          },
        },
      },
      yAxis: {
        type: 'value',
        axisLabel: {
          color: '#fff',
        },
        splitLine: {
          show: false, // 去除y轴网格
        },
        axisLine: {
          lineStyle: {
            color: '#fff',
          },
        },
      },
      series: [
        {
          name: '完成数',
          type: 'line',
          stack: '总量',
          data: qtyArr,
          smooth: true,
          // markLine: {    // 平均值配置
          //   silent: false,
          //   type: 'line',
          //   symbol: 'none',
          //   label: {
          //     show: true,
          //   },
          //   lineStyle: {
          //     normal: {
          //       type: 'solid',
          //       color: 'rgb(245, 166, 35)',
          //     },
          //   },
          //   data: [
          //     {
          //       yAxis: averageCompletedQty,
          //       name: 'target',
          //     },
          //   ],
          // },
        },
        {  // 如果觉得自带的平均值不好看,可以自己再整一条折线,前提是要去除掉默认堆叠
          name: '平均数',
          type: 'line',
          lineStyle: {
            normal: {
              type: 'solid',
              color: '#ff6600',
            },
          },
          // stack: '总量', // 删掉stack防止两条线堆叠,影响平均数显示
          data: averageArr,
          smooth: true,
        },
      ],
    };
    return option;
  }

我在使用折线图时候遇到,自定义平均值,不是显示一条直线,而是在原有线上进行变化,这不是我们想要的,查询资料原来是由于多条线时候,默认数据会堆叠,因此去除数据的堆叠,去除堆叠方法很简单就是将两条线的stack保持不一致即可,或者去除stack。

效果如下图所示:

2、百度echarts饼图的legend配置显示百分比

最终效果图:

legend: {
        orient: 'vertical',
        text: `{a|${totalQty}}\n{b| 质量总数}`,
        right: 30,
        top: 20,
        bottom: 20,
        formatter: name => {
          let index = 0;
          const clientlabels = ['已完成', '待检验']; // 数组里的名字要与series中data中name值保持一致
          const clientcounts = [Number(completedQty), Number(pendingQty)]; // 各自的数量
          let total = 0;
          for (let j = 0; j < clientcounts.length; j++) {
            total += clientcounts[j];
          }
          const occupancyOne = parseFloat(
            (parseInt((clientcounts[0] * 100000) / total, 10) / 100000) * 100
          ).toFixed(2);
          const occupancyTwo = parseFloat(100 - occupancyOne).toFixed(2);
          const occupancy = [occupancyOne, occupancyTwo];
          clientlabels.forEach((value, i) => {
            if (value === name) {
              index = i;
            }
          });
          return `${name} ${clientcounts[index]} (${occupancy[index]}%)`;
        },
        textStyle: {
          color: '#fff',
        },
      },

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值