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

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

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

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