<template>
    <div class="chart" :id="id"></div>
</template>

<script>
import util from "@/helper/util.js";
import partten from "@/helper/partten.js";
import * as echarts from "echarts";

export default {
    name: "multiple-y-line-chart",
    componentName: "multiple-y-line-chart",
    props: {
        width: {
            type: String,
            default: "100%",
        },
        height: {
            type: String,
            default: "13.889vh",
        },
        // 数据
        list: {
            type: Array,
            default: () => [
                {
                    title: "机舱震动x方向",
                    yAxis: {
                      min: -0.01,
                      max: 0,
                      unit: "",
                      position: "left",
                    },
                    value: [
                        {
                            text: "-0.003",
                            value: -0.003,
                        },
                        {
                            text: "-0.002",
                            value: -0.002,
                        },
                        {
                            text: "-0.003",
                            value: -0.006,
                        },
                        {
                            text: "-0.003",
                            value: -0.003,
                        },
                        {
                            text: "-0.002",
                            value: -0.002,
                        },
                        {
                            text: "-0.003",
                            value: -0.006,
                        },
                        {
                            text: "-0.003",
                            value: -0.003,
                        },
                        {
                            text: "-0.002",
                            value: -0.002,
                        },
                        {
                            text: "-0.003",
                            value: -0.006,
                        },
                    ],
                },
                {
                    title: "机舱震动y方向",
                    yAxis: {
                      min: -0.01,
                      max: 0,
                      unit: "",
                      position: "right",
                    },
                    value: [
                        {
                            text: "-0.01",
                            value: -0.01,
                        },
                        {
                            text: "-0.005",
                            value: -0.005,
                        },
                        {
                            text: "-0.008",
                            value: -0.008,
                        },
                        {
                            text: "-0.01",
                            value: -0.01,
                        },
                        {
                            text: "-0.005",
                            value: -0.005,
                        },
                        {
                            text: "-0.008",
                            value: -0.008,
                        },
                        {
                            text: "-0.01",
                            value: -0.01,
                        },
                        {
                            text: "-0.005",
                            value: -0.005,
                        },
                        {
                            text: "-0.008",
                            value: -0.008,
                        },
                    ],
                },
                {
                    title: "机舱震动最大偏移值",
                    yAxis: {
                      min: 0,
                      max: 1,
                      unit: "",
                      position: "left",
                    },
                    value: [
                        {
                            text: "1",
                            value: 1,
                        },
                        {
                            text: "0.05",
                            value: 0.05,
                        },
                        {
                            text: "0.5",
                            value: 0.5,
                        },
                        {
                            text: "1",
                            value: 1,
                        },
                        {
                            text: "0.05",
                            value: 0.05,
                        },
                        {
                            text: "0.5",
                            value: 0.5,
                        },
                        {
                            text: "1",
                            value: 1,
                        },
                        {
                            text: "0.05",
                            value: 0.05,
                        },
                        {
                            text: "0.5",
                            value: 0.5,
                        },
                    ],
                },
                {
                    title: "风速1",
                    yAxis: {
                      min: 0,
                      max: 10,
                      unit: "",
                      position: "right",
                    },
                    value: [
                        {
                            text: "1",
                            value: 1,
                        },
                        {
                            text: "5",
                            value: 5,
                        },
                        {
                            text: "3",
                            value: 1,
                        },
                        {
                            text: "1",
                            value: 1,
                        },
                        {
                            text: "5",
                            value: 5,
                        },
                        {
                            text: "3",
                            value: 1,
                        },
                        {
                            text: "1",
                            value: 1,
                        },
                        {
                            text: "5",
                            value: 5,
                        },
                        {
                            text: "3",
                            value: 1,
                        },
                    ],
                },
                {
                    title: "风速2",
                    yAxis: {
                      min: 0,
                      max: 10,
                      unit: "",
                      position: "left",
                    },
                    value: [
                        {
                            text: "1",
                            value: 1,
                        },
                        {
                            text: "2",
                            value: 2,
                        },
                        {
                            text: "3",
                            value: 1,
                        },
                        {
                            text: "1",
                            value: 1,
                        },
                        {
                            text: "2",
                            value: 2,
                        },
                        {
                            text: "3",
                            value: 1,
                        },
                        {
                            text: "1",
                            value: 1,
                        },
                        {
                            text: "2",
                            value: 2,
                        },
                        {
                            text: "3",
                            value: 1,
                        },
                    ],
                },
            ],
        },
        showLegend: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            id: "",
            chart: null,
            color: ["#323E6F", "#1DA0D7", "#05BB4C", "#EDB32F", "#DB5520"],
        };
    },
    computed: {
        legend() {
            return this.list.map((t) => {
                return t.title;
            });
        },
        xdata() {
            return this.list[0].value.map((t) => {
                return t.text;
            });
        },
        yAxis() {
            let result = [];
            let p = {left: 0, right: 0};
            this.list.forEach((item, index) => {
                result.push({
                    type: "value",
                    name: `${item.title}${item.yAxis.unit}`,
                    nameLocation: p[item.yAxis.position] % 2 == 0 ? "end" : "start",
                    min: item.yAxis.min,
                    max: item.yAxis.max,
                    position: item.yAxis.position,
                    offset: p[item.yAxis.position] * 60,
                    axisLabel: {
                        formatter: "{value}",
                        fontSize: util.vh(14),
                    },
                    //分格线
                    splitLine: {
                        lineStyle: {
                            color: partten.getColor("gray"),
                            type: "dashed",
                        },
                    },
                    axisLine: {
                      show: true,
                    },
                    axisTick: {
                      show: true
                    }
                });
                p[item.yAxis.position]++;
            });

            return result;
        },
        series() {
            let result = [];

            this.list.forEach((value, index) => {
                result.push({
                    name: value.title,
                    type: "line",
                    smooth: true,
                    zlevel: index,
                    lineStyle: {
                        normal: {
                            color: this.color[index],
                            width: 1,
                        },
                    },
                    yAxisIndex: index,
                    data: value.value.map((t) => {
                        return t.value;
                    }),
                });
            });

            return result;
        },
    },
    methods: {
        initChart() {
            const chart = echarts.init(this.$el);

            let option = {
                color: this.color,
                tooltip: {
                    trigger: "axis",
                    backgroundColor: partten.getColor("gray"),
                    textStyle: {
                        color: "#fff",
                        fontSize: util.vh(16),
                    },
                },
                legend: {
                    show: this.showLegend,
                    data: this.legend,
                    right: 56,
                    icon: "circle",
                    itemWidth: 6,
                    inactiveColor: partten.getColor("gray"),
                    textStyle: {
                        color: partten.getColor("grayl"),
                        fontSize: 12,
                    },
                },
                dataZoom: [{
                    show: true,
                    height: 30,
                    xAxisIndex: [0],
                    bottom: 30,
                    "start": 0,
                    "end": 10,
                    handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
                    handleSize: '110%',
                    handleStyle: {
                        color: "#05bb4c",
                    },
                    textStyle:{
                        color:"rgba(204,187,225,0.5)",
                    },
                    fillerColor:"rgba(5,187,76,0.4)",
                    borderColor: "rgba(5,187,76,0.5)",

                }, {
                    type: "inside",
                    show: true,
                    height: 15,
                    start: 1,
                    end: 35
                }],
                grid: {
                    top: util.vh(80),
                    left: util.vh(80),
                    right: util.vh(80),
                    bottom: util.vh(64),
                    containLabel: true,
                },
                xAxis: [
                    {
                        type: "category",
                        boundaryGap: false,
                        axisLabel: {
                            formatter: "{value}",
                            fontSize: util.vh(14),
                            textStyle: {
                                color: partten.getColor("gray"),
                            },
                        },
                        data: this.xdata,
                    },
                ],
                yAxis: this.yAxis,
                series: this.series,
            };
            console.log(option)
            chart.clear();
            chart.setOption(option);

            this.resize = function() {
                chart.resize();
            };

            window.addEventListener("resize", this.resize);
        },
        datazoom: function (start, end) {
            const chart = echarts.getInstanceByDom(this.$el);
            chart.dispatchAction({
                type: 'dataZoom',
                start: start,
                end: end,
            });
        },
    },
    created() {
        this.id = "pie-chart-" + util.newGUID();
    },
    mounted() {
        this.$nextTick(() => {
            this.$el.style.width = this.width;
            this.$el.style.height = this.height;
            this.initChart();
        });
    },
    updated() {
        this.$nextTick(() => {
            this.initChart();
        });
    },
    unmounted() {
        window.removeEventListener("resize", this.resize);
    },
};
</script>

<style lang="less">
.chart {
    width: 100%;
    height: 100%;
    display: inline-block;
}
</style>
