前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >搬运一个 timeline 的应用 idea

搬运一个 timeline 的应用 idea

作者头像
ZXand618
发布2022-04-10 10:02:59
3590
发布2022-04-10 10:02:59
举报

创意来自于 github 上的用户 Zacknero,用 timeline 实现地铁报站图,他发了个 issue ,我刚好看到贡献了一丁点思路,所以厚着脸皮给大家转发一下

代码语言:javascript
复制
currentIndex = 2; // 0, 1, 2, 3, ....
len = 9; // len = option.baseOption.timeline.data.length - 1

option = {
    baseOption: {
        timeline: {
            axisType: 'category',
            top: '50%', // 'top: "auto"'' -> 'top: "50%"'
            currentIndex: currentIndex, // 'currentIndex: 2' -> 'currentIndex: currentIndex' 
            controlStyle: {
                show: false
            },
            data: [
                '2002-01-01', '2003-01-01', '2004-01-01',
                {
                    value: '2005-01-01',
                    symbol: 'diamond',
                    symbolSize: 16
                },
                '2006-01-01', '2007-01-01', '2008-01-01', '2009-01-01', '2010-01-01',
                {
                    value: '2011-01-01',
                    symbol: 'diamond',
                    symbolSize: 18
                },
            ],
            label: {
                formatter: function(s) {
                    return (new Date(s)).getFullYear();
                },
                position: 'top',
                rotate: 90,
                padding: -10
            },
            lineStyle: {
                width: 5,
                color: {
                    type: 'linear',
                    colorStops: [{
                        offset: currentIndex / len,
                        color: 'green' // color at position < (currentIndex / len)
                    }, {
                        offset: currentIndex / len,
                        color: 'grey' // color at position > (currentIndex / len) 
                    }]
                }
            }
        }
    }
};

myChart.on('timelinechanged', function(params) {
    option.baseOption.timeline.currentIndex = params.currentIndex;
    option.baseOption.timeline.lineStyle.color.colorStops[0].offset = params.currentIndex / (option.baseOption.timeline.data.length - 1);
    option.baseOption.timeline.lineStyle.color.colorStops[1].offset = params.currentIndex / (option.baseOption.timeline.data.length - 1);
    myChart.setOption(option);
});
  • 这个 option.baseOption.timeline.lineStyle.color.colorStops 是渐变色的配置,定义不同 offset 处的颜色,两个 offset 之间的颜色平滑过渡。相当于下图中这两个滑块,两个滑块放一起颜色就突变了(上面配置项里,用的就是突变)
  • len = option.baseOption.timeline.data.length - 1,是因为 10 个点组成 9 个线段
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ZXand618的ECharts之旅 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档