<template>
    <div style="min-height: 100px;">
            <div class="timeAnalysis" >
                <canvas  :style="{width:width+'px'}" class="timeAnalysis" canvas-id="timeAnalysis" id="timeAnalysis" @tap="handleTap"/>
            </div>
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
import NoData from "../noData.vue";
import Analyse from "../analyse.vue";
var uChartsInstance = {};
export default {
    name: "timeAnalysis",
    components: {Analyse, NoData},
    data() {
        return {
            width:0,
            isShowData: false,
            analyseInfo:{
                analysisins_type: 1105,
                analysisins_format: 2000
            },
        }
    },
    props: {
        data: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        data: {
            handler(value) {
                console.log('value',value)
                this.handleCarData(value)
            },
            immediate:true,
            deep:true
        }
    },
    methods: {
        getSplitNumber(value){
            if (value === 0){
                return 5
            }else{
                let sum = value + value *0.2
                if (sum>0 && sum<5){
                    return 5
                }else{
                    return  Number((sum / 5).toFixed(0)) * 5
                }
            }
        },
        //点击事件
        handleTap(e){
            uChartsInstance[e.target.id].touchLegend(e);
            uChartsInstance[e.target.id].showToolTip(e,{
              formatter: (item, category, index, opts) =>{
                return item.name + ":" + item.data + '分钟'
              }
            });
        },
        // 处理传入的数据
        handleCarData(value) {
            console.log('handleCarData',value)
            let res = {
                categories:value.categories,
                series:value.series
            }
            let max = this.getSplitNumber(value.max)
            this.drawCharts('timeAnalysis', res,max)
        },
        drawCharts(id, data,max) {
            const ctx = uni.createCanvasContext(id, this);
            let _this = this
            let phoneInfo = uni.getStorageSync('phoneInfo')
            this.width =  phoneInfo.screenWidth - 16
            uChartsInstance[id] = new uCharts({
                type: "line",
                context: ctx,
                width: _this.width,
                height: 240,
                categories: data.categories,
                series: data.series,
                animation: false,
                rotate: false,
                canvas2d: true,
                rotateLock: false,
                background: "#FFFFFF",
                color: ["#1E80FF", "#00B6FF","#0FC862"],
                padding: [15, 30, 5, 5],
                dataLabel: false,
                enableScroll: false,
                xAxis: {
                    disableGrid: true,
                    formatter:(val)=>{
                        if (val===0 || val===4 || val===8 || val===12 || val===16 || val===20 || val===23){
                            if (val===23){
                                return '24:00'
                            }else{
                                return `${val}:00`
                            }
                        }else{
                            return ''
                        }
                    }
                },
                yAxis: {
                    gridType: "dash",
                    dashLength: 2,
                    showTitle:true,
                    data:[{
                        title:'单位: 分钟',
                        titleOffsetY: -5,
                        min:0,
                        max:max,
                    }]
                },
                legend: {
                    show: true,
                    position: "bottom",
                    lineHeight: 25,
                    float: 'center'

                },
                extra: {
                    line: {
                        type: "curve",
                        width: 2,
                        activeType: "hollow"
                    }
                }
            });

        },

    }
}
</script>

<style scoped lang="scss">
.timeAnalysis{
    width: 100%;
    .timeAnalysis{
        width: 100%;
        height: 220px;
        margin-top: 12px;
    }
}
</style>
