echarts 柱状图实例

这个博客展示了如何使用ECharts库创建柱状图。它包括了数据的定义,如前日、昨日和今日的数据,并详细配置了图表的各个部分,如图例、X轴、Y轴、渐变色的柱子以及无分割线的设置。通过JavaScript的Ajax请求获取数据,然后初始化并设置ECharts实例来展示柱状图。

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

<script>
    var water_name=[];//数据名称
    var water_one=[];//前日数据
    var water_two=[];//昨日数据
    var water_three=[];//今日数据
    var myChart;
    function  water_mt(){
    // 基于准备好的dom,初始化echarts实例
        myChart= echarts.init(document.getElementById('water'));
    // 指定图表的配置项和数据
    var option = {//option选项
        legend: {//图例
            data: ['前日', '昨日', '今日'],
            itemHeight:5,
            itemWidth:20,
            right:'5%',
            top:'0',
            textStyle:{
                color:'#287DB0'
            },
            borderRadius: '50%',
            selectedMode:false//关闭图例点击事件
        },
        xAxis: {
            type: 'category',//category类别
            axisLabel: {//轴标签
                show: true,
                color: '#34a7e8'
            },
            data: water_name
            ,splitLine:{//splitLine分割线
                show:false,
                lineStyle:{
                    color:'rgba(250,250,250,.2)',
                    width: 1
                }
            }
        },
         grid:{
            left:'5%',
             top:'15%',
             bottom:'15%',
             right:'5%',
             containLabel:true
        },
        barWidth:'12%',//柱宽度
        yAxis: {
            axisLabel: {
                show: true,
                color: '#34a7e8',
                formatter:function(value){
                    return (value/10000).toFixed(1)+'万吨'//toFixed(1)小数点后一位
                }
            },
            type: 'value',
            splitLine:{//分割线
                show:true,
                lineStyle:{
                    type:'dotted',
                    color:'#767779',
                    width: 1
                }
            },
            axisTick:{
                show: false
            }
        },
        series: [{//系列
            name: '前日',
            type: 'bar',
            data:water_one,//返回数据的名称  试用可独自写一数组例如:[255,362,246,145,356,256]
            itemStyle: {
                normal: {
                    barBorderRadius:[10, 10, 10, 10],//柱圆角度数
                    shadowBlur: 10,//柱宽
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{//柱渐变颜色
                        offset: 0,
                        color: '#ABCA14'
                    }, {
                        offset: 1,
                        color: '#50980E'
                    }])
                }
            }
        },
            {
                name: '昨日',
                type: 'bar',
                data: water_two,
                itemStyle: {
                    normal: {
                        barBorderRadius:[10, 10, 10, 10],
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#40AAD4'
                        }, {
                            offset: 1,
                            color: '#0B4785'
                        }])
                    }
                }
            },
            {
                name: '今日',
                type: 'bar',
                data: water_three,
                itemStyle: {
                    normal: {
                        barBorderRadius:[10, 10, 10, 10],
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#CA4E50'
                        }, {
                            offset: 1,
                            color: '#9B1C16'
                        }])
                    }
                }
            }
        ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
    }

    function water_ajax (){
        $.ajax({
            type:'get',//类型
            url: "",//地址
            success: function(elm){请求成功返回的函数
             //   $('#water_img').hide();数据显示 loading图片隐藏
                water_mt();//执行上一函数
            }
        });
    }
water_ajax();
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值