echart-条形图各个属性详解/bar渐变色/根据value设置bar的颜色/隐藏刻度线/给每一个bar添加阴影/tooltip自定义样式

echart-条形图各个属性详解 文末有对应的图

重点内容
  1. grid.containLabel
  2. tooltip 自定义样式,(添加class)
  3. 给每一个bar添加背景阴影
  4. bar渐变颜色显示:echarts.graphic.LinearGradient

代码分解

  • bar横条的渐变效果:itemStyle中设置color
    • color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0,color: '#EC8860'}, {offset: 1,color: '#EC4D4B'}])
      在这里插入图片描述
  • 根据数据大小,添加判断,设置bar的颜色
{
  name: '职位',
  type: 'bar',
  barCategoryGap: '25',
  data: [15203, 13489, 9034, 10497, 13174, 1230, 12589, 4000, 10000, 1000],
  itemStyle:{
    normal:{
      color: function(params) {
        var index_value = params.value;
        if(index_value > 15000){
          return new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0,color: '#EC8860'}, {offset: 1,color: '#EC4D4B'}]) // 颜色渐变显示
        }else{
          return new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0,color: '#53D1D2'}, {offset: 1,color: '#02E4CF'}])
        }
      }
    }
  }
}

完整效果

在这里插入图片描述

完整代码

horibarOption:{
        grid: { // 图表在整个canvas画布上的位置
          left: '0px',  // 上下左右间距是String类型,可以是百分比或px
          right: '20px',
          bottom: '3%',
          containLabel: true // 是否包含坐标文字,默认是false,改为true,left、right、top、bottom改变的是包括坐标轴标签在内的所有内容形成的矩形在画布上的位置,可以防止图表溢出
        },
        tooltip: {// 鼠标指向时的提示框
          trigger: 'axis',
          formatter: function(params, ticket, callback) { // tooltip样式用formatter来灵活定义
            let res = `<div class='bartooltip'>${params[1].value} </div>`; // 甚至可以传入class用css来修改
            return res;
          }
        },
        xAxis: [{
          type: 'value',
          max: '18000', // 给x轴添加个最大值/最小值可以手动控制坐标轴结束/起始的值
          splitLine:{ // 纵向的分割线
            lineStyle: {
              type: 'dash',
              color: 'rgba(255,255,255,.65)',//纵向的分割线颜色
            }
          },
          axisLine: {show: false},  // 隐藏x轴线
          axisLabel: { // x轴标签文字
          	show: true, // 是否显示x轴标签文字
            textStyle: {color: 'rgba(255,255,255,.65)'}
          }
        }],
          yAxis: { // 同x轴
              type: 'category',
              data: ['技工', '质量检测员/测试员', '客服专员/助理', '行政专员/助理', '人事专员', '普工/操作', '会计', '销售主管', '销售经理', '销售代表'],
              axisLine: {
                lineStyle: {
                  type: 'dashed',
                  color: 'rgba(255,255,255,.65)',//左边线的颜色
                  width:'1'//坐标线的宽度
                }
              },
              axisTick: {show:false}, // 是否显示刻度线
              axisLabel: {
                textStyle: {color: 'rgba(255,255,255,.65)'}
              }
          },
          series: [ // 传入具体数据
            { // 这一组数据data都设置为最大值,并且barGap上移100%,是为了给每一个bar添加阴影
              name: '职位',
              type: 'bar',
              barCategoryGap: '25',
              barGap: '-100%',
              data: [18000, 18000, 18000, 18000, 18000, 18000, 18000, 18000, 18000, 18000],
              itemStyle:{
                normal:{
                  color: '#252e3f',
                }
              }
            },
            {
              name: '职位',
              type: 'bar',
              barCategoryGap: '25',
              data: [15203, 13489, 9034, 10497, 13174, 1230, 12589, 4000, 10000, 1000],
              itemStyle:{
                normal:{
                  color: function(params) {
                    var index_value = params.value;
                    if(index_value > 15000){
                      return new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0,color: '#EC8860'}, {offset: 1,color: '#EC4D4B'}]) // 颜色渐变显示
                    }else{
                      return new echarts.graphic.LinearGradient(0, 0, 1, 0, [{offset: 0,color: '#53D1D2'}, {offset: 1,color: '#02E4CF'}])
                    }
                  }
                }
              }
            }
          ]
      },
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值