Echarts 横坐标时间轴,相同的年份只显示第一个,方案记录

 希望达到的效果

要点

展示第一个坐标的年份

由于横坐标过多时,echarts会自动隐藏掉一些横坐标,因此,需要得到显示的横坐标,

使用axisLabel.formatter可以获取到显示的横坐标,

通过外部变量,保存已展示的年份,遍历横坐标时,判断年份是否已存在,换行加上年份

展示竖线

使用echarts的label.rich 富文本编辑器,控制文字大小,将竖线' | '调整到合适位置。文字加粗,让颜色更清晰。

2021.11.15补充:
发现在chrome49低内核版本中,不能用rich 配置字号(canvas有最小字号),所以需要使用borderColor来画竖线。
第二行使用一个空元素(verticalLine),设置border边框属性,来展示竖线。设置左对齐。
第二行中verticalLine前使用一个空元素(paddingLeft),设置宽度,来控制竖线位置。

dataZoom支持

在拖动dataZoom的时候,echarts会重新触发formatter的遍历,此时需要在datazoom拖动的事件中,清空外部保存的已展示的年份数据

关键配置

xAxis: {
            type: 'time',
            boundaryGap: false,
            axisLabel: {
                formatter: (val, index) => {
                    let date = new Date(val).toLocaleDateString().replace(/\//g, '-');
                    let year = date.substring(0, 4);
                    console.log(date, index);
                    if (!tmpArr.includes(year)){
                        tmpArr.push(year);
                        // return `{y|${year}}-${date.substring(5)}`; // 年份不换行方案
                        // return `${date.substring(5)}\n{verticalLine||}\n${year}`; // 方案1画竖线
                        return `${date.substring(5)}\n{paddingLeft|}{verticalLine|}\n${year}`; // 方案2画竖线
                    }
                    return `${date.substring(5)}`;
                },
                rich: {
                    // 年份不换行方案
                    // y: {
                    //     fontWeight: 'bolder'
                    // }
                    // -----方案1画竖线
                    // verticalLine: {
                    //     align: 'left',
                    //     padding: 8,
                    //     fontSize: 6,
                    //     fontWeight: 'bolder',
                    //     lineHeight: 12
                    // }
                    // -----end 方案1
                    // -----方案2画竖线
                    paddingLeft: {
                        align: 'left',
                        width: 8
                    },
                    verticalLine: {
                        align: 'left',
                        borderColor: '#000',
                        borderWidth: 0.5, // 由于是左右border,所以画0.5 + 0.5 = 1
                        height: 6,
                    }
                    // ----- end 方案2
                }
            },
        },

全部代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.common.js"></script>
<!-- <script src="https://cdn.bootcdn.net/ajax/libs/echarts/4.9.0-rc.1/echarts-en.common.min.js"></script> -->
<body>
    <div id="echarts" style="height: 600px;width:800px"></div>
</body>
<script>
    let myChart = window.echarts.init(document.getElementById('echarts'));
    let base = +new Date(1988, 9, 3);
    let oneDay = 24 * 3600 * 1000;
    let data = [[base, Math.random() * 300]];
    for (let i = 1; i < 1000; i++) {
        let now = new Date((base += oneDay));
        data.push([+now, Math.round((Math.random() - 0.5) * 20 + data[i - 1][1])]);
    }
    let tmpArr = [];  // 存放已经显示的年份数组
    let option = {
        tooltip: {
            trigger: 'axis',
            position: function (pt) {
                return [pt[0], '10%'];
            }
        },
        grid: {
            bottom: 90,
        },
        title: {
            left: 'center',
            text: 'Large Ara Chart'
        },
        toolbox: {
            feature: {
                dataZoom: {
                    yAxisIndex: 'none'
                },
                restore: {},
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'time',
            boundaryGap: false,
            axisLabel: {
                formatter: (val, index) => {
                    let date = new Date(val).toLocaleDateString().replace(/\//g, '-');
                    let year = date.substring(0, 4);
                    console.log(date, index);
                    if (!tmpArr.includes(year)){
                        tmpArr.push(year);
                        // return `{y|${year}}-${date.substring(5)}`; // 年份不换行方案
                        // return `${date.substring(5)}\n{verticalLine||}\n${year}`; // 方案1画竖线
                        return `${date.substring(5)}\n{paddingLeft|}{verticalLine|}\n${year}`; // 方案2画竖线
                    }
                    return `${date.substring(5)}`;
                },
                rich: {
                    // 年份不换行方案
                    // y: {
                    //     fontWeight: 'bolder'
                    // }
                    // -----方案1画竖线
                    // verticalLine: {
                    //     align: 'left',
                    //     padding: 8,
                    //     fontSize: 6,
                    //     fontWeight: 'bolder',
                    //     lineHeight: 12
                    // }
                    // -----end 方案1
                    // -----方案2画竖线
                    paddingLeft: {
                        align: 'left',
                        width: 8
                    },
                    verticalLine: {
                        align: 'left',
                        borderColor: '#000',
                        borderWidth: 0.5, // 由于是左右border,所以画0.5 + 0.5 = 1
                        height: 6,
                    }
                    // ----- end 方案2
                }
            },
        },
        yAxis: {
            type: 'value',
            boundaryGap: [0, '100%']
        },
        dataZoom: [
            { type: 'inside', start: 0, end: 20 },
            { start: 0, end: 20 }
        ],
        series: [
            {
                name: 'Fake Data',
                type: 'line',
                smooth: true,
                symbol: 'none',
                areaStyle: {},
                data: data
            }
        ]
    };
    myChart.setOption(option);
    myChart.on('datazoom', () => {
        tmpArr = []; // 拖动dataZoom 时,重置该数组
    });
</script>

</html>

效果

使用 | 字符作为竖线效果(不兼容低版本浏览器)

使用border画竖线效果

年份放在一行效果

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值