Echarts 数据在x轴下方展示

在这里插入图片描述

new Vue({
  el: "#app",
  data() {
    return {
      comName: ['公司1', '公司2', '公司3', '公司4', '公司5', '公司6'],
      avgPriceRate: [-1.7, -2.8, -3.9, -6.5, -0.8, 6.6], // 均价涨幅 单位%
      giveInterestPrice: [55555.5, 66666.6, 44444.4, 33333.3, 8767.1, 15432.1], // 让利金额 单位万元
    }
  },
  created() {
  },
  mounted() {
    this.initChart('Echarts 数据在x轴下方展示','chart', this.comName, this.avgPriceRate, this.giveInterestPrice);
  },
  methods: {
    getTableLine(num, idName) {
      let width = $(`#${idName}`).width();
      let list = [];
      let bottom = 125;
      let height = 31;
      list.push({
        type: 'line',
        bottom: 63,
        left: 120,
        style: {
          fill: '#333'
        },
        shape: {
          x1: 0,
          y1: 95,
          x2: 0,
          y2: 0
        }
      });
      for (let i = 0; i < num; i++) {
        list.push({
          type: 'line',
          bottom: bottom - i * height,
          right: 80,
          style: {
            fill: '#333'
          },
          shape: {
            x1: 0,
            y1: 0,
            x2: width * 0.92,
            y2: 0
          }
        });
      }
      return list;
    },
    initChart(name, idName, xData, yData1, yData2) {
      let lineList = this.getTableLine(3, idName);
      let option = {
        title: [{
          text: name,
          left: 'center',
          textStyle: {
            lineHeight: 20,
            fontSize: 25,
          },
        }],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#283b56'
            }
          }
        },
        legend: {
          left: 25,
          bottom: 68,
          orient:'vertical',
          itemGap: 15,
          data: ['让利金额', '均价涨幅']
        },
        grid: {
          bottom: 160,
          left: 120,
          right: 80,
        },
        xAxis: [{
          type: 'category',
          boundaryGap: true,
          data: xData,
          axisTick: {
            length: 96
          },
          axisLabel: {
            interval:0,
            rotate: 0,
            formatter(value, index) {
              return `{table|${value}}\n{table|${yData2[index]}}\n{table|${yData1[index]}%}`;
            },
            rich: {
              table: {
                lineHeight: 30,
                align: 'center',
              }
            }
          }
        }],
        yAxis: [{
          type: 'value',
          name: '万元',
          splitLine: {
            show: true
          },
          splitNumber: 3,
        },
          {
            type: 'value',
            name: '%',
            splitLine: {
              show: true
            },
            axisLabel: {
                formatter: '{value} %'
            },
            splitNumber: 3,
          }],
        series: [{
          name: '让利金额',
          yAxisIndex: 0,
          type: 'bar',
          symbol:'rect',
          symbolSize:'8',
          data: yData2,
          barWidth: 30,
          itemStyle: {
            normal: {
              barBorderRadius: [10, 10, 10, 10],
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: 'rgb(74, 122, 180)'
              }, {
                offset: 1,
                color: 'rgb(74, 122, 180)'
              }]),
            }
          },
        }, {
          name: '均价涨幅',
          yAxisIndex: 1,
          type: 'line',
          symbol:'rect',
          symbolSize:'8',
          data: yData1,
          itemStyle: {
            normal: {
              color: 'rgb(161, 180, 212)',
              lineStyle: {
                width: 3,
                type: 'dotted',
              }
            }
          }
        }],
        graphic: lineList
      };


      let chart = document.getElementById(idName);
      echarts.init(chart).setOption(option);
    },
  }
})

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 ECharts 的 X 线上添加小色块,可以通过在 X 上添加自定义组件的方式来实现。具体的步骤如下: 1. 在 ECharts 的 option 中的 xAxis 中添加一个 type 为 'category' 的 X ,并设置 boundaryGap 为 false,使得坐标从头开始。 2. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X ,并设置 boundaryGap 为 false,使得坐标从头开始。 3. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X ,并设置 boundaryGap 为 false,使得坐标从头开始。 4. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X ,并设置 boundaryGap 为 false,使得坐标从头开始。 5. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X ,并设置 boundaryGap 为 false,使得坐标从头开始。 6. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X ,并设置 boundaryGap 为 false,使得坐标从头开始。 7. 在 option 中的 xAxis 中添加一个 type 为 'category' 的 X ,并设置 boundaryGap 为 false,使得坐标从头开始。 8. 使用 ECharts 提供的 graphic 组件,根据条件来绘制小色块,可以使用 rect、circle 等基础形状。 9. 将绘制好的小色块添加到 X 的 axisLine 上,使用 offset 属性来控制小色块的位置。 示例如下: ``` option = { xAxis: { type: 'category', boundaryGap: false, axisLine: { onZero: false, lineStyle: { color: '#333' }, // 添加自定义组件 // 绘制小色块 // 将小色块添加到 axisLine 上 // 控制小色块的位置 // 添加多个小色块,根据条件设置不同的颜色和位置 // 示例代码如下: graphic: [{ type: 'rect', position: [100, 0], shape: { width: 10, height: 10 }, style: { fill: '#ff0000' }, // 控制小色块的位置 // 可以使用 offset 属性 // offset: [0, 10] // 在 X 上方,向下偏移 10 像素 // offset: [0, -10] // 在 X 下方,向上偏移 10 像素 // offset: [10, 0] // 在 X 左侧,向右偏移 10 像素 offset: [-10, 0] // 在 X 右侧,向左偏移 10 像素 }] }, // X 数据 data: ['A', 'B', 'C', 'D', 'E', 'F', 'G'] }, yAxis: { type: 'value' }, series: [{ data: [10, 20, 30, 40, 50, 60, 70], type: 'line' }] }; ``` 这样就可以在 X 线上添加小色块了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值