@formepdf/preact 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/dist/charts.d.ts +72 -0
- package/dist/charts.js +338 -0
- package/dist/components.d.ts +533 -0
- package/dist/components.js +608 -0
- package/dist/expr.d.ts +20 -0
- package/dist/expr.js +39 -0
- package/dist/font.d.ts +2 -0
- package/dist/font.js +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +15 -0
- package/dist/render.d.ts +12 -0
- package/dist/render.js +15 -0
- package/dist/serialize.d.ts +14 -0
- package/dist/serialize.js +1475 -0
- package/dist/stylesheet.d.ts +4 -0
- package/dist/stylesheet.js +5 -0
- package/dist/template-proxy.d.ts +38 -0
- package/dist/template-proxy.js +100 -0
- package/dist/types.d.ts +286 -0
- package/dist/types.js +1 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @formepdf/preact
|
|
2
|
+
|
|
3
|
+
Preact-native adapter for the [Forme](https://formepdf.com) PDF engine.
|
|
4
|
+
|
|
5
|
+
Same component set, same props, same serialized output as [`@formepdf/react`](https://www.npmjs.com/package/@formepdf/react) — authored with Preact's JSX runtime, without the React runtime in your bundle.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @formepdf/preact @formepdf/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires `preact ^10.19.0` as a peer. `@formepdf/core` is an optional peer — install it if you want to render locally; skip it if you're serializing to JSON and POSTing to the hosted API.
|
|
14
|
+
|
|
15
|
+
## Quickstart
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
/** @jsxImportSource preact */
|
|
19
|
+
import { Document, Page, View, Text, renderDocument } from '@formepdf/preact';
|
|
20
|
+
|
|
21
|
+
const pdf = await renderDocument(
|
|
22
|
+
<Document>
|
|
23
|
+
<Page size="Letter" margin={36}>
|
|
24
|
+
<Text style={{ fontSize: 24, fontWeight: 'bold' }}>Invoice #001</Text>
|
|
25
|
+
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
|
|
26
|
+
<Text>Widget Pro</Text>
|
|
27
|
+
<Text>$49.00</Text>
|
|
28
|
+
</View>
|
|
29
|
+
</Page>
|
|
30
|
+
</Document>
|
|
31
|
+
);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or configure `jsxImportSource: "preact"` in your `tsconfig.json` / bundler and drop the `/** @jsxImportSource */` comment.
|
|
35
|
+
|
|
36
|
+
## Component reference
|
|
37
|
+
|
|
38
|
+
Every component in `@formepdf/react` is exported here with the same name and the same props: `Document`, `Page`, `View`, `Text`, `H1`-`H6`, `Strong`, `Em`, `Code`, `Link`, `Image`, `Svg`, `QrCode`, `Barcode`, `Canvas`, `Watermark`, `Table` / `Row` / `Cell`, `OrderedList` / `UnorderedList` / `ListItem`, `BarChart` / `LineChart` / `PieChart` / `AreaChart` / `DotPlot`, `TextField` / `Checkbox` / `Dropdown` / `RadioButton`, `Fixed`, `PageBreak`. See the [components reference](https://docs.formepdf.com/components) — all examples work identically.
|
|
39
|
+
|
|
40
|
+
## Why a native Preact package vs `preact/compat`?
|
|
41
|
+
|
|
42
|
+
You can already use `@formepdf/react` with Preact via `preact/compat` aliasing. This package exists so you don't have to: no compat shim in your bundle, no unmet-peer warnings from npm about React not being installed, and the JSX runtime is Preact-native.
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
MIT
|
package/dist/charts.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type VNode } from 'preact';
|
|
2
|
+
import type { Style, ChartDataPoint, PieDataPoint } from './types.js';
|
|
3
|
+
interface LegacyBarChartProps {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
data: ChartDataPoint[];
|
|
7
|
+
color?: string;
|
|
8
|
+
barGap?: number;
|
|
9
|
+
showGrid?: boolean;
|
|
10
|
+
showValues?: boolean;
|
|
11
|
+
yAxisTicks?: number;
|
|
12
|
+
style?: Style;
|
|
13
|
+
}
|
|
14
|
+
interface LegacyLineChartProps {
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
data: ChartDataPoint[];
|
|
18
|
+
color?: string;
|
|
19
|
+
strokeWidth?: number;
|
|
20
|
+
showDots?: boolean;
|
|
21
|
+
showGrid?: boolean;
|
|
22
|
+
showArea?: boolean;
|
|
23
|
+
style?: Style;
|
|
24
|
+
}
|
|
25
|
+
interface LegacyPieChartProps {
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
data: PieDataPoint[];
|
|
29
|
+
showLabels?: boolean;
|
|
30
|
+
innerRadius?: number;
|
|
31
|
+
style?: Style;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A bar chart rendered as SVG + positioned Text labels.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```tsx
|
|
38
|
+
* <BarChart width={400} height={200}
|
|
39
|
+
* data={[{ label: 'Jan', value: 120 }, { label: 'Feb', value: 90 }]}
|
|
40
|
+
* color="#1a365d" showGrid showValues />
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function LegacyBarChart(props: LegacyBarChartProps): VNode<any>;
|
|
44
|
+
/**
|
|
45
|
+
* A line chart rendered as SVG + positioned Text labels.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```tsx
|
|
49
|
+
* <LineChart width={400} height={200}
|
|
50
|
+
* data={[{ label: 'Jan', value: 120 }, { label: 'Feb', value: 90 }]}
|
|
51
|
+
* color="#2b6cb0" showDots showGrid showArea />
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function LegacyLineChart(props: LegacyLineChartProps): VNode<any>;
|
|
55
|
+
/**
|
|
56
|
+
* A pie chart (or donut chart) rendered as SVG + positioned Text labels.
|
|
57
|
+
*
|
|
58
|
+
* Requires SVG arc path commands (A/a) — supported since engine v0.6.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```tsx
|
|
62
|
+
* <PieChart width={200} height={200}
|
|
63
|
+
* data={[
|
|
64
|
+
* { label: 'A', value: 40, color: '#1a365d' },
|
|
65
|
+
* { label: 'B', value: 30, color: '#2b6cb0' },
|
|
66
|
+
* { label: 'C', value: 30, color: '#63b3ed' },
|
|
67
|
+
* ]}
|
|
68
|
+
* showLabels />
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function LegacyPieChart(props: LegacyPieChartProps): VNode<any>;
|
|
72
|
+
export {};
|
package/dist/charts.js
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { h } from 'preact';
|
|
2
|
+
import { View, Text, Svg } from './components.js';
|
|
3
|
+
// ─── Helpers ────────────────────────────────────────────────────────
|
|
4
|
+
/** Round up to a "nice" axis maximum (1, 2, 5, 10, 20, 50, ...). */
|
|
5
|
+
function niceNumber(value) {
|
|
6
|
+
if (value <= 0)
|
|
7
|
+
return 1;
|
|
8
|
+
const exp = Math.floor(Math.log10(value));
|
|
9
|
+
const frac = value / Math.pow(10, exp);
|
|
10
|
+
let nice;
|
|
11
|
+
if (frac <= 1.0)
|
|
12
|
+
nice = 1;
|
|
13
|
+
else if (frac <= 2.0)
|
|
14
|
+
nice = 2;
|
|
15
|
+
else if (frac <= 5.0)
|
|
16
|
+
nice = 5;
|
|
17
|
+
else
|
|
18
|
+
nice = 10;
|
|
19
|
+
return nice * Math.pow(10, exp);
|
|
20
|
+
}
|
|
21
|
+
/** Format number compactly for axis labels. */
|
|
22
|
+
function formatNumber(value) {
|
|
23
|
+
if (value >= 1000000)
|
|
24
|
+
return (value / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
|
|
25
|
+
if (value >= 1000)
|
|
26
|
+
return (value / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
|
|
27
|
+
if (Number.isInteger(value))
|
|
28
|
+
return String(value);
|
|
29
|
+
return value.toFixed(1);
|
|
30
|
+
}
|
|
31
|
+
/** Lighten a hex color toward white. amount=0 is original, amount=1 is white. */
|
|
32
|
+
function lightenColor(hex, amount) {
|
|
33
|
+
const r = parseInt(hex.slice(1, 3), 16);
|
|
34
|
+
const g = parseInt(hex.slice(3, 5), 16);
|
|
35
|
+
const b = parseInt(hex.slice(5, 7), 16);
|
|
36
|
+
const lr = Math.round(r + (255 - r) * amount);
|
|
37
|
+
const lg = Math.round(g + (255 - g) * amount);
|
|
38
|
+
const lb = Math.round(b + (255 - b) * amount);
|
|
39
|
+
return '#' + [lr, lg, lb].map(c => c.toString(16).padStart(2, '0')).join('');
|
|
40
|
+
}
|
|
41
|
+
/** Escape XML special characters in text. */
|
|
42
|
+
function escapeXml(s) {
|
|
43
|
+
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
44
|
+
}
|
|
45
|
+
// Chart layout constants
|
|
46
|
+
const AXIS_LABEL_FONT = 8;
|
|
47
|
+
const LABEL_MARGIN = 4;
|
|
48
|
+
const Y_AXIS_WIDTH = 28;
|
|
49
|
+
const X_AXIS_HEIGHT = 20;
|
|
50
|
+
// ─── BarChart ───────────────────────────────────────────────────────
|
|
51
|
+
/**
|
|
52
|
+
* A bar chart rendered as SVG + positioned Text labels.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```tsx
|
|
56
|
+
* <BarChart width={400} height={200}
|
|
57
|
+
* data={[{ label: 'Jan', value: 120 }, { label: 'Feb', value: 90 }]}
|
|
58
|
+
* color="#1a365d" showGrid showValues />
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function LegacyBarChart(props) {
|
|
62
|
+
const { width, height, data, color = '#1a365d', barGap = 4, showGrid = false, showValues = false, yAxisTicks = 5, } = props;
|
|
63
|
+
if (data.length === 0) {
|
|
64
|
+
return h(View, { style: { width, height, ...props.style } });
|
|
65
|
+
}
|
|
66
|
+
const plotLeft = Y_AXIS_WIDTH;
|
|
67
|
+
const plotTop = 10;
|
|
68
|
+
const plotWidth = width - plotLeft - 10;
|
|
69
|
+
const plotHeight = height - plotTop - X_AXIS_HEIGHT;
|
|
70
|
+
const maxValue = niceNumber(Math.max(...data.map(d => d.value)));
|
|
71
|
+
// Build SVG content
|
|
72
|
+
let svg = '';
|
|
73
|
+
// Grid lines
|
|
74
|
+
if (showGrid) {
|
|
75
|
+
for (let t = 0; t <= yAxisTicks; t++) {
|
|
76
|
+
const y = plotTop + plotHeight - (t / yAxisTicks) * plotHeight;
|
|
77
|
+
svg += `<line x1="${plotLeft}" y1="${y}" x2="${plotLeft + plotWidth}" y2="${y}" stroke="#e2e8f0" stroke-width="0.5"/>`;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// X axis
|
|
81
|
+
svg += `<line x1="${plotLeft}" y1="${plotTop + plotHeight}" x2="${plotLeft + plotWidth}" y2="${plotTop + plotHeight}" stroke="#a0aec0" stroke-width="1"/>`;
|
|
82
|
+
// Y axis
|
|
83
|
+
svg += `<line x1="${plotLeft}" y1="${plotTop}" x2="${plotLeft}" y2="${plotTop + plotHeight}" stroke="#a0aec0" stroke-width="1"/>`;
|
|
84
|
+
// Bars
|
|
85
|
+
const barWidth = (plotWidth - barGap * (data.length + 1)) / data.length;
|
|
86
|
+
for (let i = 0; i < data.length; i++) {
|
|
87
|
+
const barH = (data[i].value / maxValue) * plotHeight;
|
|
88
|
+
const x = plotLeft + barGap + i * (barWidth + barGap);
|
|
89
|
+
const y = plotTop + plotHeight - barH;
|
|
90
|
+
svg += `<rect x="${x}" y="${y}" width="${barWidth}" height="${barH}" fill="${escapeXml(color)}"/>`;
|
|
91
|
+
}
|
|
92
|
+
const viewBox = `0 0 ${width} ${height}`;
|
|
93
|
+
// Y-axis tick labels + X-axis labels as positioned Text
|
|
94
|
+
const labels = [];
|
|
95
|
+
for (let t = 0; t <= yAxisTicks; t++) {
|
|
96
|
+
const val = (t / yAxisTicks) * maxValue;
|
|
97
|
+
const y = plotTop + plotHeight - (t / yAxisTicks) * plotHeight - AXIS_LABEL_FONT / 2;
|
|
98
|
+
labels.push(h(Text, {
|
|
99
|
+
key: `y-${t}`,
|
|
100
|
+
style: {
|
|
101
|
+
position: 'absolute',
|
|
102
|
+
top: y,
|
|
103
|
+
left: 0,
|
|
104
|
+
width: Y_AXIS_WIDTH - LABEL_MARGIN,
|
|
105
|
+
fontSize: AXIS_LABEL_FONT,
|
|
106
|
+
textAlign: 'right',
|
|
107
|
+
color: '#4a5568',
|
|
108
|
+
},
|
|
109
|
+
}, formatNumber(val)));
|
|
110
|
+
}
|
|
111
|
+
for (let i = 0; i < data.length; i++) {
|
|
112
|
+
const x = plotLeft + barGap + i * (barWidth + barGap);
|
|
113
|
+
labels.push(h(Text, {
|
|
114
|
+
key: `x-${i}`,
|
|
115
|
+
style: {
|
|
116
|
+
position: 'absolute',
|
|
117
|
+
top: plotTop + plotHeight + LABEL_MARGIN,
|
|
118
|
+
left: x - barGap,
|
|
119
|
+
width: barWidth + barGap * 2,
|
|
120
|
+
fontSize: AXIS_LABEL_FONT,
|
|
121
|
+
textAlign: 'center',
|
|
122
|
+
color: '#4a5568',
|
|
123
|
+
},
|
|
124
|
+
}, data[i].label));
|
|
125
|
+
if (showValues) {
|
|
126
|
+
const barH = (data[i].value / maxValue) * plotHeight;
|
|
127
|
+
const valY = plotTop + plotHeight - barH - AXIS_LABEL_FONT - 2;
|
|
128
|
+
labels.push(h(Text, {
|
|
129
|
+
key: `v-${i}`,
|
|
130
|
+
style: {
|
|
131
|
+
position: 'absolute',
|
|
132
|
+
top: valY,
|
|
133
|
+
left: x,
|
|
134
|
+
width: barWidth,
|
|
135
|
+
fontSize: AXIS_LABEL_FONT,
|
|
136
|
+
textAlign: 'center',
|
|
137
|
+
color: '#2d3748',
|
|
138
|
+
},
|
|
139
|
+
}, formatNumber(data[i].value)));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return h(View, {
|
|
143
|
+
style: { width, height, position: 'relative', ...props.style },
|
|
144
|
+
}, h(Svg, { width, height, viewBox, content: svg }), ...labels);
|
|
145
|
+
}
|
|
146
|
+
// ─── LineChart ───────────────────────────────────────────────────────
|
|
147
|
+
/**
|
|
148
|
+
* A line chart rendered as SVG + positioned Text labels.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```tsx
|
|
152
|
+
* <LineChart width={400} height={200}
|
|
153
|
+
* data={[{ label: 'Jan', value: 120 }, { label: 'Feb', value: 90 }]}
|
|
154
|
+
* color="#2b6cb0" showDots showGrid showArea />
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
export function LegacyLineChart(props) {
|
|
158
|
+
const { width, height, data, color = '#2b6cb0', strokeWidth = 2, showDots = false, showGrid = false, showArea = false, } = props;
|
|
159
|
+
if (data.length === 0) {
|
|
160
|
+
return h(View, { style: { width, height, ...props.style } });
|
|
161
|
+
}
|
|
162
|
+
const plotLeft = Y_AXIS_WIDTH;
|
|
163
|
+
const plotTop = 10;
|
|
164
|
+
const plotWidth = width - plotLeft - 10;
|
|
165
|
+
const plotHeight = height - plotTop - X_AXIS_HEIGHT;
|
|
166
|
+
const maxValue = niceNumber(Math.max(...data.map(d => d.value)));
|
|
167
|
+
const yAxisTicks = 5;
|
|
168
|
+
const pointX = (i) => plotLeft + (i / (data.length - 1 || 1)) * plotWidth;
|
|
169
|
+
const pointY = (v) => plotTop + plotHeight - (v / maxValue) * plotHeight;
|
|
170
|
+
let svg = '';
|
|
171
|
+
// Grid lines
|
|
172
|
+
if (showGrid) {
|
|
173
|
+
for (let t = 0; t <= yAxisTicks; t++) {
|
|
174
|
+
const y = plotTop + plotHeight - (t / yAxisTicks) * plotHeight;
|
|
175
|
+
svg += `<line x1="${plotLeft}" y1="${y}" x2="${plotLeft + plotWidth}" y2="${y}" stroke="#e2e8f0" stroke-width="0.5"/>`;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Axes
|
|
179
|
+
svg += `<line x1="${plotLeft}" y1="${plotTop + plotHeight}" x2="${plotLeft + plotWidth}" y2="${plotTop + plotHeight}" stroke="#a0aec0" stroke-width="1"/>`;
|
|
180
|
+
svg += `<line x1="${plotLeft}" y1="${plotTop}" x2="${plotLeft}" y2="${plotTop + plotHeight}" stroke="#a0aec0" stroke-width="1"/>`;
|
|
181
|
+
// Area fill
|
|
182
|
+
if (showArea && data.length > 1) {
|
|
183
|
+
const areaColor = lightenColor(color, 0.7);
|
|
184
|
+
let pathD = `M ${pointX(0)} ${pointY(data[0].value)}`;
|
|
185
|
+
for (let i = 1; i < data.length; i++) {
|
|
186
|
+
pathD += ` L ${pointX(i)} ${pointY(data[i].value)}`;
|
|
187
|
+
}
|
|
188
|
+
pathD += ` L ${pointX(data.length - 1)} ${plotTop + plotHeight}`;
|
|
189
|
+
pathD += ` L ${pointX(0)} ${plotTop + plotHeight} Z`;
|
|
190
|
+
svg += `<path d="${pathD}" fill="${escapeXml(areaColor)}" stroke="none"/>`;
|
|
191
|
+
}
|
|
192
|
+
// Line
|
|
193
|
+
if (data.length > 1) {
|
|
194
|
+
let pathD = `M ${pointX(0)} ${pointY(data[0].value)}`;
|
|
195
|
+
for (let i = 1; i < data.length; i++) {
|
|
196
|
+
pathD += ` L ${pointX(i)} ${pointY(data[i].value)}`;
|
|
197
|
+
}
|
|
198
|
+
svg += `<path d="${pathD}" fill="none" stroke="${escapeXml(color)}" stroke-width="${strokeWidth}"/>`;
|
|
199
|
+
}
|
|
200
|
+
// Dots
|
|
201
|
+
if (showDots) {
|
|
202
|
+
for (let i = 0; i < data.length; i++) {
|
|
203
|
+
const x = pointX(i);
|
|
204
|
+
const y = pointY(data[i].value);
|
|
205
|
+
svg += `<circle cx="${x}" cy="${y}" r="3" fill="${escapeXml(color)}"/>`;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const viewBox = `0 0 ${width} ${height}`;
|
|
209
|
+
const labels = [];
|
|
210
|
+
// Y-axis labels
|
|
211
|
+
for (let t = 0; t <= yAxisTicks; t++) {
|
|
212
|
+
const val = (t / yAxisTicks) * maxValue;
|
|
213
|
+
const y = plotTop + plotHeight - (t / yAxisTicks) * plotHeight - AXIS_LABEL_FONT / 2;
|
|
214
|
+
labels.push(h(Text, {
|
|
215
|
+
key: `y-${t}`,
|
|
216
|
+
style: {
|
|
217
|
+
position: 'absolute',
|
|
218
|
+
top: y,
|
|
219
|
+
left: 0,
|
|
220
|
+
width: Y_AXIS_WIDTH - LABEL_MARGIN,
|
|
221
|
+
fontSize: AXIS_LABEL_FONT,
|
|
222
|
+
textAlign: 'right',
|
|
223
|
+
color: '#4a5568',
|
|
224
|
+
},
|
|
225
|
+
}, formatNumber(val)));
|
|
226
|
+
}
|
|
227
|
+
// X-axis labels
|
|
228
|
+
for (let i = 0; i < data.length; i++) {
|
|
229
|
+
const x = pointX(i);
|
|
230
|
+
const labelWidth = plotWidth / data.length;
|
|
231
|
+
labels.push(h(Text, {
|
|
232
|
+
key: `x-${i}`,
|
|
233
|
+
style: {
|
|
234
|
+
position: 'absolute',
|
|
235
|
+
top: plotTop + plotHeight + LABEL_MARGIN,
|
|
236
|
+
left: x - labelWidth / 2,
|
|
237
|
+
width: labelWidth,
|
|
238
|
+
fontSize: AXIS_LABEL_FONT,
|
|
239
|
+
textAlign: 'center',
|
|
240
|
+
color: '#4a5568',
|
|
241
|
+
},
|
|
242
|
+
}, data[i].label));
|
|
243
|
+
}
|
|
244
|
+
return h(View, {
|
|
245
|
+
style: { width, height, position: 'relative', ...props.style },
|
|
246
|
+
}, h(Svg, { width, height, viewBox, content: svg }), ...labels);
|
|
247
|
+
}
|
|
248
|
+
// ─── PieChart ───────────────────────────────────────────────────────
|
|
249
|
+
/**
|
|
250
|
+
* A pie chart (or donut chart) rendered as SVG + positioned Text labels.
|
|
251
|
+
*
|
|
252
|
+
* Requires SVG arc path commands (A/a) — supported since engine v0.6.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```tsx
|
|
256
|
+
* <PieChart width={200} height={200}
|
|
257
|
+
* data={[
|
|
258
|
+
* { label: 'A', value: 40, color: '#1a365d' },
|
|
259
|
+
* { label: 'B', value: 30, color: '#2b6cb0' },
|
|
260
|
+
* { label: 'C', value: 30, color: '#63b3ed' },
|
|
261
|
+
* ]}
|
|
262
|
+
* showLabels />
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
export function LegacyPieChart(props) {
|
|
266
|
+
const { width, height, data, showLabels = false, innerRadius = 0, } = props;
|
|
267
|
+
const total = data.reduce((sum, d) => sum + d.value, 0);
|
|
268
|
+
if (data.length === 0 || total <= 0) {
|
|
269
|
+
return h(View, { style: { width, height, ...props.style } });
|
|
270
|
+
}
|
|
271
|
+
const cx = width / 2;
|
|
272
|
+
const cy = height / 2;
|
|
273
|
+
const labelMargin = showLabels ? 30 : 0;
|
|
274
|
+
const outerR = Math.min(width - labelMargin * 2, height - labelMargin * 2) / 2;
|
|
275
|
+
let svg = '';
|
|
276
|
+
let startAngle = -Math.PI / 2; // Start from top
|
|
277
|
+
const labels = [];
|
|
278
|
+
for (let i = 0; i < data.length; i++) {
|
|
279
|
+
const slice = data[i];
|
|
280
|
+
const sliceAngle = (slice.value / total) * Math.PI * 2;
|
|
281
|
+
const endAngle = startAngle + sliceAngle;
|
|
282
|
+
// For full circle (single slice), use two arcs
|
|
283
|
+
if (data.length === 1) {
|
|
284
|
+
// Full circle — draw as ellipse-like two half arcs
|
|
285
|
+
if (innerRadius > 0) {
|
|
286
|
+
// Donut: outer arc + inner arc
|
|
287
|
+
svg += `<path d="M ${cx} ${cy - outerR} A ${outerR} ${outerR} 0 1 1 ${cx} ${cy + outerR} A ${outerR} ${outerR} 0 1 1 ${cx} ${cy - outerR} Z M ${cx} ${cy - innerRadius} A ${innerRadius} ${innerRadius} 0 1 0 ${cx} ${cy + innerRadius} A ${innerRadius} ${innerRadius} 0 1 0 ${cx} ${cy - innerRadius} Z" fill="${escapeXml(slice.color)}" fill-rule="evenodd"/>`;
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
svg += `<circle cx="${cx}" cy="${cy}" r="${outerR}" fill="${escapeXml(slice.color)}"/>`;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
const largeArc = sliceAngle > Math.PI ? 1 : 0;
|
|
295
|
+
const x1 = cx + outerR * Math.cos(startAngle);
|
|
296
|
+
const y1 = cy + outerR * Math.sin(startAngle);
|
|
297
|
+
const x2 = cx + outerR * Math.cos(endAngle);
|
|
298
|
+
const y2 = cy + outerR * Math.sin(endAngle);
|
|
299
|
+
let pathD;
|
|
300
|
+
if (innerRadius > 0) {
|
|
301
|
+
const ix1 = cx + innerRadius * Math.cos(startAngle);
|
|
302
|
+
const iy1 = cy + innerRadius * Math.sin(startAngle);
|
|
303
|
+
const ix2 = cx + innerRadius * Math.cos(endAngle);
|
|
304
|
+
const iy2 = cy + innerRadius * Math.sin(endAngle);
|
|
305
|
+
pathD = `M ${ix1} ${iy1} L ${x1} ${y1} A ${outerR} ${outerR} 0 ${largeArc} 1 ${x2} ${y2} L ${ix2} ${iy2} A ${innerRadius} ${innerRadius} 0 ${largeArc} 0 ${ix1} ${iy1} Z`;
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
pathD = `M ${cx} ${cy} L ${x1} ${y1} A ${outerR} ${outerR} 0 ${largeArc} 1 ${x2} ${y2} Z`;
|
|
309
|
+
}
|
|
310
|
+
svg += `<path d="${pathD}" fill="${escapeXml(slice.color)}"/>`;
|
|
311
|
+
}
|
|
312
|
+
// Labels
|
|
313
|
+
if (showLabels) {
|
|
314
|
+
const midAngle = startAngle + sliceAngle / 2;
|
|
315
|
+
const labelR = outerR + 12;
|
|
316
|
+
const lx = cx + labelR * Math.cos(midAngle);
|
|
317
|
+
const ly = cy + labelR * Math.sin(midAngle);
|
|
318
|
+
const isRight = Math.cos(midAngle) >= 0;
|
|
319
|
+
labels.push(h(Text, {
|
|
320
|
+
key: `label-${i}`,
|
|
321
|
+
style: {
|
|
322
|
+
position: 'absolute',
|
|
323
|
+
top: ly - AXIS_LABEL_FONT / 2,
|
|
324
|
+
left: isRight ? lx : lx - 50,
|
|
325
|
+
width: 50,
|
|
326
|
+
fontSize: AXIS_LABEL_FONT,
|
|
327
|
+
textAlign: isRight ? 'left' : 'right',
|
|
328
|
+
color: '#2d3748',
|
|
329
|
+
},
|
|
330
|
+
}, slice.label));
|
|
331
|
+
}
|
|
332
|
+
startAngle = endAngle;
|
|
333
|
+
}
|
|
334
|
+
const viewBox = `0 0 ${width} ${height}`;
|
|
335
|
+
return h(View, {
|
|
336
|
+
style: { width, height, position: 'relative', ...props.style },
|
|
337
|
+
}, h(Svg, { width, height, viewBox, content: svg }), ...labels);
|
|
338
|
+
}
|