echarts双X轴(非等分情况),区县比例堆叠柱状图

文章展示了如何使用ECharts库创建一个具有双X轴的柱状图,其中一个轴表示具体的区域(如贵阳市的各区),另一个轴表示这些区域在总数据中的比例。通过代码详细配置了图表的各个部分,包括颜色、标签、网格和数据系列。
摘要由CSDN通过智能技术生成

效果图:

 代码展示:

let data = {
    xData: ['花溪区', '南明区', '观山湖区', '钟山区', '六枝特区', '红花岗区', '汇川区', '遵义县', '西秀区', '平坝县', '玉屏县'],
    yData: [0.1, 0.15, 0.18, 0.25, 0.23, 0.32, 0.33, 0.36, 0.57, 0.72, 0.45]
}
/**
    双X轴标签对应,伪实现思路:
    底部的标签也是柱状图,对应包含的区域为上方X轴条数占总数的比例,设为宽度即可
*/
option = {
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'shadow'
        }
    },
    grid: [
        {
            top: 100,
            bottom: 101
        },
        {
            height: 60,
            bottom: 40
        }
    ],
    xAxis: [{
        type: 'category',
        data: data.xData,
        gridIndex: 0,
        axisLabel: {
            color: '#333'
        },
        axisLine: {
            lineStyle: {
                color: '#e7e7e7'
            }
        },
        axisTick: {
            lineStyle: {
                color: '#e7e7e7'
            }
        },
        zlevel: 2
    }, {
        type: 'category',
        gridIndex: 1,
        axisLine: {
            show: false
        },
        zlevel: 1
    }],
    yAxis: [{
        type: 'value',
        gridIndex: 0,
        axisLabel: {
            color: '#333'
        },
        splitLine: {
            lineStyle: {
                type: 'dashed'
            }
        },
        axisLine: {
            lineStyle: {
                color: '#ccc'
            }
        },
        axisTick: {
            lineStyle: {
                color: '#ccc'
            }
        }
    },{
        type: 'value',
        gridIndex: 1,
        axisLabel: {
            show: false
        },
        axisLine: {
            show: false
        },
        splitLine: {
            show: false
        },
        axisTick: {
            show: false
        }
    }],
    series: [{
        data: [120, 232, 101, 234, 350, 300],
        stack:"Search Engine",
        type: 'bar',
        label: {
            show: true,
            position: 'top',
            textStyle: {
                color: '#555'
            }
        },
        itemStyle: {
            normal: {
                color: (params) => {
                    let colors = ['#4150d8', '#28bf7e', '#ed7c2f', '#f2a93b', '#f9cf36', '#4a5bdc', '#4cd698', '#f4914e', '#fcb75b', '#ffe180', '#b6c2ff', '#96edc1']
                    return colors[params.dataIndex]
                }
            }
        },
        xAxisIndex: 0,
        yAxisIndex: 0
    },
    {
        data: [320, 332, 301, 334, 390, 330],
        stack:"Search Engine",
        type: 'bar',
        label: {
            show: true,
            position: 'top',
            textStyle: {
                color: '#555'
            }
        },
        itemStyle: {
            normal: {
                color: (params) => {
                    let colors = ['red', 'blue', 'pink', 'lightblue', 'purple', 'orange']
                    return colors[params.dataIndex]
                }
            }
        },
        xAxisIndex: 0,
        yAxisIndex: 0
    },
    {
        data: [{
            name: '贵阳市',
            value: 1
        }],
        label: {
            show: true,
            position: 'inside',
            formatter: '{b}',
            offset: [0, 10],
            textStyle: {
                color: '#777'
            }
        },
        type: 'bar',
        barGap: 0,
        barWidth: '27.2727%',
        itemStyle: {
             normal: {
                color: '#fff',
                borderColor:'#ccc'
            }
        },
        xAxisIndex: 1,
        yAxisIndex: 1
    },{
        data: [{
            name: '六盘水市',
            value: 1
        }],
        label: {
            show: true,
            position: 'inside',
            formatter: '{b}',
            offset: [0, 10],
            textStyle: {
                color: '#777'
            }
        },
        type: 'bar',
        barGap: 0,
        barWidth: '18.1818%',
        itemStyle: {
             normal: {
                color: '#fff',
                borderColor:'#ccc'
            }
        },
        xAxisIndex: 1,
        yAxisIndex: 1
    },{
        data: [{
            name: '遵义市',
            value: 1
        }],
        label: {
            show: true,
            position: 'inside',
            formatter: '{b}',
            offset: [0, 10],
            textStyle: {
                color: '#777'
            }
        },
        type: 'bar',
        barGap: 0,
        barWidth: '27.2727%',
        itemStyle: {
             normal: {
                color: '#fff',
                borderColor:'#ccc'
            }
        },
        xAxisIndex: 1,
        yAxisIndex: 1
    },{
        data: [{
            name: '安顺市',
            value: 1
        }],
        label: {
            show: true,
            position: 'inside',
            formatter: '{b}',
            offset: [0, 10],
            textStyle: {
                color: '#777'
            }
        },
        type: 'bar',
        barGap: 0,
        barWidth: '18.1818%',
        itemStyle: {
             normal: {
                color: '#fff',
                borderColor:'#ccc'
            }
        },
        xAxisIndex: 1,
        yAxisIndex: 1
    },{
        data: [{
            name: '铜仁市',
            value: 1
        }],
        label: {
            show: true,
            position: 'inside',
            formatter: '{b}',
            offset: [0, 10],
            textStyle: {
                color: '#777'
            }
        },
        type: 'bar',
        barCategoryGap: 0,
        barGap: 0,
        barWidth: '9.0909%',
        itemStyle: {
            normal: {
                color: '#fff',
                borderColor:'#ccc'
            }
        },
        xAxisIndex: 1,
        yAxisIndex: 1
    }]
};

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过Echarts的grid、xAxis和yAxis属性配置实现x非等分情况,具体步骤如下: 1. 在grid属性中设置bottom、top、left、right属性,控制图表的位置和大小。 2. 在xAxis属性中设置type为'category',设置data属性为x要展示的数据,同时设置axisTick、axisLabel、splitLine等属性。 3. 在yAxis属性中设置type为'value',设置axisTick、axisLabel、splitLine等属性。 4. 在series属性中设置type为'bar',同时设置xAxisIndex属性为0或1,控制柱状图在哪个x上展示。 5. 如果需要让两个x的刻度对应起来,可以设置grid属性中的height、width属性和yAxis属性中的max、min属性,使得两个x的刻度对应。 以下是一个示例代码: ```javascript option = { grid: { top: 50, left: 50, right: 50, bottom: 80, height: 200, width: '80%' }, xAxis: [{ type: 'category', data: ['A', 'B', 'C', 'D', 'E'], axisTick: { alignWithLabel: true }, axisLabel: { interval: 0 }, splitLine: { show: false } }, { type: 'category', data: ['F', 'G', 'H', 'I', 'J'], axisTick: { alignWithLabel: true }, axisLabel: { interval: 0 }, splitLine: { show: false } }], yAxis: { type: 'value', axisTick: { show: false }, axisLabel: { formatter: '{value} $' }, splitLine: { lineStyle: { type: 'dashed' } } }, series: [{ name: 'Series 1', type: 'bar', data: [10, 20, 30, 40, 50], xAxisIndex: 0 }, { name: 'Series 2', type: 'bar', data: [60, 70, 80, 90, 100], xAxisIndex: 1 }] }; ``` 以上代码实现了x非等分柱状图,其中第一个x显示A到E,第二个x显示F到J,两个x的刻度对应,同时图表通过grid属性控制了位置和大小。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值