<template>
    <div class="main">
        <canvas class="canvas" canvas-id="monthTotal" id="monthTotal" @tap="handleTap"/>
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
var uChartsInstance = {};
export default {
    name: "monthTotal",
    data(){
        return {

        }
    },
    props: {
        data: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        data: {
            handler(value) {
                this.handleCarData(value)
            },
            immediate:true,
            deep:true
        }
    },
    methods:{
        handleTap(e){
            uChartsInstance[e.target.id].showToolTip(e,{
                formatter: (item, category, index, opts) => {
                    let text = ''
                    if (item.name === '车流量'){
                       text = `入区 ${opts.series[0].info[index].allCarCount} 辆, 入区率 ${opts.series[0].info[index].rate} %`
                        return text
                    }else{
                        return `金额 ${opts.series[0].info[index].money} 元, 单车价值 ${((opts.series[0].info[index].money) / opts.series[0].info[index].allCarCount).toFixed(2)} 元`
                    }

                }
            });
        },
        // 处理传入的数据
        handleCarData(value) {
            let res = {
                categories: value.categories,
                series: value.series
            }
            this.drawCharts('monthTotal', res)
        },
        drawCharts(id, data) {
            const ctx = uni.createCanvasContext(id, this);
            let phoneInfo = uni.getStorageSync('phoneInfo')
            let width = phoneInfo.screenWidth-32
            uChartsInstance[id] = new uCharts({
                type: "mix",
                context: ctx,
                width: width,
                height: 300,
                categories: data.categories,
                series: data.series,
                animation: false,
                rotate: false,
                rotateLock: false,
                canvas2d: true,
                background: "#FFFFFF",
                color: ["#1E80FF", "#00B6FF","#38C275","#6B6FFF","#6B85AE"],
                padding: [35, 10, 5, 5],
                dataLabel: false,
                enableScroll: false,
                xAxis: {
                    disabled: false,
                    disableGrid: true,
                    axisLine: false,
                    fontColor: '#777777',
                    fontSize:data.categories.length<10 ? 13 : 11
                },
                yAxis:{
                    showTitle:true,
                    data:[{
                        position: "left",
                        title: '车流量 / 万辆 ',
                        disabled: false,
                        axisLine: false,
                        titleOffsetY: -5,
                        titleOffsetX: 15,
                        min: 0,
                    },
                    {
                        position: "right",
                        title: '交易金额 / 万元',
                        disabled: false,
                        axisLine: false,
                        titleOffsetY: -5,
                        titleOffsetX: -15,
                        min: 0,
                    }]
                },
                legend: {
                    show: true,
                    position: "bottom",
                    lineHeight: 25,
                    float:'center'
                },
                extra: {
                    mix:{
                        column:{
                            type: "group",
                            width: 12,
                            activeBgColor: "#000000",
                            activeBgOpacity: 0.08
                        },
                        line:{
                            type: "curve",
                            width: 2,
                            activeType: "hollow"
                        }
                    }
                }
            });
        },
    }
}
</script>

<style scoped lang="scss">
.main{
    width: 100%;
    height: 300px;
    .canvas{
        width: 100%;
        height: 100%;
    }
}
</style>
