import * as echarts from "echarts";
import { handelWatermark, chartsWidth,emptyData} from "../../util/processingData"
export default {
    data() {
        return {
            //柱状图配置
            columnar: {
                title: {
                    text: "",
                    left: "center",
                    textStyle: {
                        fontSize: 15,
                        fontWeight: "normal"
                    },
                },
                tooltip: {
                    trigger: "axis",
                    textStyle: {
                        align: "left",
                    },
                    axisPointer: {
                        type: "cross",
                        crossStyle: {
                            color: "#999",
                        },
                    },
                    //   formatter: function (params) {
                    //   var res = params[0].axisValueLabel;

                    //   for (let i = 0; i <= params.length - 1; i++) {
                    //     res += `</br>${params[i].marker} <span style="font-weight: bold">${params[i].value[1]}</span> ${params[i].seriesName}`;
                    //   }
                    //   return "111";
                    // },
                },
                dataZoom: [
                    {
                        type: "inside",
                        start: 0,
                        end: 100,
                    },
                    {  type: "inside",
                        start: 0,
                        end: 100,
                    },
                ],
                

                legend: {
                    right: 100,
                    formatter: function (name) {

                        return name;
                    },
                },
                xAxis: [
                    {
                        type: "category",
                        data: [],
                        // axisLabel:{
                        //     formatter:(value)=>{                             
                        //         let label = null
                        //         if (value.search("y") !== -1) {
                        //             if (value.search("p") !== -1) {
                        //                 label = value.replace("p",".").replace("y","years")

                        //             }else{
                        //                 label = value.replace("y","years")
                        //             }

                        //         }else{
                        //             label=`${value}days`
                        //         }
                        //         return label
                        //     }
                        // }  
                    },
                ],
                yAxis: [
                    {
                        type: "value",
                        // name: "Precipitation",
                        // min: 0,
                        // max: 250,
                        // interval: 50,
                        axisLabel: {
                            formatter: function (value) {

                                let label = value / 1000000
                                return `${label}M`
                            },
                        },
                    },

                ],
                series: [
                    {

                        type: "bar",
                        colorBy: "data",
                        // tooltip: {
                        //   valueFormatter: function (value) {
                        //     return value + " ml";
                        //   },
                        // },
                        data: [],
                        itemStyle: {
                            color: "#fdaa56",
                        },
                    },
                    // {
                    //     name: "Change 24h",
                    //     type: "bar",
                    //     itemStyle: {
                    //         color: "#607d8b",
                    //     },
                    //     // tooltip: {
                    //     //   valueFormatter: function (value) {
                    //     //     return value + " ml";
                    //     //   },
                    //     // },
                    //     data: [],
                    // },
                ],
            },
            params: {
                req: "holderBalance",
                radius: "btc"
            },
            BalanceLoading: false,
            value1: "",
            pickerOptions: {//指定时间以前的日期禁用
                disabledDate: (time) => {
                    return time.getTime() > new Date(this.DateTime).getTime()
                },
            },
            DateTime: "",

        }
    },
    mounted() {
        this.fetchHolderBalance(this.params.req, this.params.radius)
    },
    methods: {
        //初始化柱状图图表
        inCharts() {
            this.myChart = echarts.init(document.getElementById("holderBalance"));

            this.myChart.setOption(this.columnar);
            chartsWidth("holderBalance", this);
            window.addEventListener("resize", () => {
                if (this.myChart) this.myChart.resize();
            });
        },
        async fetchHolderBalance(req, radius, start) {
            this.BalanceLoading = true
            const { data } = await this.$API.getHolder(req, radius, start)
            this.balanceData = data.data
            //空数据处理
            let noData = emptyData(data.data, this.columnar.series, this.inCharts, this)
            if (noData) {
                this.BalanceLoading = false
                return
            }
            this.columnar.xAxis[0].data.length = 0
            this.columnar.series[0].data.length = 0
            //添加水印
            this.columnar = handelWatermark(this.columnar)

            //对象数据转换成数组
            let arr = Object.entries(this.balanceData)
            arr.forEach(item => {
                if (item[0] == "id" || item[0] == "dateTime") {
                    if (item[0] == "dateTime") {
                        this.DateTime = item[1].split("T")[0]
                        this.columnar.title.text = `${this.DateTime}`
                    }
                    return

                } else {
                    //    this.columnar.xAxis[0].data.push(item[0].replace("holderBalance",""))
                    //X轴显示处理
                    let xLabel = item[0].replace("holderBalance", "")
                    if (xLabel.search("y") !== -1) {
                        if (xLabel.search("p") !== -1) {
                            this.columnar.xAxis[0].data.push(xLabel.replace("p", ".").replace("y", "years"))

                        } else {
                            this.columnar.xAxis[0].data.push(xLabel.replace("y", "years"))
                        }

                    } else {
                        this.columnar.xAxis[0].data.push(`${xLabel}days`)
                    }

                    this.columnar.series[0].data.push(item[1])
                }
            })

            this.inCharts()

            this.BalanceLoading = false
        },
        //时间选择
        handeldate() {

            this.fetchHolderBalance(this.params.req, this.params.radius, this.value1)
        }
    }
}