echart柱状图的使用

本文详细介绍了如何使用ECharts库创建横向和纵向柱状图,包括设置数据、配置项以及实现动态交互效果,是ECharts图表应用的实用教程。

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

1、echart柱状图的使用(横向)

function initColumnEchart(idName , dayArray , fromClr , toClr, tipClr,type){
    var myChart = null;
    var xArry = [];
    var yArry = [];
    if(type=="A"){
        $.each(dayArray,function (i,iele) {
            yArry.unshift(iele.name);
            xArry.unshift(iele.avg);
        });
    }else {
        $.each(dayArray,function (i,iele) {
            yArry.unshift(iele.name);
            xArry.unshift(iele.sum);
        });
    }


    myChart = echarts.init(document.getElementById(idName));
    var columnOption = {
        grid: {
            left: '15px',
            right: '15px',
            bottom: '10px',
            top: '10px',
            x:20,
            x2:20,
            containLabel: true
        },
        tooltip: {
            padding: 10,
            backgroundColor: tipClr,
            borderColor: '#79ABCC',
            borderWidth: 1,
            textStyle: {
                color: "#D4F7FF",
                fontSize:12,
                textAlign:'center'
            },
            extraCssText: 'box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);',
            formatter: function (obj) {
                var value = obj.value;
                var index = obj.dataIndex;
                if(type=="A"){
                    var clickDate = yArry[index];
                    var count = xArry[index];
                    return clickDate+"<br>分数:"+count+"分";
                }else {
                    var clickDate = yArry[index];
                    var count = xArry[index];
                    return clickDate+"<br>数量:"+count+"次";
                }
            }
        },
        backgroundColor: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: 'rgba(8,56,61,0)'
        }, {
            offset: 1,
            color: 'rgba(255,255,255,0.04)'
        }]),
        xAxis: {
            type: 'value',
            data: xArry,
            axisLabel: {
                color: '#94A8A2',
            },
            axisLine: {
                show: false,
                lineStyle:{
                    color:'rgba(27,42,48,1)',
                    width: 1
                }
            },
            axisTick: {
                show: true,
                lineStyle:{
                    color:'rgba(27,42,48,1)',
                    width: 1,
                },
            },
            splitLine: {
                show: true,
                lineStyle:{
                    color:'rgba(27,42,48,1)',
                    width: 0.5,
                    type:'dashed'
                }
            },
            // splitNumber: 5,
        },
        yAxis: {
            type: 'category',
            data: yArry,
            axisLabel: {
                show:true,
                color: '#94A8A2',
            },
            axisLine: {
                show: true,
                lineStyle:{
                    color:'#A9A9A9',
                    width: 0.5
                }

            },
            axisTick: {
                show: true,
                lineStyle:{
                    color:'#A9A9A9',
                    width: 1
                },
                color:'#A9A9A9',
                alignWithLabel: true,
                length:2
            },
        },
        series: [{
            data: xArry,
            type: 'bar',
            barWidth: '10',
            // barGap:'-100%',
            // barCategoryGap:'50%',
            itemStyle: {
                color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                    offset: 0,
                    color: fromClr
                }, {
                    offset: 1,
                    color: toClr
                }]),
            }
        }]
    }
    myChart.setOption(columnOption);
    window.addEventListener("resize", function () {
        myChart.resize();
    });
}

在这里插入图片描述
2、echart柱状图的使用(纵向)

function initColumnEchart(idName , dayArray , fromClr , toClr, tipClr){
    var myChart = null;
    var xArry = [];
    var yArry = [];
    $.each(dayArray,function (i,iele) {
        xArry.push(iele.monthInfo);
        yArry.push(iele.monthIncome);
    });

    myChart = echarts.init(document.getElementById(idName));
    var columnOption = {
        grid: {
            left: '15px',
            right: '15px',
            bottom: '10px',
            top: '25px',
            x:20,
            x2:20,
            containLabel: true
        },
        tooltip: {
            padding: 10,
            backgroundColor: tipClr,
            borderColor: '#79ABCC',
            borderWidth: 1,
            textStyle: {
                color: "#D4F7FF",
                fontSize:12,
                textAlign:'center'
            },
            extraCssText: 'box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);',
            formatter: function (obj) {
                var value = obj.value;
                var index = obj.dataIndex;
                var clickDate = dayArray[index].monthInfo;
                var count = dayArray[index].monthIncome;
                return clickDate+"<br>收入:"+count+"元";
            }
        },
        backgroundColor: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: 'rgba(8,56,61,0)'
        }, {
            offset: 1,
            color: 'rgba(255,255,255,0.04)'
        }]),
        xAxis: {
            type: 'category',
            data: xArry,
            axisLabel: {
                color: '#94A8A2',
            },
            axisLine: {
                show: false,
                lineStyle:{
                    color:'rgba(27,42,48,1)',
                    width: 1
                }
            },
            axisTick: {
                show: false,
                lineStyle:{
                    color:'rgba(27,42,48,1)',
                    width: 1,
                },
            },
            splitLine: {
                show: false,
                lineStyle:{
                    color:'rgba(27,42,48,1)',
                    width: 0.5,
                    type:'dashed'
                }
            },
            // splitNumber: 5,
        },
        yAxis: {
            type: 'value',
            data: yArry,
            axisLabel: {
                show:true,
                color: '#94A8A2',
            },
            axisLine: {
                show: false,
                lineStyle:{
                    color:'rgba(191,191,191,0.7)',
                    width: 1
                }

            },
            axisTick: {
                show: false,
                lineStyle:{
                    color:'rgba(191,191,191,0.7)',
                    width: 1
                },
                color:'rgba(191,191,191,0.7)',
                alignWithLabel: true
            },
            splitLine: {
                show: true,
                lineStyle:{
                    color:'#1B2A30',
                    width: 1,
                }
            },
        },
        series: [{
            data: yArry,
            type: 'bar',
            barWidth: '20',
            barGap:'-100%',
            // barCategoryGap:'65%',
            itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: fromClr
                }, {
                    offset: 1,
                    color: toClr
                }]),
                normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: fromClr
                    }, {
                        offset: 1,
                        color: toClr
                    }]),
                    label: {
                        show: true, //开启显示
                        position: 'top', //在上方显示
                        textStyle: { //数值样式
                            color: '#2FCAF1',
                            fontSize: 12
                        }
                    }
                }
            }
        }]
    }
    myChart.setOption(columnOption);
    window.addEventListener("resize", function () {
        myChart.resize();
    });
}

在这里插入图片描述

### 如何使用 ECharts 创建柱状图 要在 JavaScript 中使用 ECharts 创建柱状图,可以按照以下方式实现。此过程涉及初始化 ECharts 实例、设置数据以及应用配置选项。 #### 初始化 ECharts 实例 为了在页面上显示图表,需先引入 ECharts 库并初始化一个实例。通过 `document.getElementById` 方法获取目标 DOM 容器,并将其传递给 `echarts.init()` 函数完成实例化[^1]。 ```javascript var chart = echarts.init(document.getElementById('chart')); ``` #### 数据准备 定义用于展示的数据集。通常情况下,这些数据会被分为两个部分:X 轴上的类别名称和 Y 轴对应的数值。下面是一个简单的模拟数据数组: ```javascript const data = [ { name: '项目A', value: 120 }, { name: '项目B', value: 200 }, { name: '项目C', value: 150 }, { name: '项目D', value: 80 }, { name: '项目E', value: 70 } ]; ``` #### 配置图表属性 构建图表所需的配置对象,其中包括标题、提示框组件 (`tooltip`)、坐标轴以及其他系列参数等。这里我们设置了 X 和 Y 坐标轴分别对应于项目的名称及其关联的值[^2]。 ```javascript const options = { title: { text: '示例柱状图' }, tooltip: {}, xAxis: { type: 'category', data: data.map(item => item.name) }, yAxis: {}, series: [{ name: '数量', type: 'bar', data: data.map(item => item.value) }] }; ``` #### 渲染图表 最后一步就是调用 `setOption` 方法将之前设定好的所有配置应用于已创建的图表实例之上,从而渲染出最终效果[^3]。 ```javascript chart.setOption(options); ``` 以上便是利用 ECharts 构建基本柱状图的主要流程概述及相关代码片段演示说明。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值