<template>
    <div class="revenueTrends">
        <canvas v-if="!trendsPath" class="trends" canvas-id="trends" id="trends"/>
        <image v-if="trendsPath" class="trends" :src="trendsPath"></image>
    </div>
</template>

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

                },
                xAxis: {
                    disableGrid: true,
                  axisLineColor:'#F2F2F5'
                },
                yAxis: {
                    splitNumber:4,
                    showTitle:true,
                    gridColor:'#F2F2F5',
                    data:[
                        {
                            min:0,
                            max:12,
                            title:'单位: 千万',
                            titleOffsetY:-5,
                            titleOffsetX:8,
                            fontSize:12,
                          axisLineColor:'#F2F2F5'
                        }
                    ]
                },
                extra: {
                    area: {
                        type: "curve",
                        opacity: 0.8,
                        addLine: true,
                        width: 2,
                        gradient: true,
                        activeType: "hollow"
                    }
                }
            });
          setTimeout( ()=>{
            this.canvasToTempImage('trends')
          },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.trendsPath = base64
                }
              })
            }
          }
        },this)
      },
    }
}
</script>

<style scoped lang="scss">
.revenueTrends{
    width: 100%;
    .trends{
        width: calc(100vw - 32px);
        height: 208px;
    }
}
</style>
