echarts:绘制每个单元格的标记线

本文介绍如何使用ECharts在柱状图中实现每个单元格的标记线,详细阐述了如何绘制标记线以突出显示每个单元格的最小值。

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

需求

用echarts绘制每个单元格的标记线,而非整个图表的标记线


实现

柱状图:绘制标记线,标记每个单元格的最小值

//法一:
var data1 = [18203, 23489, 29034, 44970, 31744, 30230];
var data2 = [19325, 23438, 31000, 21594, 54141, 81807];
var dataMin = [];
for (var i = 0; i < data1.length; ++i) {
  dataMin.push(Math.min(data1[i], data2[i]));
}

option = {
  title: {
    text: 'World Population'
  },
  tooltip: {
    trigger: 'item',
    axisPointer: {
      type: 'shadow'
    }
  },
  legend: {},
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  yAxis: {
    type: 'value',
    boundaryGap: [0, 0.01]
  },
  xAxis: [
    {
      type: 'category',
      data: ['Brazil', 'Indonesia', 'USA', 'India', 'China', 'World']
    },
    {
      type: 'category',
      data: ['Brazil', 'Indonesia', 'USA', 'India', 'China', 'World'],
      position: 'bottom'
    }
  ],
  series: [
    {
      name: '2011',
      type: 'bar',
      data: data1
    },
    {
      name: '2012',
      type: 'bar',
      data: data2
    },

    {
      name: '',
      type: 'bar',
      data: dataMin,
      xAxisIndex: 1,
      stack: 'a',
      itemStyle: {
        color: 'transparent'
      },
      barGap: '-100%',// 关键
      barCategoryGap: '10%', // this controls the width of the bar
      tooltip: {
        show: false
      },
      z: -1
    },
    {
      name: '标记线',
      type: 'bar',
      data: [0, 0, 0, 0, 0, 0],
      xAxisIndex: 1,
      stack: 'a',
      barMinHeight: 2, // this controls the height of the bar
      itemStyle: {
        color: 'red'
      },
      tooltip: {
        trigger: 'item',
        formatter: '{b}: {c}'
      }
    }
  ]
};
// 法二
var data1 = [18203, 23489, 29034, 44970, 31744, 30230];
var data2 = [19325, 23438, 31000, 21594, 54141, 81807];
var dataMin = [];
for (var i = 0; i < data1.length; ++i) {
  dataMin.push(Math.min(data1[i], data2[i]));
}

option = {
  title: {
    text: 'World Population'
  },
  tooltip: {
    trigger: 'item',
    axisPointer: {
      type: 'shadow'
    }
  },
  legend: {},
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  yAxis: {
    type: 'value',
    boundaryGap: [0, 0.01]
  },
  xAxis: {
      type: 'category',
      data: ['Brazil', 'Indonesia', 'USA', 'India', 'China', 'World']
    },
  series: [
    {
      name: '2011',
      type: 'bar',
      data: data1
    },
    {
      name: '2012',
      type: 'bar',
      data: data2
    },
    {
      data: dataMin,
      name: '标记线',
      type: 'pictorialBar',
      barGap: '-100%',// 关键
      barCategoryGap: '10%', 
      symbolSize: ['100%', 2],
      symbolRepeat: false,
      symbol: 'rect',
      symbolPosition: 'end',
      itemStyle: {
        color: '#f00',
      },
      z: 3
    }
  ]
};

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值