<template>
    <chart :options="Opts" ref="bar" 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/bar";
import getBar from "../../src/_unit/bar";
export default {
    name: "DoBar",
    components: {
        chart: ECharts
    },
    props: {
        charsData: {
            type: Array,
            default: () => {
                return [
                    {
                        name: "Forest",
                        data: [320, 332, 301, 334]
                    },
                    {
                        name: "Steppe",
                        data: [220, 182, 191, 234]
                    },
                    {
                        name: "Desert",
                        data: [150, 232, 201, 154]
                    }
                ];
            }
        },
        xAxisData: {
            type: Array,
            default: () => {
                return ["2014年", "2015年", "2016年", "2017年"];
            }
        },
        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.value}`;
                }
            }
        },
        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 getBar(
                this.charsData,
                this.xAxisData,
                this.color,
                this.tooltipFormatter,
                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 bar = this.$refs.bar;
            let dataLen = bar.options.series[0].data.length;
            this.interval = setInterval(() => {
                bar.dispatchAction({
                    type: "downplay",
                    seriesIndex: 0,
                    dataIndex
                });
                dataIndex = (dataIndex + 1) % dataLen;
                bar.dispatchAction({
                    type: "highlight",
                    seriesIndex: 0,
                    dataIndex
                });
                bar.dispatchAction({
                    type: "showTip",
                    seriesIndex: 0,
                    dataIndex
                });
            }, this.animationTime);
        },
        stopAnimation() {
            clearInterval(this.interval);
        }
    },
    mounted() {
        if (this.animation) {
            this.animationFunction();
        }
    }
};
</script>