Преглед изворни кода

fix(charts): 添加clearRect方法的兼容性处理

- 在CanvasContext接口中添加clearRect方法声明为可选方法
- 将draw-charts.ts中所有context.clearRect调用改为可选链调用(context.clearRect?.())
- 修复Taro小程序CanvasContext不支持clearRect方法导致的运行时错误

在Taro小程序中,Canvas的clearRect方法可能不可用,
使用可选链操作符避免方法不存在时的运行时错误。

🤖 Generated with [Claude Code](https://claude.com/claude-code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname пре 3 недеља
родитељ
комит
7a3a9759b4

+ 9 - 2
mini-ui-packages/mini-charts/src/components/ColumnChart.tsx

@@ -53,6 +53,8 @@ export const ColumnChart: React.FC<ColumnChartProps> = (props) => {
     ...baseProps
   } = props;
 
+  console.debug('[ColumnChart] 接收到的props:', { categories, series, config, baseProps })
+
   const chartRef = React.useRef<any>(null);
 
   /**
@@ -60,7 +62,7 @@ export const ColumnChart: React.FC<ColumnChartProps> = (props) => {
    */
   const defaultConfig = useMemo(() => {
     const legendConfig: LegendConfig = legend ? { show: true } : { show: false };
-    return {
+    const result = {
       legend: legendConfig,
       fontSize,
       background,
@@ -77,6 +79,8 @@ export const ColumnChart: React.FC<ColumnChartProps> = (props) => {
         }
       }
     };
+    console.debug('[ColumnChart] 计算后的defaultConfig:', result)
+    return result
   }, [legend, fontSize, background, animation, dataLabel, xAxis, yAxis, columnType, categories.length, baseProps.width]);
 
   /**
@@ -97,13 +101,16 @@ export const ColumnChart: React.FC<ColumnChartProps> = (props) => {
     baseProps.onTouchStart?.(e);
   };
 
+  const finalConfig = { ...defaultConfig, ...config }
+  console.debug('[ColumnChart] 最终传递给BaseChart的config:', finalConfig)
+
   return (
     <BaseChart
       {...baseProps}
       categories={categories}
       series={series}
       type="column"
-      config={{ ...defaultConfig, ...config }}
+      config={finalConfig}
       onTouchStart={handleTouchStart}
     />
   );

+ 6 - 0
mini-ui-packages/mini-charts/src/lib/charts/u-charts.ts

@@ -127,6 +127,8 @@ export interface CanvasContext {
   setShadow?(offsetX: number, offsetY: number, blur: number, color: string): void;
   setLineDash?(segments: number[]): void;
   draw?(): void;
+  // clearRect 方法(Taro 小程序可能不支持,使用可选)
+  clearRect?(x: number, y: number, width: number, height: number): void;
   [key: string]: any;
 }
 
@@ -360,6 +362,8 @@ export class uCharts extends uChartsEvent {
   constructor(opts: ChartsConfig) {
     super();
 
+    console.debug('[uCharts] 构造函数开始, opts:', opts)
+
     // 确保必需的属性存在
     if (!opts.height) opts.height = 300;
     if (!opts.width) opts.width = 300;
@@ -514,7 +518,9 @@ export class uCharts extends uChartsEvent {
     };
     this.opts = opts;
     this.config = config;
+    console.debug('[uCharts] 准备调用drawCharts, type:', opts.type, 'opts:', opts, 'config:', config)
     drawCharts.call(this, opts.type || '', opts as any, config, this.context);
+    console.debug('[uCharts] drawCharts调用完成')
   }
 
   /**

+ 18 - 18
mini-ui-packages/mini-charts/src/lib/draw-controllers/draw-charts.ts

@@ -296,7 +296,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -309,7 +309,7 @@ export const drawCharts: DrawChartsFunction = function(
       });
       break;
     case 'map':
-      context.clearRect(0, 0, opts.width, opts.height);
+      context.clearRect?.(0, 0, opts.width, opts.height);
       drawMapDataPoints(series, opts, config, context);
       setTimeout(() => {
         _this.uevent.trigger('renderComplete');
@@ -320,7 +320,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -339,7 +339,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -370,7 +370,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -401,7 +401,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -432,7 +432,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -463,7 +463,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -494,7 +494,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -525,7 +525,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -556,7 +556,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -587,7 +587,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -606,7 +606,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -625,7 +625,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -644,7 +644,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -663,7 +663,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -680,7 +680,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }
@@ -697,7 +697,7 @@ export const drawCharts: DrawChartsFunction = function(
         timing: opts.timing,
         duration: duration,
         onProcess: function onProcess(process) {
-          context.clearRect(0, 0, opts.width, opts.height);
+          context.clearRect?.(0, 0, opts.width, opts.height);
           if (opts.rotate) {
             contextRotate(context, opts);
           }