echarts柱状图渐变(根据数据大小,进行渐变)

效果

在这里插入图片描述

1、定义五种渐变颜色

// 柱状图渐变色
// 修改color中的x、y、x2、y2, 即可改变横向、纵向展示效果
var colorList = [];
colorList[0] = {color: '#11d56d'};
colorList[1] = {
color: {
  type: 'linear',
  x: 0,
  y: 1,
  x2: 0,
  y2: 0,
  global: false, // 缺省为 false
  colorStops: [{
    offset: 0,
    color: '#11d56d'
  }, {
    offset: 1,
    color: '#ffda30'
  }]
}
};
colorList[2] = {
color: {
  type: 'linear',
  x: 0,
  y: 1,
  x2: 0,
  y2: 0,
  global: false, // 缺省为 false
  colorStops: [{
    offset: 0,
    color: '#11d56d'
  }, {
    offset: 0.5,
    color: '#ffda30'
  }, {
    offset: 1,
    color: '#ff7e00'
  }]
}
};
colorList[3] = {
color: {
  type: 'linear',
  x: 0,
  y: 1,
  x2: 0,
  y2: 0,
  global: false, // 缺省为 false
  colorStops: [{
    offset: 0,
    color: '#11d56d'
  }, {
    offset: 0.33,
    color: '#ffda30'
  }, {
    offset: 0.66,
    color: '#ff7e00'
  }, {
    offset: 1,
    color: '#ff0000'
  }]
}
};
colorList[4] = {
color: {
  type: 'linear',
  x: 0,
  y: 1,
  x2: 0,
  y2: 0,
  global: false, // 缺省为 false
  colorStops: [{
    offset: 0,
    color: '#11d56d'
  }, {
    offset: 0.25,
    color: '#ffda30'
  }, {
    offset: 0.5,
    color: '#ff7e00'
  }, {
    offset: 0.75,
    color: '#ff0000'
  }, {
    offset: 1,
    color: '#99004c'
  }]
}
};

2、分装一个方法

function dataChange(list) {
// max是这个柱状图最大值,第三行可以不要,直接在第四行修改max为固定最大值
const max = Math.max.apply(null, list)
const oneCopy = max / 4;
let newList = [];
list.forEach(item => {
  let index = parseInt(item / oneCopy);
  newList.push({
    value: item,
    itemStyle: colorList[index]
  });
});
return newList;
}

3、使用dataChange

let echart = this.$echarts.init(this.$refs.ec);
  echart.setOption({
    title: {
      text: 'ECharts 入门示例'
    },
    tooltip: {},
  xAxis: {
      data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
    },
  yAxis: {},
    series: [
      {
        name: '销量',
        type: 'bar',
        showBackground: true,
        backgroundStyle: {
          color: 'rgba(180, 180, 180, 0.8)'
        },
        data: dataChange(allList)
      }
    ]
  })

借鉴了银子先生项目,pc上没有问题,小程序上有问题我自己修改了下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值