RadarChart.js 4.69 KB
// packageStudyCenter/pages/myTask/components/RadarChart/RadarChart.js
import { getSystemInfoSync } from '~/utils/systemInfo'
import uCharts from '~/packageStudyCenter/ucharts/index.js'
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
Component({
  properties: {
    chartData: {
      type: Object,
      value: {},
      observer: function(newVal) {
        this.checkData(newVal);
        if (this.data.hasData) {
          this.scheduleDraw();
        }
      }
    },
    heightClass: {
      type: String,
      value: ''
    },
    labelColors: {
      type: Array,
      value: ['#666666', '#666666', '#666666', '#666666', '#666666', '#666666', '#666666', '#666666', '#666666'],
      observer: function(newVal) {
        if (this.data.hasData && this.chartDrawn) {
          this.scheduleDraw();
        }
      }
    }
  },

  data: {
    IMG_URL,
    hasData: false,
    chartHeight: 272
  },

  lifetimes: {
    ready() {
      this.initHeight();
      this.checkData(this.properties.chartData);
    },
    
    moved() {
      if (this.data.hasData && !this.chartDrawn) {
        this.scheduleDraw();
      }
    },
    
    detached() {
      if (this.drawTimer) {
        clearTimeout(this.drawTimer);
      }
    }
  },

  pageLifetimes: {
    show() {
      if (this.data.hasData && !this.chartDrawn) {
        this.scheduleDraw();
      }
    }
  },

  methods: {
    initHeight() {
      const chartHeight = this.properties.heightClass === 'big-height' ? 300 : 272;
      this.setData({ chartHeight });
    },

    checkData(chartData) {
      const hasData = chartData && 
                     chartData.categories && 
                     chartData.categories.length > 0 && 
                     chartData.series && 
                     chartData.series.length > 0 &&
                     chartData.series[0].data &&
                     chartData.series[0].data.length > 0;
      
      this.setData({ hasData });
    },

    scheduleDraw() {
      if (this.drawTimer) {
        clearTimeout(this.drawTimer);
      }
      
      this.drawTimer = setTimeout(() => {
        this.drawCharts();
      }, 100);
    },

    drawCharts() {
      return new Promise((resolve, reject) => {
        const query = wx.createSelectorQuery().in(this);
        
        query.select('#radarChart').fields({ node: true, size: true }).exec((res) => {
          if (!res[0]) {
            reject(new Error('未找到canvas元素'));
            return;
          }

          const canvas = res[0].node;
          if (!canvas) {
            reject(new Error('canvas节点为空'));
            return;
          }

          try {
            const ctx = canvas.getContext('2d');
            const systemInfo = getSystemInfoSync();
            const pixelRatio = systemInfo.pixelRatio;
            
            const actualWidth = res[0].width * pixelRatio;
            const actualHeight = res[0].height * pixelRatio; 
            canvas.width = actualWidth;
            canvas.height = actualHeight;

            const config = {
              type: "radar",
              animation: false,
              background: "#FFFFFF",
              fontSize: 12,
              canvas2d: true,
              categories: this.properties.chartData.categories,
              color: ["#3B73FF"],
              context: ctx,
              dataLabel: false,
              extra: {
                radar: {
                  gridType: "radar",
                  gridColor: "#dae8fd",
                  gridCount: 3,
                  opacity: 0.2,
                  max: 3,
                  labelShow: true,
                  labelColor: this.properties.labelColors,
                  labelFontSize: 12,
                  dataPointShape: false,
                  axisLabelTofix: 1,
                  labelFormatter: (val) => typeof val === 'number' ? val.toFixed(1) : val,
                  border: false,
                  borderColor: "#3B73FF",
                  borderWidth: 1,
                  fillOpacity: 0.2
                }
              },
              height: actualHeight,
              legend: { show: false },
              padding: [5, 5, 5, 5],
              pixelRatio: pixelRatio,
              series: this.properties.chartData.series,
              width: actualWidth,
              xAxis: { disableGrid: false },
              yAxis: { data: [{ min: 0 }] }
            };

            new uCharts(config);
            this.chartDrawn = true;
            resolve();
          } catch (error) {
            console.error('绘制雷达图失败:', error);
            reject(error);
          }
        });
      });
    },

    // 外部调用方法:强制重绘
    redraw() {
      this.chartDrawn = false;
      this.scheduleDraw();
    }
  }
});