<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) {
                this.handleCarData(value)
            },
            immediate:true,
            deep:true
        }
    },
    methods: {
        //点击事件
        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) {
            let res = {
                categories:value.categories,
                series:value.series
            }
            this.drawCharts('timeAnalysis', res)
        },
        drawCharts(id, data) {
            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:180,
                    }]
                },
                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>
