echarts柱状图渐变 堆叠柱状图渐变 小程序 vue

echarts堆叠柱状图渐变

官网实例 堆叠柱状图
堆叠渐变柱状图
堆叠渐变柱状图

import * as echarts from '../../ec-canvas/echarts';  //微信小程序需引用,其他正常引用echarts
let chart = null;
let base = [12, 45, 72, 26, 36]; //数据
let low = [], //范围值低
normal = [], //范围值正常
high = [], //范围值高
warning = []; //范围值警告
base.forEach((e, i) = >{
    if (e < 20) {
        low[i] = e
    }
    if (e < 40) {
        normal[i] = e
    }
    if (e < 60) {
        high[i] = e
    }
    if (e < 80) {
        warning[i] = e
    }
}) function initChart(canvas, width, height, dpr) {
    chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
    });
    canvas.setChart(chart);

    var option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器,坐标轴触发有效
                type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
            },
            confine: true
        },
        grid: {
            left: 20,
            right: 20,
            bottom: 15,
            top: 40,
            containLabel: true
        },
        xAxis: [{
            type: 'value',
            axisLine: {
                lineStyle: {
                    color: '#999'
                }
            },
            axisLabel: {
                color: '#666'
            }
        }],
        yAxis: [{
            type: 'category',
            axisTick: {
                show: false
            },
            data: ['汽车之家', '今日头条', '百度贴吧', '一点资讯', '微信'],
            axisLine: {
                lineStyle: {
                    color: '#999'
                }
            },
            axisLabel: {
                color: '#666'
            }
        }],
        series: [
        /* 范围值低 */
        {
            name: 'low',
            type: 'bar',   // 折线图
            barWidth: 30,  //柱体宽度
            stack: 'a',    //分组
            data: low,
            itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: '#009944'
                },
                {
                    offset: 0.5,
                    color: '#4EDD8E'
                },

                ])
            },
        },
        /* 范围值正常 */
        {
            name: 'normal',
            type: 'bar',
            barWidth: 30,
            stack: 'a',
            data: normal,
            itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: '#FFAE04'
                },
                {
                    offset: 0.5,
                    color: '#FFD630'
                },

                ])
            },
        },
        /* 范围值高 */
        {
            name: 'high',
            type: 'bar',
            barWidth: 30,
            stack: 'a',
            data: high,
            itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: '#FF9294'
                },
                {
                    offset: 0.5,
                    color: '#FFC17F'
                },

                ])
            },
        },
        /* 范围值超出警告 */
        {
            name: 'warn',
            type: 'bar',
            barWidth: 30,
            stack: 'a',
            data: warning,
            itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: '#DD352A'
                },
                {
                    offset: 0.5,
                    color: '#F27269'
                },

                ])
            },
        }]
    };

    chart.setOption(option);
    return chart;
}

echarts柱状图分级多色渐变

不同柱子颜色渐变不同

import * as echarts from '../../ec-canvas/echarts';
let chart = null;
var base = [10, 45, 72, 26, 90];

function initChart(canvas, width, height, dpr) {
    chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
    });
    canvas.setChart(chart);

    var option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器,坐标轴触发有效
                type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
            },
            confine: true
        },
        grid: {
            left: 20,
            right: 20,
            bottom: 15,
            top: 40,
            containLabel: true
        },
        xAxis: [{
            type: 'value',
            axisLine: {
                lineStyle: {
                    color: '#999'
                }
            },
            axisLabel: {
                color: '#666'
            }
        }],
        yAxis: [{
            type: 'category',
            axisTick: {
                show: false
            },
            data: ['汽车之家', '今日头条', '百度贴吧', '一点资讯', '微信'],
            axisLine: {
                lineStyle: {
                    color: '#999'
                }
            },
            axisLabel: {
                color: '#666'
            }
        }],
        series: [
        {
            name: 'low',
            type: 'bar',  // 折线图
            barWidth: 30, //柱体宽度
            stack: 'bar', //分组
            data: base,
            itemStyle: {
                normal: {
                    color: function(params) {
                        let colorList = [];
                        let value = params.value;
                        if (value > 0 && value <= 20) {
                            colorList = ['#009944', '#4EDD8E']
                        }
                        if (value > 20 && value <= 40) {
                            colorList = ['#FFAE04', '#FFD630']
                        }
                        if (value > 40 && value <= 60) {
                            colorList = ['#FF9294', '#FFC17F']
                        }
                        if (value > 60 && value <= 80) {
                            colorList = ['#FF5409', '#FFB080']
                        }
                        if (value > 80 && value <= 100) {
                            colorList = ['#DD352A', '#F27269']
                        }
                        return new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                            offset: 0,
                            color: colorList[0]
                        },
                        {
                            offset: 1,
                            color: colorList[1]
                        }]);
                    },
                    barBorderRadius: 2 //柱状角成椭圆形
                }
            }
        }]
    };

    chart.setOption(option);
    return chart;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值