echarts 实现点击时间轴,显示切换功能

echarts时间轴的样例中,是有点击时间轴,然后直接切换的功能的,但是用它的sample的时候,并没有这一块的功能,然后看网上的教材也都不怎么理想,后来看官方文档,找到的解决方法

1.在timeline中增加事件监听

timeline: {
    data: dataTime,
    triggerEvent: true,
    label: {
        formatter: function (s) {
            return formatDate(s, 'yyyy-MM');
        }
    },
    currentIndex: month,
    autoPlay: false
},

2.增加事件绑定

var myChart1 = echarts.init(document.getElementById("chart-box"));
myChart1.setOption(option);
myChart1.on("click", function (params) {
    if(params.componentType === 'timeline'){
        var month = new Date(params.dataIndex).getMonth();
        var myChart1 = echarts.init(document.getElementById("chart-box"));
        var option = {
            timeline: {
                data: dataTime,
                triggerEvent: true,
                label: {
                    formatter: function (s) {
                        return formatDate(s, 'yyyy-MM');
                    }
                },
                currentIndex: month,
                autoPlay: false
            },
            options: []
        };

        setDetail(chartData, option, month + 1);
        myChart1.setOption(option);
        myChart1.resize();
    }
});

3.设置样式,需要注意的几个地方就是timeline的currentIndex属性,用户指定当前显示使用的是哪个options[i]

另外一个需要注意的是options里面保存的显示的数据,但是我们需要为当前显示的那个数据设置样式,其余的只需要设置数据就行,因此我单独写了一个方法,用来拼接options的内容

/**
 * 动态设置图表的数据
 * @param chartData
 * @param option
 */
function setDetail(chartData, option, index) {
    var date = new Date();
    var options = [];
    var month = date.getMonth() + 1;
    for (var i = 1; i <= month; i++) {
        if (index === i) {
            //设置具体的样式
            var detail = {
                title: {
                    textStyle: {
                        color: '#a8a8a8',
                        fontSize: 13
                    },
                    text: i + '月工单分布'
                },
                tooltip: {'trigger': 'axis'},
                legend: {
                    x: 'right',
                    data: ['工单数', '同比', '环比'],
                    textStyle: {
                        color: '#a8a8a8',
                        fontSize: 13
                    },
                    triggerEvent: true,
                    selected: {
                        '工单数': true,
                        '同比': false,
                        '环比': false
                    },
                    itemStyle: {
                        normal: {
                            color: function (params) {
                                var colorList = ['#C1232B', '#B5C334', '#FCCE10'];
                                return colorList[params.dataIndex]
                            }
                        }
                    }
                },
                toolbox: {
                    show: true,
                    orient: 'vertical',
                    x: 'right',
                    y: 'center',
                    feature: {
                        'magicType': {'show': true, 'type': ['line', 'bar', 'stack', 'tiled']}
                    }
                },
                calculable: true,
                grid: {'y': 80, 'y2': 100},
                xAxis: [{
                    type: 'category',
                    data: chartData.get(i).major,
                    axisLabel: {
                        interval: 0,
                        textStyle: {
                            color: '#a8a8a8'
                        }
                    }
                }],
                yAxis: [
                    {
                        type: 'value',
                        name: '工单数(条)',
                        axisLabel: {
                            textStyle: {
                                color: '#a8a8a8'
                            }
                        }
                    },
                    {
                        type: 'value',
                        name: '百分比(%)',
                        axisLabel: {
                            textStyle: {
                                color: '#a8a8a8',
                                fontSize: 10,
                                fontStyle: "bold"
                            }
                        }
                    }
                ],
                series: [
                    {
                        name: '工单数',
                        type: 'bar',
                        itemStyle: {
                            normal: {
                                color: '#C1232B'
                            }
                        },
                        barWidth: 30,
                        markLine: {
                            symbol: ['arrow', 'none'],
                            symbolSize: [4, 2],
                            itemStyle: {
                                normal: {
                                    lineStyle: {color: 'orange'},
                                    barBorderColor: 'orange',
                                    label: {
                                        position: 'left',
                                        formatter: function (params) {
                                            return Math.round(params.value);
                                        },
                                        textStyle: {color: 'orange'}
                                    }
                                }
                            },
                            data: [{'type': 'average', 'name': '平均值'}]
                        },
                        data: chartData.get(i).total
                    },
                    {
                        name: '环比', 'yAxisIndex': 1, 'type': 'bar',
                        data: chartData.get(i).line,
                        itemStyle: {
                            normal: {
                                color: '#B5C334'
                            }
                        },
                        barWidth: 30
                    },
                    {
                        name: '同比', 'yAxisIndex': 1, 'type': 'bar',
                        data: chartData.get(i).yoy,
                        itemStyle: {
                            normal: {
                                color: '#FCCE10'
                            }
                        },
                        barWidth: 30
                    }
                ]
            };
            options.push(detail);
        } else {
            //添加数据
            var detail = {
                title: {text: i + '月工单分布'},
                series: [
                    {data: chartData.get(i).total},
                    {data: chartData.get(i).line},
                    {data: chartData.get(i).yoy}
                ]
            };
            options.push(detail);
        }
    }
    option.options = options;
}

最后再调用

myChart1.setOption(option);
myChart1.resize();
就可以正常工作了
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts时间轴Echarts中的一个组件,用于展示和控制数据随时间的变化。在Vue项目中,可以通过引入Echarts的图表实例来使用时间轴功能。首先,在main.js中引入Echarts的库,并将其挂载到Vue的原型上,以便在整个项目中可以方便地调用。接下来,在需要使用时间轴的组件中,可以通过在初始化Echarts画板时添加时间轴配置来实现。具体可以参考以下步骤: 1. 在Vue组件中引入Echarts的图表实例。 2. 在data函数中定义需要用到的数据,包括时间轴的数据和其他图表数据。 3. 在mounted生命周期钩子函数中初始化Echarts画板,并配置时间轴相关的配置项。 4. 在配置项中,可以使用timeline属性来配置时间轴的参数,包括时间轴类型、时间轴的起始和结束时间、时间轴的间隔和步长等。 5. 在初始化画板后,可以通过绑定timelinechanged事件来监听时间轴的变化,从而获取当前选中的时间轴下标或其他相关信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Echarts使用(Vue) 时间轴事件绑定、单图例多种切换](https://blog.csdn.net/YourNikee/article/details/105709340)[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_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值