-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathgenerate-legend-test-page.ts
More file actions
166 lines (157 loc) · 4.81 KB
/
Copy pathgenerate-legend-test-page.ts
File metadata and controls
166 lines (157 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Generate a static HTML gallery of server-rendered SVG charts with legends
* at all four positions, plus background and colorScheme tests.
*
* Output: test-results/server-legend-gallery.html
* Used by: integration-tests/server-legend.spec.ts
*/
import * as fs from "fs"
import * as path from "path"
// Run static generation through the Node export. The source renderer uses the
// browser-compatible React entry for edge bundles, which intentionally keeps a
// MessagePort alive when evaluated directly by Node on React 19.
import { renderChart } from "semiotic/server/node"
const outDir = path.resolve(__dirname, "test-results")
if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true })
const barData = [
{ category: "A", value: 10, group: "X" },
{ category: "B", value: 20, group: "Y" },
{ category: "C", value: 15, group: "X" },
{ category: "D", value: 25, group: "Y" },
]
const lineData = [
{ x: 1, y: 10, series: "Alpha" },
{ x: 2, y: 20, series: "Alpha" },
{ x: 3, y: 15, series: "Alpha" },
{ x: 1, y: 5, series: "Beta" },
{ x: 2, y: 15, series: "Beta" },
{ x: 3, y: 25, series: "Beta" },
]
const pieData = [
{ category: "Red", value: 30 },
{ category: "Blue", value: 50 },
{ category: "Green", value: 20 },
]
interface TestCase {
id: string
svg: string
}
const cases: TestCase[] = [
{
id: "bar-legend-right",
svg: renderChart("BarChart", {
data: barData, categoryAccessor: "category", valueAccessor: "value",
colorBy: "group", showLegend: true, legendPosition: "right",
width: 400, height: 300,
}),
},
{
id: "bar-legend-left",
svg: renderChart("BarChart", {
data: barData, categoryAccessor: "category", valueAccessor: "value",
colorBy: "group", showLegend: true, legendPosition: "left",
width: 400, height: 300,
}),
},
{
id: "bar-legend-top",
svg: renderChart("BarChart", {
data: barData, categoryAccessor: "category", valueAccessor: "value",
colorBy: "group", showLegend: true, legendPosition: "top",
width: 400, height: 300,
}),
},
{
id: "bar-legend-bottom",
svg: renderChart("BarChart", {
data: barData, categoryAccessor: "category", valueAccessor: "value",
colorBy: "group", showLegend: true, legendPosition: "bottom",
width: 400, height: 300,
}),
},
{
id: "line-legend-right",
svg: renderChart("LineChart", {
data: lineData, xAccessor: "x", yAccessor: "y",
lineBy: "series", colorBy: "series",
showLegend: true, legendPosition: "right",
width: 400, height: 300,
}),
},
{
id: "line-legend-left",
svg: renderChart("LineChart", {
data: lineData, xAccessor: "x", yAccessor: "y",
lineBy: "series", colorBy: "series",
showLegend: true, legendPosition: "left",
width: 400, height: 300,
}),
},
{
id: "pie-legend-right",
svg: renderChart("PieChart", {
data: pieData, categoryAccessor: "category", valueAccessor: "value",
colorBy: "category", showLegend: true, legendPosition: "right",
width: 400, height: 300,
}),
},
{
id: "pie-legend-bottom",
svg: renderChart("PieChart", {
data: pieData, categoryAccessor: "category", valueAccessor: "value",
colorBy: "category", showLegend: true, legendPosition: "bottom",
width: 400, height: 300,
}),
},
{
id: "pie-background",
svg: renderChart("PieChart", {
data: pieData, categoryAccessor: "category", valueAccessor: "value",
colorBy: "category", showLegend: true, background: "#1a1a2e",
width: 400, height: 300,
}),
},
{
id: "line-colorscheme",
svg: renderChart("LineChart", {
data: lineData, xAccessor: "x", yAccessor: "y",
lineBy: "series", colorBy: "series",
colorScheme: ["#0EA4AF", "#DB2187"],
showLegend: true,
width: 400, height: 300,
}),
},
{
id: "stacked-bar-legend",
svg: renderChart("StackedBarChart", {
data: barData, categoryAccessor: "category", valueAccessor: "value",
stackBy: "group", colorBy: "group",
showLegend: true, legendPosition: "right",
width: 400, height: 300,
}),
},
]
const html = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Server Legend Gallery</title>
<style>
body { font-family: sans-serif; padding: 20px; background: #f5f5f5; }
.case { margin-bottom: 24px; background: white; padding: 12px; border-radius: 8px; display: inline-block; }
.case h3 { margin: 0 0 8px 0; font-size: 14px; color: #333; }
</style>
</head>
<body>
<h1>Server-Rendered SVG Legend Gallery</h1>
${cases.map(c => `
<div class="case">
<h3>${c.id}</h3>
${c.svg}
</div>
`).join("\n")}
</body>
</html>`
const outPath = path.join(outDir, "server-legend-gallery.html")
fs.writeFileSync(outPath, html)
console.log(`Generated ${outPath} with ${cases.length} charts`)