PieChartFCExample.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { useState, useRef, useLayoutEffect } from 'react';
  2. import Taro from '@tarojs/taro';
  3. import { View, Canvas } from '@tarojs/components';
  4. import uChartsClass from '../lib/u-charts-original.js';
  5. import type { ChartsConfig, TouchEvent } from '../lib/u-charts-original';
  6. import type { ExtendedCanvasContext } from '../types';
  7. /**
  8. * PieChartFCExample 组件
  9. *
  10. * FC 示例组件(基于 docs/小程序图表库示例/taro-2d饼状图class使用示例.md)
  11. * 使用原始 u-charts.js + Canvas 2D API
  12. *
  13. * 用于与 BaseChartOriginal2D 进行对比调试
  14. */
  15. interface PieChartFCExampleProps {
  16. /** Canvas 元素的 ID,必须唯一 */
  17. canvasId?: string;
  18. /** 图表宽度(像素),默认为屏幕宽度 */
  19. width?: number;
  20. /** 图表高度(像素),默认根据宽高比计算 */
  21. height?: number;
  22. }
  23. function PieChartFCComponent(props: PieChartFCExampleProps) {
  24. const { canvasId = 'HPTdeOYFgaHlwidEmFOZkYTRwhnebAad', width = 750, height = 500 } = props;
  25. const [cWidth, setCWidth] = useState(750);
  26. const [cHeight, setCHeight] = useState(500);
  27. const [pixelRatio, setPixelRatio] = useState(1);
  28. // 使用 ref 存储图表实例
  29. const uChartsInstanceRef = useRef<Record<string, any>>({});
  30. const drawCharts = (id: string, data: any) => {
  31. const query = Taro.createSelectorQuery();
  32. query.select('#' + id).fields({ node: true, size: true }).exec(res => {
  33. if (res[0]) {
  34. const canvas = res[0].node;
  35. const ctx = canvas.getContext('2d') as ExtendedCanvasContext;
  36. const chart = new uChartsClass({
  37. type: "pie",
  38. context: ctx,
  39. width: canvas.width,
  40. height: canvas.height,
  41. series: data.series,
  42. animation: true,
  43. background: "#FFFFFF",
  44. color: ["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
  45. padding: [5,5,5,5],
  46. enableScroll: false,
  47. extra: {
  48. pie: {
  49. activeOpacity: 0.5,
  50. activeRadius: 10,
  51. offsetAngle: 0,
  52. labelWidth: 15,
  53. border: false,
  54. borderWidth: 3,
  55. borderColor: "#FFFFFF"
  56. }
  57. }
  58. } as ChartsConfig);
  59. uChartsInstanceRef.current[id] = chart;
  60. const series = data.series;
  61. console.log('[PieChartFCExample] 图表初始化完成:', id, {
  62. cWidth, cHeight,
  63. canvasWidth: canvas.width,
  64. canvasHeight: canvas.height,
  65. seriesLength: series.length,
  66. series,
  67. });
  68. } else {
  69. console.error("[PieChartFCExample]: 未获取到 context");
  70. }
  71. });
  72. }
  73. const getServerData = () => {
  74. //模拟从服务器获取数据时的延时
  75. setTimeout(() => {
  76. //模拟服务器返回数据,如果数据格式和标准格式不同,需自行按下面的格式拼接
  77. let res = {
  78. series: [
  79. {
  80. data: [{"name":"一班","value":50},{"name":"二班","value":30},{"name":"三班","value":20},{"name":"四班","value":18},{"name":"五班","value":8}]
  81. }
  82. ]
  83. };
  84. drawCharts(canvasId, res);
  85. }, 500);
  86. }
  87. const tap = (e: any) => {
  88. uChartsInstanceRef.current[e.currentTarget.id]?.touchLegend(e);
  89. uChartsInstanceRef.current[e.currentTarget.id]?.showToolTip(e);
  90. }
  91. useLayoutEffect(() => {
  92. console.debug('[PieChartFCExample] useLayoutEffect')
  93. const sysInfo = Taro.getSystemInfoSync();
  94. const pr = sysInfo.pixelRatio;
  95. //这里的第一个 750 对应 css .charts 的 width
  96. const cw = width / 750 * sysInfo.windowWidth;
  97. //这里的 500 对应 css .charts 的 height
  98. const ch = height / 750 * sysInfo.windowWidth;
  99. setCWidth(cw);
  100. setCHeight(ch);
  101. setPixelRatio(pr);
  102. // 直接在这里获取数据
  103. getServerData();
  104. }, [width, height, canvasId]);
  105. const canvasProps = { style: { width: cWidth, height: cHeight } };
  106. return (
  107. <View>
  108. <Canvas
  109. {...canvasProps}
  110. canvas-id={canvasId}
  111. id={canvasId}
  112. type="2d"
  113. className="charts"
  114. onTouchEnd={tap}
  115. />
  116. </View>
  117. );
  118. }
  119. // 默认导出
  120. export default PieChartFCComponent;
  121. // 命名导出,保持向后兼容
  122. export const PieChartFCExample = PieChartFCComponent;