<template>
    <div class="consumptionCompare">
        <canvas v-if="!consumptionPath" class="consumption" canvas-id="consumption" id="consumption"/>
        <image v-if="consumptionPath" :src="consumptionPath" class="consumption"></image>
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
var uChartsInstance = {};
export default {
    name: "consumptionCompare",
    data() {
        return {
            width: 0,
            consumptionPath:''
        }
    },
    props: {
        data: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        data: {
            handler(value) {
                console.log('carDatawatch', value)
                this.handleCarData(value)
            }
        }
    },
    methods: {
        // 处理传入的数据
        handleCarData(value) {
            console.log('value',value)
            let res = value
            this.drawCharts('consumption', res)
        },
        drawCharts(id, data) {
            const ctx = uni.createCanvasContext(id, this);
            let _this = this
            let phoneInfo = uni.getStorageSync('phoneInfo')
            this.width =  phoneInfo.screenWidth - 32
            uChartsInstance[id] = new uCharts({
                type: "area",
                context: ctx,
                width:  _this.width,
                height: 220,
                categories: data.categories,
                series: data.series,
                animation: true,
                rotate: false,
                rotateLock: false,
                background: "#FFFFFF",
                color: ["#CAD0DA", "#1E80FF"],
                padding: [15, 5, 5, 5],
                dataLabel: false,
                enableScroll: false,
                xAxis: {
                    disableGrid: true
                },
                yAxis:{
                    showTitle:true,
                    splitNumber:4,
                    data: [
                        {
                            index:0,
                            title:'单位: 笔',
                            min: 0,
                            max: 80,
                            position: "left",
                            titleOffsetY: -5
                        },
                        {
                            index:1,
                            title:'单位: 辆',
                            min: 0,
                            max: 400,
                            position: 'right',
                            titleOffsetY: -5,
                            titleOffsetX: 5
                        },
                    ]
                },
                legend: {
                    show: true,
                    position: "bottom",
                    lineHeight: 25,
                    float: 'center'

                },
                extra: {
                    area: {
                        type: "curve",
                        opacity: 0.9,
                        addLine: true,
                        width: 2,
                        gradient: true,
                        activeType: "hollow"
                    }
                }
            });
            setTimeout( ()=>{
                this.canvasToTempImage('consumption')
            },2000)
        },
        canvasToTempImage(id){
            uni.canvasToTempFilePath({
                canvasId:id,
                complete:(res)=>{
                    if (res.tempFilePath){
                        uni.getFileSystemManager().readFile({
                            filePath: res.tempFilePath,
                            encoding: 'base64',
                            success: res => {
                                let base64 = 'data:image/png;base64,' + res.data;
                                this.consumptionPath = base64
                            }
                        })
                    }
                }
            },this)
        },
    }
}
</script>

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