Skip to content

Commit

Permalink
Merge pull request #759 from thundersdata-frontend/feat/3dpie
Browse files Browse the repository at this point in the history
feat: 3d饼图支持中心标题
  • Loading branch information
chj-damon authored Nov 9, 2023
2 parents 4aa917c + 66f777c commit 7731f56
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-days-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/lego': patch
---

3d饼图支持中心标题
28 changes: 28 additions & 0 deletions packages/lego/src/three-dimensional-pie/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.td-lego-3d-pie {
position: relative;

.td-lego-3d-pie-text-wrap {
position: absolute;
color: #fff;
left: 39%;
top: 40%;
z-index: 999;

.td-lego-3d-pie-title {
text-align: center;
font-family: Roboto;
font-size: 38px;
font-style: normal;
font-weight: 700;
line-height: 38px;
}

.td-lego-3d-pie-text {
font-family: Alibaba PuHuiTi;
text-align: center;
font-size: 14px;
font-style: normal;
font-weight: 400;
}
}
}
26 changes: 14 additions & 12 deletions packages/lego/src/three-dimensional-pie/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ group:

## API

| 属性 | 必填 | 说明 | 类型 | 默认值 |
| ----------- | ------- | --------------- | ---------------------------------------- | ------ |
| seriesData | `true` | 图表数据 | `{ name: string; value: string }[]` | |
| style | `false` | 自定义样式 | `CSSProperties` | |
| barConfig | `false` | 3D 饼图纵向配置 | `ECOption` | |
| pieConfig | `false` | 3D 饼图配置 | `ECOption` | |
| autoLoop | `false` | 是否自动切换 | `boolean` | |
| isFlat | `false` | 是否扁平 | `boolean` | `true` |
| loopSpeed | `false` | 切换速度 | `number` | `2000` |
| coefficient | `false` | 高度系数 | `number` | `1` |
| pieColors | `false` | 自定义颜色 | `string[]` | |
| onEvents | `false` | 自定义事件 | `Record<string, (params?: any) => void>` | |
| 属性 | 必填 | 说明 | 类型 | 默认值 |
| ----------- | ------- | ---------------- | ---------------------------------------- | ------- |
| seriesData | `true` | 图表数据 | `{ name: string; value: string }[]` | |
| style | `false` | 自定义样式 | `CSSProperties` | |
| barConfig | `false` | 3D 饼图纵向配置 | `ECOption` | |
| pieConfig | `false` | 3D 饼图配置 | `ECOption` | |
| autoLoop | `false` | 是否自动切换 | `boolean` | |
| isFlat | `false` | 是否扁平 | `boolean` | `true` |
| loopSpeed | `false` | 切换速度 | `number` | `2000` |
| coefficient | `false` | 高度系数 | `number` | `1` |
| pieColors | `false` | 自定义颜色 | `string[]` | |
| showTitle | `false` | 是否显示中心标题 | `boolean` | `false` |
| titleStyle | `false` | 中心标题的样式 | `CSSProperties` | |
| onEvents | `false` | 自定义事件 | `Record<string, (params?: any) => void>` | |

## 基本效果

Expand Down
18 changes: 16 additions & 2 deletions packages/lego/src/three-dimensional-pie/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useEchartsRef from '../hooks/useEchartsRef';
import { useRAF } from '../hooks/useRAF';
import useStyle from '../hooks/useStyle';
import useTheme from '../hooks/useTheme';
import './index.less';

type ECOption = echarts.ComposeOption<PieSeriesOption | TooltipComponentOption | GraphicComponentOption>;

Expand All @@ -32,6 +33,8 @@ export interface ThreeDimensionalPieProps {
onEvents?: Record<string, (params?: any) => void>;
coefficient?: number;
pieColors?: string[];
showTitle?: boolean;
titleStyle?: CSSProperties;
}

/** 3D立体饼图-对应Figma饼图2 */
Expand All @@ -49,6 +52,8 @@ export default forwardRef<ReactEcharts, ThreeDimensionalPieProps>(
pieColors = [],
onEvents,
coefficient = 1,
showTitle = false,
titleStyle,
},
ref
) => {
Expand Down Expand Up @@ -191,7 +196,7 @@ export default forwardRef<ReactEcharts, ThreeDimensionalPieProps>(
}
});
// 如果触发 mouseover 的扇形当前已高亮,则不做操作
if (hoveredIndex === seriesIndex) {
if (autoLoop || hoveredIndex === seriesIndex) {
return;
} else {
if (hoveredIndex !== '') {
Expand All @@ -207,6 +212,7 @@ export default forwardRef<ReactEcharts, ThreeDimensionalPieProps>(
});
}
hoveredIndex = hoveredIndex !== '' ? '' : seriesIndex;
setHoveredIndex(hoveredIndex);
myChart.setOption(option);
}
});
Expand All @@ -224,7 +230,15 @@ export default forwardRef<ReactEcharts, ThreeDimensionalPieProps>(
}, [echartsRef, seriesData, option, isFlat, generateData, getInstance]);

return (
<div style={modifiedStyle}>
<div className="td-lego-3d-pie" style={modifiedStyle}>
{showTitle && hoveredIndex ? (
<div className="td-lego-3d-pie-text-wrap" style={titleStyle}>
<div className="td-lego-3d-pie-title">
{((seriesData?.[hoveredIndex]?.value / total) * 100).toFixed(2) + '%'}
</div>
<div className="td-lego-3d-pie-text">{seriesData?.[hoveredIndex]?.name}</div>
</div>
) : null}
<ReactEcharts
ref={echartsRef}
style={{ width: modifiedStyle.width, height: modifiedStyle.height }}
Expand Down

0 comments on commit 7731f56

Please sign in to comment.