@formepdf/react 0.4.4 → 0.6.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/dist/charts.d.ts +41 -0
- package/dist/charts.js +338 -0
- package/dist/components.d.ts +56 -1
- package/dist/components.js +61 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/serialize.d.ts +1 -1
- package/dist/serialize.js +340 -9
- package/dist/types.d.ts +289 -3
- package/package.json +1 -1
package/dist/charts.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { BarChartProps, LineChartProps, PieChartProps } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* A bar chart rendered as SVG + positioned Text labels.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* <BarChart width={400} height={200}
|
|
9
|
+
* data={[{ label: 'Jan', value: 120 }, { label: 'Feb', value: 90 }]}
|
|
10
|
+
* color="#1a365d" showGrid showValues />
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare function BarChart(props: BarChartProps): React.ReactElement;
|
|
14
|
+
/**
|
|
15
|
+
* A line chart rendered as SVG + positioned Text labels.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* <LineChart width={400} height={200}
|
|
20
|
+
* data={[{ label: 'Jan', value: 120 }, { label: 'Feb', value: 90 }]}
|
|
21
|
+
* color="#2b6cb0" showDots showGrid showArea />
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function LineChart(props: LineChartProps): React.ReactElement;
|
|
25
|
+
/**
|
|
26
|
+
* A pie chart (or donut chart) rendered as SVG + positioned Text labels.
|
|
27
|
+
*
|
|
28
|
+
* Requires SVG arc path commands (A/a) — supported since engine v0.6.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* <PieChart width={200} height={200}
|
|
33
|
+
* data={[
|
|
34
|
+
* { label: 'A', value: 40, color: '#1a365d' },
|
|
35
|
+
* { label: 'B', value: 30, color: '#2b6cb0' },
|
|
36
|
+
* { label: 'C', value: 30, color: '#63b3ed' },
|
|
37
|
+
* ]}
|
|
38
|
+
* showLabels />
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function PieChart(props: PieChartProps): React.ReactElement;
|
package/dist/charts.js
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import React from 'react';
|
|
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 BarChart(props) {
|
|
62
|
+
const { width, height, data, color = '#1a365d', barGap = 4, showGrid = false, showValues = false, yAxisTicks = 5, } = props;
|
|
63
|
+
if (data.length === 0) {
|
|
64
|
+
return React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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 React.createElement(View, {
|
|
143
|
+
style: { width, height, position: 'relative', ...props.style },
|
|
144
|
+
}, React.createElement(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 LineChart(props) {
|
|
158
|
+
const { width, height, data, color = '#2b6cb0', strokeWidth = 2, showDots = false, showGrid = false, showArea = false, } = props;
|
|
159
|
+
if (data.length === 0) {
|
|
160
|
+
return React.createElement(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(React.createElement(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(React.createElement(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 React.createElement(View, {
|
|
245
|
+
style: { width, height, position: 'relative', ...props.style },
|
|
246
|
+
}, React.createElement(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 PieChart(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 React.createElement(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(React.createElement(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 React.createElement(View, {
|
|
336
|
+
style: { width, height, position: 'relative', ...props.style },
|
|
337
|
+
}, React.createElement(Svg, { width, height, viewBox, content: svg }), ...labels);
|
|
338
|
+
}
|
package/dist/components.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DocumentProps, PageProps, ViewProps, TextProps, ImageProps, TableProps, RowProps, CellProps, FixedProps, SvgProps } from './types.js';
|
|
1
|
+
import type { DocumentProps, PageProps, ViewProps, TextProps, ImageProps, TableProps, RowProps, CellProps, FixedProps, SvgProps, QrCodeProps, CanvasProps, WatermarkProps } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Root document container. Must be the top-level element in every Forme document.
|
|
4
4
|
*
|
|
@@ -187,6 +187,61 @@ export declare function Fixed(_props: FixedProps): null;
|
|
|
187
187
|
* ```
|
|
188
188
|
*/
|
|
189
189
|
export declare function Svg(_props: SvgProps): null;
|
|
190
|
+
/**
|
|
191
|
+
* A QR code rendered as crisp vector graphics in the PDF.
|
|
192
|
+
*
|
|
193
|
+
* @param props.data - The data to encode (URL, text, etc.)
|
|
194
|
+
* @param props.size - Display size in points. QR codes are square. Defaults to available width.
|
|
195
|
+
* @param props.color - QR code color. Default: black.
|
|
196
|
+
* @param props.style - Style properties for the QR code container.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```tsx
|
|
200
|
+
* <QrCode data="https://formepdf.com" size={100} />
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
export declare function QrCode(_props: QrCodeProps): null;
|
|
204
|
+
/**
|
|
205
|
+
* A canvas drawing primitive for arbitrary vector graphics.
|
|
206
|
+
*
|
|
207
|
+
* The `draw` callback receives a `CanvasContext` that records drawing operations.
|
|
208
|
+
* These are serialized to JSON and rendered as native PDF vector commands.
|
|
209
|
+
*
|
|
210
|
+
* @param props.width - Canvas width in points
|
|
211
|
+
* @param props.height - Canvas height in points
|
|
212
|
+
* @param props.draw - Drawing callback that receives a `CanvasContext`
|
|
213
|
+
* @param props.style - Additional style properties
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```tsx
|
|
217
|
+
* <Canvas width={200} height={100} draw={(ctx) => {
|
|
218
|
+
* ctx.setFillColor(0, 0, 255);
|
|
219
|
+
* ctx.rect(10, 10, 180, 80);
|
|
220
|
+
* ctx.fill();
|
|
221
|
+
* }} />
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
export declare function Canvas(_props: CanvasProps): null;
|
|
225
|
+
/**
|
|
226
|
+
* A watermark rendered as rotated text behind all page content.
|
|
227
|
+
*
|
|
228
|
+
* Appears on every page. Use `rgba()` colors for translucent watermarks.
|
|
229
|
+
*
|
|
230
|
+
* @param props.text - Watermark text (e.g. "DRAFT", "CONFIDENTIAL")
|
|
231
|
+
* @param props.fontSize - Font size in points. Default: 60.
|
|
232
|
+
* @param props.color - Text color. Use `rgba(0,0,0,0.1)` for subtle watermarks. Default: "rgba(0,0,0,0.1)".
|
|
233
|
+
* @param props.angle - Rotation angle in degrees. Default: -45.
|
|
234
|
+
* @param props.style - Additional style properties (fontFamily, etc.)
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```tsx
|
|
238
|
+
* <Page size="A4" margin={40}>
|
|
239
|
+
* <Watermark text="DRAFT" fontSize={60} color="rgba(0,0,0,0.1)" angle={-45} />
|
|
240
|
+
* <Text>Document content here</Text>
|
|
241
|
+
* </Page>
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
export declare function Watermark(_props: WatermarkProps): null;
|
|
190
245
|
/**
|
|
191
246
|
* An explicit page break. Content after this element starts on a new page.
|
|
192
247
|
*
|
package/dist/components.js
CHANGED
|
@@ -206,6 +206,67 @@ export function Fixed(_props) {
|
|
|
206
206
|
export function Svg(_props) {
|
|
207
207
|
return null;
|
|
208
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* A QR code rendered as crisp vector graphics in the PDF.
|
|
211
|
+
*
|
|
212
|
+
* @param props.data - The data to encode (URL, text, etc.)
|
|
213
|
+
* @param props.size - Display size in points. QR codes are square. Defaults to available width.
|
|
214
|
+
* @param props.color - QR code color. Default: black.
|
|
215
|
+
* @param props.style - Style properties for the QR code container.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```tsx
|
|
219
|
+
* <QrCode data="https://formepdf.com" size={100} />
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
export function QrCode(_props) {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* A canvas drawing primitive for arbitrary vector graphics.
|
|
227
|
+
*
|
|
228
|
+
* The `draw` callback receives a `CanvasContext` that records drawing operations.
|
|
229
|
+
* These are serialized to JSON and rendered as native PDF vector commands.
|
|
230
|
+
*
|
|
231
|
+
* @param props.width - Canvas width in points
|
|
232
|
+
* @param props.height - Canvas height in points
|
|
233
|
+
* @param props.draw - Drawing callback that receives a `CanvasContext`
|
|
234
|
+
* @param props.style - Additional style properties
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```tsx
|
|
238
|
+
* <Canvas width={200} height={100} draw={(ctx) => {
|
|
239
|
+
* ctx.setFillColor(0, 0, 255);
|
|
240
|
+
* ctx.rect(10, 10, 180, 80);
|
|
241
|
+
* ctx.fill();
|
|
242
|
+
* }} />
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export function Canvas(_props) {
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* A watermark rendered as rotated text behind all page content.
|
|
250
|
+
*
|
|
251
|
+
* Appears on every page. Use `rgba()` colors for translucent watermarks.
|
|
252
|
+
*
|
|
253
|
+
* @param props.text - Watermark text (e.g. "DRAFT", "CONFIDENTIAL")
|
|
254
|
+
* @param props.fontSize - Font size in points. Default: 60.
|
|
255
|
+
* @param props.color - Text color. Use `rgba(0,0,0,0.1)` for subtle watermarks. Default: "rgba(0,0,0,0.1)".
|
|
256
|
+
* @param props.angle - Rotation angle in degrees. Default: -45.
|
|
257
|
+
* @param props.style - Additional style properties (fontFamily, etc.)
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```tsx
|
|
261
|
+
* <Page size="A4" margin={40}>
|
|
262
|
+
* <Watermark text="DRAFT" fontSize={60} color="rgba(0,0,0,0.1)" angle={-45} />
|
|
263
|
+
* <Text>Document content here</Text>
|
|
264
|
+
* </Page>
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
export function Watermark(_props) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
209
270
|
/**
|
|
210
271
|
* An explicit page break. Content after this element starts on a new page.
|
|
211
272
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { Document, Page, View, Text, Image, Table, Row, Cell, Fixed, Svg, PageBreak } from './components.js';
|
|
1
|
+
export { Document, Page, View, Text, Image, Table, Row, Cell, Fixed, Svg, QrCode, Canvas, Watermark, PageBreak } from './components.js';
|
|
2
|
+
export { BarChart, LineChart, PieChart } from './charts.js';
|
|
2
3
|
export { serialize, serializeTemplate, mapStyle, mapDimension, parseColor, expandEdges, expandCorners } from './serialize.js';
|
|
3
4
|
export { StyleSheet } from './stylesheet.js';
|
|
4
5
|
export { Font } from './font.js';
|
|
@@ -6,4 +7,4 @@ export type { FontRegistration } from './font.js';
|
|
|
6
7
|
export { createDataProxy, isRefMarker, isEachMarker, isExprMarker } from './template-proxy.js';
|
|
7
8
|
export { expr } from './expr.js';
|
|
8
9
|
export { render, renderToObject } from './render.js';
|
|
9
|
-
export type { Style, Edges, Corners, EdgeColors, DocumentProps, PageProps, ViewProps, TextProps, ImageProps, ColumnDef, TableProps, RowProps, CellProps, FixedProps, SvgProps, TextRun, FormeDocument, FormeFont, FormeNode, FormeNodeKind, FormeStyle, FormePageConfig, FormePageSize, FormeEdges, FormeMetadata, FormeColumnDef, FormeColumnWidth, FormeDimension, FormeColor, FormeEdgeValues, FormeCornerValues, } from './types.js';
|
|
10
|
+
export type { Style, GridTrackSize, Edges, Corners, EdgeColors, DocumentProps, PageProps, ViewProps, TextProps, ImageProps, ColumnDef, TableProps, RowProps, CellProps, FixedProps, SvgProps, QrCodeProps, CanvasProps, CanvasContext, CanvasOp, WatermarkProps, ChartDataPoint, PieDataPoint, BarChartProps, LineChartProps, PieChartProps, TextRun, FormeDocument, FormeFont, FormeNode, FormeNodeKind, FormeStyle, FormePageConfig, FormePageSize, FormeEdges, FormeMetadata, FormeColumnDef, FormeColumnWidth, FormeDimension, FormeColor, FormeEdgeValues, FormeCornerValues, FormeGridTrackSize, FormeGridPlacement, } from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Components
|
|
2
|
-
export { Document, Page, View, Text, Image, Table, Row, Cell, Fixed, Svg, PageBreak } from './components.js';
|
|
2
|
+
export { Document, Page, View, Text, Image, Table, Row, Cell, Fixed, Svg, QrCode, Canvas, Watermark, PageBreak } from './components.js';
|
|
3
|
+
export { BarChart, LineChart, PieChart } from './charts.js';
|
|
3
4
|
// Serialization
|
|
4
5
|
export { serialize, serializeTemplate, mapStyle, mapDimension, parseColor, expandEdges, expandCorners } from './serialize.js';
|
|
5
6
|
// StyleSheet
|
package/dist/serialize.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare function serialize(element: ReactElement): FormeDocument;
|
|
|
8
8
|
export declare function mapStyle(style?: Style): FormeStyle;
|
|
9
9
|
export declare function mapDimension(val: number | string): FormeDimension;
|
|
10
10
|
export declare function parseColor(hex: string): FormeColor;
|
|
11
|
-
export declare function expandEdges(val: number | Edges): FormeEdges;
|
|
11
|
+
export declare function expandEdges(val: number | string | number[] | Edges): FormeEdges;
|
|
12
12
|
export declare function expandCorners(val: number | Corners): FormeCornerValues;
|
|
13
13
|
/**
|
|
14
14
|
* Serialize a React element tree into a Forme template JSON document.
|