Echarts 双向柱图封装,含双值数据及名称位置修改

前言

实现如下图所示双向柱状图,难点名称位置的调整,双值展现,以及双值最大值设置。


 

一、组件封装

1.代码如下(示例):

const myData = [
    '地区一',
    '地区二',
    '地区三四',
    '地区四',
    '地区五',
    '地区六',
    '地区七',
    '地区八',
];

const vehicle = [80, 17.4, 65.5, 58.5, 87.3, 98.2, 45, 65,];
const personnel = [28, 17.5, 26.4, 30.3, 28, 34.4, 56, 26];
const data1 = [12112, 3882, 2332, 3039, 1233, 308, 1297, 1283];
const data2 = [10180, 1284, 2093, 3890, 1908, 203, 1123, 1987];
let maxVal1 = Math.max(...vehicle)
let maxVal2 = Math.max(...personnel)
let maxVal = maxVal1>maxVal2 ? maxVal1 : maxVal2
console.log('maxVal',maxVal)
function fomatPercent(data) {
            let sum = 0;
            data1.map((item) => {
                sum += item;
            });
            return Math.round((data / sum) * 100) + '%';
        }
        function formatterArr() {
            var arr = new Array();
            data1.map((item, i) => {
                arr.push({
                    coord: [item, i]
                });
            });
            return arr
        }
option = {
    tooltip: {
        show: true,
    },
    grid: [
        {
            show: false,
            left: '55%',
            top: 0,
            bottom: 22,
            width: '0%',
        },
        {
            show: false,
            left: '10%',
            top: '5%',
            bottom: 0,
            containLabel: true,
            width: '39.8%',
        },
        {
            show: false,
            right: '10%',
            top: '5%',
            bottom: 0,
            containLabel: true,
            width: '39.8%',
        },
    ],
    xAxis: [
        {
            gridIndex: 0,
            show: false,
        },
        {
            gridIndex: 1,
            max:maxVal,
            type: 'value',
            inverse: true,
            axisLine: {
                show: false,
            },
            axisTick: {
                show: false,
            },
            axisLabel: {
                show: false,
                verticalAlign: 'middle',
                textStyle: {
                    color: '#B2B2B2',
                    fontSize: 12,
                },
            },
            splitLine: {
                show: false,
            },
        },
        {
            gridIndex: 2,
            type: 'value',
            max:maxVal,
            axisLine: {
                show: false,
            },
            axisTick: {
                show: false,
            },
            axisLabel: {
                show: false,
                verticalAlign: 'middle',
                textStyle: {
                    color: '#B2B2B2',
                    fontSize: 12,
                },
            },
            splitLine: {
                show: false,
            },
        },
    ],
    yAxis: [
        {
            gridIndex: 0,
            type: 'category',
            inverse: true,
            position: 'right',
            axisLine: {
                show: false,
            },
            axisTick: {
                show: false,
            },
            data: myData.map(function (value) {
                return {
                    value: value,
                    textStyle: {
                        align: 'right',
                    },
                };
            }),
            axisLabel: {
                fontSize: 14,
                rich: {
                    b: {
                        color: '#333',
                        width: 100,
                        height: 30,
                        align: 'center',
                    },
                },
                formatter: function (params) {
                    return [`{b|${params}}`].join('\n');
                },
            },
        },
        {
            gridIndex: 1,
            type: 'category',
            inverse: true,
            position: 'left',
            axisLine: {
                show: false,
            },
            axisTick: {
                show: false,
            },
            axisLabel: {
                show: true,
                margin: 8,
                textStyle: {
                    color: '#1B1D1E',
                    fontSize: 14,
                    fontWeight:600
                },
                align: "top",
                padding: [0, 100, 0, 0],
                formatter: function (params) {
                    var index = myData.map((item) => item).indexOf(params);
                    var value = data1.map((item) => item);
                    return value[index]
                },
            },
            data: myData.map(function(value) {
                return {
                    value: value,
                    textStyle: {
                        align: 'center'
                    }
                }
            })
        },
        {
            gridIndex: 2,
            type: 'category',
            inverse: true,
            position: 'right',
            axisLine: {
                show: false,
            },
            axisTick: {
                show: false,
            },
            axisLabel: {
                show: true,
                textStyle: {
                    color:'#1B1D1E',
                    fontSize:14,
                    fontWeight:600,
                },
                padding: [0, 0, 0, 40],
                formatter: function (params) {
                    var index = myData.map((item) => item).indexOf(params);
                    var value = data2.map((item) => item);
                    return value[index]
                },
            },
            data: myData,
        },
    ],
    series: [
        {
            name: '车辆',
            type: 'bar',
            barGap: 20,
            barWidth: 20,
            xAxisIndex: 1,
            yAxisIndex: 1,
            showBackground: true,
            backgroundStyle: {
                color: 'rgba(245, 245, 245, 1)',
            },
            label: {
                normal: {
                    show: true,
                    color:'#1B1D1E',
                    fontSize:14,
                    fontWeight:600,
                    verticalAlign:'middle',
                    position:"insideRight",
                    padding:[3,130,0,0],
                    formatter: (params) => {
                        return params.data + '%'
                    },
                },
                // emphasis: {
                //     show: true,
                //     position: 'left',
                //     offset: [0, 0],
                //     textStyle: {
                //         color: '#666666',
                //         fontSize: 14,
                //     },
                // },
            },
           
            itemStyle: {
                normal: {
                    // barBorderRadius: [10, 0, 0, 10],
                    color:'#BFD76D'
                },
            },
            data: vehicle,
        },
        {
            name: '人员',
            type: 'bar',
            barGap: 20,
            barWidth: 20,
            xAxisIndex: 2,
            yAxisIndex: 2,
            showBackground: true,
            backgroundStyle: {
                color: 'rgba(245, 245, 245, 1)',
            },
            label: {
                normal: {
                    show: true,
                    // position:'right',
                    color:'#1B1D1E',
                    fontSize:14,
                    fontWeight:600,
                    verticalAlign:'middle',
                    position:"insideLeft",
                    padding:[3,0,0,130],
                    formatter: (params) => {
                        return params.data + '%'
                    },
                },
                // emphasis: {
                //     show: true,
                //     position: 'right',
                //     offset: [0, 0],
                //     textStyle: {
                //         color: '#666666',
                //         fontSize: 14,
                //     },
                // },
            },
            itemStyle: {
                normal: {
                    // barBorderRadius: [0, 10, 10, 0],
                    color:'#D7B36D',
                },
            },
            data: personnel,
        },
        
    ],
};


总结

关键属性:grid        第一个对象中设置的为名称的位置(left),第二个第三个分别对应左右两边柱状图的位置及宽度,为实现自适应最好宽度以及上下左右属性都用百分比设置

y轴的gridIndex   为0时为名字显示的数据,为1时为左侧外侧显示的数据,为2时为右侧外侧显示的数据,具体样式和需求对应即可

series中的label  定义柱体中的数值百分比位置

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Java中,可以使用ECharts库对ECharts图表数据进行封装和处理。关于ECharts的使用,你可以先查看官方网站以获取详细的信息和文档。 在封装ECharts数据时,你可以使用不同的方法来处理不同的图表类型。下面是两个示例的方法: 1. 饼图数据拼装方法:在这个方法中,首先需要传入一个包含图表数据的Map对象。然后通过遍历数据列表,将每个数据项的名称和值存入一个新的Map对象中,最后将这个Map对象作为饼图的数据项,放入Option对象中。最后返回Option对象。 2. 柱形图数据拼装方法:同样需要传入一个包含图表数据的Map对象。通过遍历数据列表,将每个数据项的日期和数量分别存入对应的数组中,然后创建一个类目轴(CategoryAxis)对象,并将日期数组作为轴的数据项。接着创建一个柱状图(Bar)对象,并将数量数组作为柱状图数据项。最后将类目轴和柱状图对象都设置到Option对象中,并返回Option对象。 以上是对ECharts数据封装的简要解释,你可以根据具体需求选择合适的方法来进行数据封装。希望这可以帮助到你。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [java Echarts的实战(Echarts图表数据封装)](https://blog.csdn.net/weixin_42444325/article/details/119040398)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值