echarts自定义多根柱子在一起柱状图/条形图

先看效果:

与单一柱状图相比,多柱子的多定义了几个series的值,legend值。注意的是:

这东西叫图例标记。是legend下面的参数。

看代码:

<div id="part5" style="width: 100%;height:240px;"></div>
<script src="js/jquery.min.js"></script>
<script src="echarts/dist/echarts.min.js"></script>

<script type="text/javascript">
//第5部分
    var dom5 = document.getElementById("part5");
    var myChart5 = echarts.init(dom5);
    var app5 = {};
    option5 = null;
    app5.title = '金融产品新增情况';

    option5 = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
        },
        textStyle: {
            color: '#466293'
        },
        legend: {
            data: ['租客分期','装修贷','周转贷','房东贷'],
            textStyle: {
                color: '#8db0ef',
                fontFamily: 'DINProRegular',
            },
            //图例标记宽高
            itemWidth: 15,
            itemHeight: 7,
            itemGap: 15,
            top: 15
        },
        //直角坐标系内绘图网格,就是柱状图距外边容器的margin值
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        yAxis: {
            type: 'value',
            name: '(个)',
            min: 0,
            max: 80,
            boundaryGap: [0, 0.1],
            //y轴文字属性设置
            axisLabel: {
                textStyle: {
                    fontSize: 16,
                    color: '#466293',
                    fontFamily: 'DINProRegular'
                },
                formatter: '{value}'
            },
            //y轴横向分割线颜色
            splitLine: {
                show: true,
                lineStyle: {
                   color: '#0c3668' 
                }
            },
            //y轴刻度线颜色
            axisTick: {
                show: false,
                lineStyle: {
                    color: '#00345c',
                } 
            },
            //y轴边框颜色
            axisLine: {
                show: false,
                lineStyle: {
                    color: '#00345c',
                } 
            }
        },
        xAxis: {
            type: 'category',
            //name: '(月)',
            data: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
            axisLabel: {
                show: true,
                textStyle: {
                   //fontSize: 16,
                    color: '#466293',
                    fontFamily: 'DINProRegular'
                }
            },
            axisTick: {
                show: true,
                lineStyle: {
                    color: '#00345c',
                } 
            },
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#00345c',
                } 
            }
        },
        series: [
            {
                name: '租客分期',
                type: 'bar',
                data: [0,7,73,34,44,46,33,6,0,0,0,0],
                itemStyle: {
                    //柱条渐变色
                    color:new echarts.graphic.LinearGradient(
                        1, 0, 0, 1,
                        [
                            {offset: 0, color: '#0090ff'},
                            {offset: 1, color: '#0075d0'}
                        ]
                    )
                }
            },
            {
                name: '装修贷',
                type: 'bar',
                data: [0,0,0,0,0,1,1,0,1,0,0,0],
                itemStyle: {
                    //柱条渐变色
                    color:new echarts.graphic.LinearGradient(
                        1, 0, 0, 1,
                        [
                            {offset: 0, color: '#464aff'},
                            {offset: 1, color: '#393dfe'}
                        ]
                    )
                }
            },
            {
                name: '周转贷',
                type: 'bar',
                data: [0,0,3,0,0,0,0,0,0,0,0,0],
                itemStyle: {
                    //柱条渐变色
                    color:new echarts.graphic.LinearGradient(
                        1, 0, 0, 1,
                        [
                            {offset: 0, color: '#ffb746'},
                            {offset: 1, color: '#ffb034'}
                        ]
                    )
                }
            },
            {
                name: '房东贷',
                type: 'bar',
                data: [0,2,3,2,1,0,0,0,0,0,0,0],
                itemStyle: {
                    //柱条渐变色
                    color:new echarts.graphic.LinearGradient(
                        1, 0, 0, 1,
                        [
                            {offset: 0, color: '#ff605f'},
                            {offset: 1, color: '#ff504f'}
                        ]
                    )
                }
            },
            
        ]
    };
    ;
    if (option5 && typeof option5 === "object") {
        myChart5.setOption(option5, true);
    }
</script>

 

 

echarts自定义柱状图,每个列柱子数据类型不同,需要在数据源中为不同的柱子指定不同的数据类型,例如字符串、数字或日期等。 首先,可以使用数组或对象作为数据源。对于每个柱子,可以为其指定一个对象,包含柱子的值和类型。例如: ``` // 数组作为数据源 var data = [ { value: 20, type: '数字' }, { value: '高峰期', type: '字符串' }, { value: new Date('2021-10-01'), type: '日期' } ]; // 对象作为数据源 var data = { '柱子1': { value: 20, type: '数字' }, '柱子2': { value: '高峰期', type: '字符串' }, '柱子3': { value: new Date('2021-10-01'), type: '日期' } }; ``` 然后,在echarts中配置柱状图时,可以根据每个柱子的类型来设置不同的格式化方式。例如: ``` option = { xAxis: { type: 'category', data: ['柱子1', '柱子2', '柱子3'] }, yAxis: { type: 'value' }, series: [{ type: 'bar', data: data.map(function(item) { return item.value; }), label: { show: true, formatter: function(params) { var dataIndex = params.dataIndex; var itemType = data[dataIndex].type; var itemValue = params.value; if (itemType === '日期') { return itemValue.getFullYear() + '/' + (itemValue.getMonth()+1) + '/' + itemValue.getDate(); } else { return itemValue; } } } }] }; ``` 在以上示例中,针对不同的类型,通过自定义label的formatter来格式化柱子上的标签。对于日期类型的柱子,使用getFullYear()、getMonth()+1和getDate()分别获取年、月、日并拼接成字符串;对于数字或字符串类型,直接返回其值。 通过上述方法,可以轻松实现echarts自定义柱状图中每个列柱子数据类型不同的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值