<template>
    <div class="levelTop">
        <canvas v-if="!levelTopPath" class="level" canvas-id="levelTop" id="levelTop"/>
        <image v-if="levelTopPath" class="level" :src="levelTopPath"></image>
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
var uChartsInstance = {};
export default {
    name: "levelTop",
    data() {
        return {
            width:0,
            levelTopPath:''
        }
    },
    props: {
        data: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        data: {
            handler(value) {
                console.log('carDatawatch', value)
                this.handleCarData(value)
            }
        }
    },
    methods: {
        // 处理传入的数据
        handleCarData(value) {
            let res = value
            this.drawCharts('levelTop', 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: "column",
                context: ctx,
                width: _this.width,
                height: 272,
                categories: data.categories,
                series: data.series,
                animation: true,
                rotate: false,
                rotateLock: false,
                background: "#FFFFFF",
                color: ["#1E80FF","#ACB9CD","#CAD0DA" ],
                padding: [15, 5, 5, 5],
                dataLabel: false,
                enableScroll: false,
                legend: {
                    show: true,
                    position: "bottom",
                    lineHeight: 25,
                    float: 'center'

                },
                xAxis: {
                    disableGrid: true
                },
                yAxis: {
                    showTitle:true,
                    splitNumber:5,
                    data: [
                        {
                            min: 0,
                            max: 100,
                            title:'单位: /%',
                            titleOffsetY:-5,
                            titleOffsetX:-5
                        }
                    ]
                },
                extra: {
                    column: {
                        type: "stack",
                        width: 20,
                        activeBgColor: "#000000",
                        activeBgOpacity: 0.08,
                        labelPosition: "center"
                    }
                }
            });
            setTimeout( ()=>{
                this.canvasToTempImage('levelTop')
            },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.levelTopPath = base64
                            }
                        })
                    }
                }
            },this)
        },
    }
}
</script>

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