<template>
    <chart :options="lineOpts" ref="line" autoresize :style="{width,height}" />
</template>

<script>
import ECharts from "../../src/components/ECharts";
import "echarts/lib/component/legend";
import "echarts/lib/component/tooltip";
import "echarts/lib/chart/line";
import getLine from "../../src/_unit/line";
export default {
    name: "DoLine",
    components: {
        chart: ECharts
    },
    props: {
        charsData: {
            type: Array,
            default: () => {
                return [
                    {
                        name: "人均年收入",
                        data: [11, 11, 15, 13, 12],
                        xAxisData: [
                            "2014年",
                            "2015年",
                            "2016年",
                            "2017年",
                            "2018年"
                        ]
                    }
                ];
            }
        },
        xAxisData: {
            type: Array,
            default: () => {
                return ["2014年", "2015年", "2016年", "2017年", "2018年"];
            }
        },
        color: {
            type: [Array, String],
            default: () => {
                return ["#34fba8", "#1e6af4"];
            }
        },
        lineOpt: {
            type: Object
        },
        changeOpt: {
            type: Object
        },
        type: {
            type: String,
            default: () => {
                return "white";
            },
            validator: value => {
                return ["dark", "white"].indexOf(value) !== -1;
            }
        },
        animation: {
            type: Boolean,
            default: () => {
                return false;
            }
        },
        animationTime: {
            type: Number,
            default: () => {
                return 1000;
            }
        },
        width: {
            type: String,
            default: () => {
                return "600px";
            }
        },
        height: {
            type: String,
            default: () => {
                return "400px";
            }
        }
    },
    data() {
        return {
            line: null,
            interval: null
        };
    },
    computed: {
        lineOpts() {
            return getLine(
                this.charsData,
                this.xAxisData,
                this.color,
                this.changeOpt,
                this.lineOpt,
                this.type
            );
        }
    },
    watch: {
        animation: {
            immediate: true,
            handler(val) {
                if (val) {
                    this.animationFunction();
                } else {
                    this.stopAnimation();
                }
            }
        }
    },
    methods: {
        animationFunction() {
            let dataIndex = -1;
            let line = this.$refs.line;
            let dataLen = line.options.series[0].data.length;
            this.interval = setInterval(() => {
                line.dispatchAction({
                    type: "downplay",
                    seriesIndex: 0,
                    dataIndex
                });
                dataIndex = (dataIndex + 1) % dataLen;
                line.dispatchAction({
                    type: "highlight",
                    seriesIndex: 0,
                    dataIndex
                });
                line.dispatchAction({
                    type: "showTip",
                    seriesIndex: 0,
                    dataIndex
                });
            }, this.animationTime);
        },
        stopAnimation() {
            clearInterval(this.interval);
        }
    },
    mounted() {
        if (this.animation) {
            this.animationFunction();
        }
    }
};
</script>

