echarts入门 柱状图多个柱子横向渐变效果

echarts柱状图多个柱子横向渐变效果

效果图

 实现的主要核心思想是,首先获取每个柱子的index,然后设置给每个柱子不同的透明度

color: function(d) {
    let a = 6 * 10
    return `rgba(34,95,219,${(((100-a)+(d.dataIndex*5))/100).toFixed(2)})`
}

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 步骤一:引入echarts.js -->
    <script src="../lib/echarts.min.js"></script>
</head>
<body>
    <!-- 步骤二:准备一个呈现图表的盒子 -->
    <div id="main" style="width:600px;height:400px"></div>
    <script>
        // 步骤三:初始化echarts实例对象
        // 参数,dom,决定图标最终呈现的位置
        var chartDom = document.getElementById('main');
        var myChart = echarts.init(chartDom);
        var option;
        var large = 0.2;
        // 步骤四:准备配置项
        option = {
            // backgroud:'#333',
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                type: 'shadow'
                }
            },
            // legend: {},
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [
                {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
                }
            ],
            yAxis: [
                {
                type: 'value'
                }
            ],
            series: [
                {
                    name: 'Email',
                    type: 'bar',
                    stack: 'Ad',
                    emphasis: {
                        focus: 'series'
                    },
                    barWidth:10,
                    data: [120, 132, 101, 134, 90, 230, 210],
                    itemStyle: {
                        normal: {
                            color: function(d) {
                                let a = 6 * 10
                                return `rgba(34,95,219,${(((100-a)+(d.dataIndex*5))/100).toFixed(2)})`
                            }
                        }
                    }
                },
                {
                    name: 'Union Ads',
                    type: 'bar',
                    stack: 'Ad',
                    emphasis: {
                        focus: 'series'
                    },
                    data: [220, 182, 191, 234, 290, 330, 310],
                    itemStyle: {
                        normal: {
                            // color: "rgba(44,184,229)"
                            color: function(d) {
                                let a = 6 * 10
                                return `rgba(44,184,229,${(((100-a)+(d.dataIndex*5))/100).toFixed(2)})`
                            }
                        }
                    }
                },
            ]
        };
        
        // 步骤五:将配置项设置给echarts实例对象
        option && myChart.setOption(option);
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_45818290/article/details/126579132