<template>
    <div class="moneyCompare">
        <canvas  class="compare" canvas-id="compare" id="compare"  @tap="tap"/>
<!--        <image v-if="comparePath" class="compare" :src="comparePath"></image>-->
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
var uChartsInstance = {};
export default {
    name: "moneyCompare",
    data() {
        return {
          width:0,
          comparePath:''
        }
    },
    props: {
        data: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        data: {
            handler(value) {
                console.log('营收对比value',value)
                this.comparePath = ''
                this.handleCarData(value)
            },
            immediate: true,
            deep: true
        }
    },
    methods: {
        tap(e){
            uChartsInstance[e.target.id].showToolTip(e, {
                formatter: (item, category, index, opts) => {
                    return item.name + ":" + item.data;
                }
            });
            uChartsInstance[e.target.id].touchLegend(e);
        },
        // 处理传入的数据
        handleCarData(value) {
            let res = value
            this.drawCharts('compare', res)
        },
        drawCharts(id, data) {
            const ctx = uni.createCanvasContext(id, this);
            let _this = this
            let phoneInfo = uni.getStorageSync('phoneInfo')
            this.width = phoneInfo.screenWidth
            uChartsInstance[id] = new uCharts({
                type: "column",
                context: ctx,
                width: _this.width,
                height: 208,
                categories: data.categories,
                series: data.series,
                animation: false,
                rotate: false,
                rotateLock: false,
                background: "#FFFFFF",
                color: ["#1E80FF", "#00B6FF","#ACB9CD"],
                padding: [15, 40, 5, 0],
                dataLabel: false,
                enableScroll: false,
                legend: {
                    show: true,
                    position: "bottom",
                    lineHeight: 25,
                    float: 'center'

                },
                xAxis: {
                    disableGrid: true,
                    axisLineColor:'#F2F2F5'
                },
                yAxis: {
                    showTitle:false,
                    splitNumber:4,
                    gridColor:'#F2F2F5',
                    data: [
                        {
                            min: 0,
                            titleOffsetX:15,
                            titleOffsetY:-5,
                            fontSize:12,
                            axisLineColor:'#F2F2F5'
                        }
                    ]
                },
                extra: {
                    column: {
                        type: "group",
                        width: 5,
                        seriesGap:4,
                        activeBgColor: "#000000",
                        activeBgOpacity: 0.08,
                        barBorderRadius:[3,3,0,0]
                    }
                }
            });
          setTimeout( ()=>{
            // this.canvasToTempImage('compare')
          },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.comparePath = base64
                }
              })
            }
          }
        },this)
      },
    }
}
</script>

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