<template>
    <chart :options="Opts" ref="DoLineAndBar" theme="ovilia-green" 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 "echarts/lib/chart/bar";
import getlineAndBar from "../../src/_unit/lineAndBar";
export default {
    name: "DoLineAndBar",
    components: {
        chart: ECharts
    },
    props: {
        barData: {
            type: Object,
            default: () => {
                return {
                    name: "最低气温",
                    data: [11, 11, 15, 13, 12, 13, 10]
                };
            }
        },
        lineData: {
            type: Object,
            default: () => {
                return {
                    name: "最高气温",
                    data: [11, 11, 15, 13, 12, 13, 10]
                };
            }
        },
        xAxisData: {
            type: Array,
            default: () => {
                return ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
            }
        },
        color: {
            type: [Array, String],
            default: () => {
                return ["#358fff", "#32eaaa", "#e8a24c"];
            }
        },
        tooltipFormatter: {
            type: Function,
            default: params => {
                if(params.length){
                    var res = '';
                    for (var i = 0; i < params.length; i++) {
                        let returnData = params[i].data;
                        res += params[i].marker + params[i].seriesName + "：" + returnData + "<br>";
                    }
                    return res;
                }else{
                    return `${params.marker}${params.name}`;
                }
            }
        },
        lineOpt:{
            type: Object
        },
        barOpt:{
            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 {
            interval: null
        };
    },
    computed: {
        Opts() {
            return getlineAndBar(
                this.barData,
                this.lineData,
                this.xAxisData,
                this.color,
                this.tooltipFormatter,
                this.lineOpt,
                this.barOpt,
                this.changeOpt,
                this.type
            );
        }
    },
    watch: {
        animation: {
            immediate: true,
            handler(val) {
                if (val) {
                    this.animationFunction();
                } else {
                    this.stopAnimation();
                }
            }
        }
    },
    methods: {
        animationFunction() {
            let dataIndex = -1;
            let DoLineAndBar = this.$refs.DoLineAndBar;
            let dataLen = DoLineAndBar.options.series[0].data.length;
            this.interval = setInterval(() => {
                DoLineAndBar.dispatchAction({
                    type: "downplay",
                    seriesIndex: 0,
                    dataIndex
                });
                dataIndex = (dataIndex + 1) % dataLen;
                DoLineAndBar.dispatchAction({
                    type: "highlight",
                    seriesIndex: 0,
                    dataIndex
                });
                DoLineAndBar.dispatchAction({
                    type: "showTip",
                    seriesIndex: 0,
                    dataIndex
                });
            }, this.animationTime);
        },
        stopAnimation() {
            clearInterval(this.interval);
        }
    },
    mounted() {
        if (this.animation) {
            this.animationFunction();
        }
    }
};
</script>