@cognitap/radiant 0.0.69 → 0.0.70
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/CHANGELOG.md +4 -0
- package/dist/library/charts/Charts.d.ts +44 -0
- package/dist/library/charts/Charts.d.ts.map +1 -0
- package/dist/library/charts/Charts.js +92 -0
- package/dist/library/charts/Charts.js.map +1 -0
- package/dist/library/charts/index.d.ts +3 -0
- package/dist/library/charts/index.d.ts.map +1 -0
- package/dist/library/charts/index.js +3 -0
- package/dist/library/charts/index.js.map +1 -0
- package/dist/library/charts/palette.d.ts +20 -0
- package/dist/library/charts/palette.d.ts.map +1 -0
- package/dist/library/charts/palette.js +11 -0
- package/dist/library/charts/palette.js.map +1 -0
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +3 -1
- package/dist/library/cn.js.map +1 -1
- package/dist/library/internal/charts.d.ts +45 -0
- package/dist/library/internal/charts.d.ts.map +1 -0
- package/dist/library/internal/charts.js +98 -0
- package/dist/library/internal/charts.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +12 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +12 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +7 -8
- package/package.json +5 -1
- package/src/charts/Charts.tsx +442 -0
- package/src/charts/index.ts +16 -0
- package/src/charts/palette.ts +21 -0
- package/src/cn.ts +3 -1
- package/src/internal/charts.ts +150 -0
- package/src/themes/components.ts +12 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ColorPalette } from '../color/recommendColorPalette.js'
|
|
2
|
+
export function chartPalette(palette: ColorPalette) {
|
|
3
|
+
const roles = ['primary', 'support', 'success', 'warning', 'danger', 'neutral'] as const
|
|
4
|
+
const mode = (mode: 'light' | 'dark') =>
|
|
5
|
+
Object.fromEntries(
|
|
6
|
+
roles.map((role, index) => [
|
|
7
|
+
`series${index + 1}`,
|
|
8
|
+
role === 'neutral'
|
|
9
|
+
? palette.working[role][mode].foreground
|
|
10
|
+
: palette.working[role][mode].background
|
|
11
|
+
])
|
|
12
|
+
) as {
|
|
13
|
+
series1: string
|
|
14
|
+
series2: string
|
|
15
|
+
series3: string
|
|
16
|
+
series4: string
|
|
17
|
+
series5: string
|
|
18
|
+
series6: string
|
|
19
|
+
}
|
|
20
|
+
return { light: mode('light'), dark: mode('dark') }
|
|
21
|
+
}
|
package/src/cn.ts
CHANGED
|
@@ -72,6 +72,7 @@ export const cn: CnFunction = createCn({
|
|
|
72
72
|
'text-(--rad-tabs-font-size)'
|
|
73
73
|
],
|
|
74
74
|
h: [
|
|
75
|
+
'h-(--rad-chart-height)',
|
|
75
76
|
'h-(--rad-resizable-handle-size)',
|
|
76
77
|
'h-(--rad-meter-height)',
|
|
77
78
|
'h-(--rad-native-select-height)',
|
|
@@ -118,6 +119,7 @@ export const cn: CnFunction = createCn({
|
|
|
118
119
|
],
|
|
119
120
|
mb: ['mb-(--rad-multi-select-gap)', 'mb-(--rad-fieldset-gap)'],
|
|
120
121
|
gap: [
|
|
122
|
+
'gap-(--rad-chart-gap)',
|
|
121
123
|
'gap-(--rad-carousel-gap)',
|
|
122
124
|
'gap-(--rad-carousel-slide-gap)',
|
|
123
125
|
'gap-(--rad-form-gap)',
|
|
@@ -345,7 +347,7 @@ export const cn: CnFunction = createCn({
|
|
|
345
347
|
'[animation-duration:var(--rad-spinner-duration)]',
|
|
346
348
|
'[animation-duration:var(--rad-skeleton-duration)]'
|
|
347
349
|
],
|
|
348
|
-
'stroke-w': ['stroke-(--rad-spinner-stroke-width)'],
|
|
350
|
+
'stroke-w': ['stroke-(--rad-chart-stroke-width)', 'stroke-(--rad-spinner-stroke-width)'],
|
|
349
351
|
opacity: ['opacity-(--rad-spinner-track-opacity)', 'opacity-(--rad-tabs-disabled-opacity)'],
|
|
350
352
|
'backdrop-filter': [
|
|
351
353
|
'[backdrop-filter:var(--rad-popover-backdrop-filter)]',
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
export type ChartDatum = {
|
|
2
|
+
key: string
|
|
3
|
+
label: string
|
|
4
|
+
values: Readonly<Record<string, number | null>>
|
|
5
|
+
}
|
|
6
|
+
export type ChartSeries = { key: string; label: string }
|
|
7
|
+
export type ChartKind = 'line' | 'bar' | 'area' | 'pie'
|
|
8
|
+
export type ChartMark = {
|
|
9
|
+
row: number
|
|
10
|
+
series: number
|
|
11
|
+
value: number
|
|
12
|
+
x: number
|
|
13
|
+
y: number
|
|
14
|
+
path?: string
|
|
15
|
+
width?: number
|
|
16
|
+
height?: number
|
|
17
|
+
top?: number
|
|
18
|
+
}
|
|
19
|
+
export function validateChart(
|
|
20
|
+
data: readonly ChartDatum[],
|
|
21
|
+
series: readonly ChartSeries[],
|
|
22
|
+
pie = false
|
|
23
|
+
) {
|
|
24
|
+
for (const [name, values] of [
|
|
25
|
+
['data', data],
|
|
26
|
+
['series', series]
|
|
27
|
+
] as const) {
|
|
28
|
+
const keys = values.map(item => item.key)
|
|
29
|
+
if (
|
|
30
|
+
keys.some(key => !key.trim()) ||
|
|
31
|
+
new Set(keys).size !== keys.length ||
|
|
32
|
+
values.some(item => !item.label.trim())
|
|
33
|
+
)
|
|
34
|
+
throw new TypeError(`Chart ${name} needs unique nonempty keys and nonempty labels.`)
|
|
35
|
+
}
|
|
36
|
+
if (!series.length || series.length > 6 || (pie && series.length !== 1))
|
|
37
|
+
throw new TypeError(pie ? 'PieChart needs one series.' : 'Charts need one to six series.')
|
|
38
|
+
for (const row of data)
|
|
39
|
+
for (const item of series) {
|
|
40
|
+
const value = row.values[item.key]
|
|
41
|
+
if (value != null && (!Number.isFinite(value) || (pie && value < 0)))
|
|
42
|
+
throw new TypeError('Chart values must be finite; pie values must also be nonnegative.')
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const point = (x: number, y: number) => `${Number(x.toFixed(4))} ${Number(y.toFixed(4))}`
|
|
46
|
+
export function chartGeometry(
|
|
47
|
+
kind: ChartKind,
|
|
48
|
+
data: readonly ChartDatum[],
|
|
49
|
+
series: readonly ChartSeries[],
|
|
50
|
+
viewport?: { width: number; height: number } | null
|
|
51
|
+
) {
|
|
52
|
+
validateChart(data, series, kind === 'pie')
|
|
53
|
+
const width = Math.max(480, data.length * 72, viewport?.width ?? 0),
|
|
54
|
+
height = Math.max(160, viewport?.height ?? 320),
|
|
55
|
+
left = 48,
|
|
56
|
+
right = 24,
|
|
57
|
+
top = 24,
|
|
58
|
+
bottom = 44,
|
|
59
|
+
innerWidth = width - left - right,
|
|
60
|
+
innerHeight = height - top - bottom
|
|
61
|
+
const marks: ChartMark[] = [],
|
|
62
|
+
paths: { series: number; line: string; area: string }[] = [],
|
|
63
|
+
ticks: { value: number; y: number }[] = []
|
|
64
|
+
if (kind === 'pie') {
|
|
65
|
+
const values = data.map(row => row.values[series[0]!.key] ?? 0),
|
|
66
|
+
magnitude = Math.max(1, ...values),
|
|
67
|
+
total = values.reduce((sum, value) => sum + value / magnitude, 0),
|
|
68
|
+
cx = width / 2,
|
|
69
|
+
cy = (height - bottom) / 2,
|
|
70
|
+
radius = Math.min(width / 2 - 32, (height - bottom) / 2 - 16)
|
|
71
|
+
let start = -Math.PI / 2
|
|
72
|
+
values.forEach((value, row) => {
|
|
73
|
+
if (value <= 0 || total <= 0) return
|
|
74
|
+
const angle = (value / magnitude / total) * Math.PI * 2,
|
|
75
|
+
end = start + angle,
|
|
76
|
+
middle = start + angle / 2,
|
|
77
|
+
coords = (a: number) => point(cx + Math.cos(a) * radius, cy + Math.sin(a) * radius)
|
|
78
|
+
marks.push({
|
|
79
|
+
row,
|
|
80
|
+
series: 0,
|
|
81
|
+
value,
|
|
82
|
+
x: cx + Math.cos(middle) * radius * 0.65,
|
|
83
|
+
y: cy + Math.sin(middle) * radius * 0.65,
|
|
84
|
+
path: `M ${point(cx, cy)} L ${coords(start)} A ${radius} ${radius} 0 0 1 ${coords(middle)} A ${radius} ${radius} 0 0 1 ${coords(end)} Z`
|
|
85
|
+
})
|
|
86
|
+
start = end
|
|
87
|
+
})
|
|
88
|
+
return { width, height, marks, paths, ticks, zero: cy, labels: [] }
|
|
89
|
+
}
|
|
90
|
+
const values = data.flatMap(row => series.flatMap(item => row.values[item.key] ?? [])),
|
|
91
|
+
magnitude = Math.max(1, ...values.map(Math.abs)),
|
|
92
|
+
low = Math.min(0, ...values.map(value => value / magnitude)),
|
|
93
|
+
rawHigh = Math.max(0, ...values.map(value => value / magnitude)),
|
|
94
|
+
high = rawHigh === low ? low + 1 : rawHigh,
|
|
95
|
+
span = high - low
|
|
96
|
+
const y = (value: number) => top + ((high - value / magnitude) / span) * innerHeight,
|
|
97
|
+
zero = y(0),
|
|
98
|
+
step = innerWidth / Math.max(1, data.length)
|
|
99
|
+
for (let i = 0; i < 5; i++) {
|
|
100
|
+
const normalized = low + (span * i) / 4
|
|
101
|
+
ticks.push({ value: normalized * magnitude, y: top + innerHeight - (innerHeight * i) / 4 })
|
|
102
|
+
}
|
|
103
|
+
series.forEach((item, seriesIndex) => {
|
|
104
|
+
let segment: ChartMark[] = []
|
|
105
|
+
const close = () => {
|
|
106
|
+
if (!segment.length) return
|
|
107
|
+
const line = segment
|
|
108
|
+
.map((mark, index) => `${index ? 'L' : 'M'} ${point(mark.x, mark.y)}`)
|
|
109
|
+
.join(' ')
|
|
110
|
+
paths.push({
|
|
111
|
+
series: seriesIndex,
|
|
112
|
+
line,
|
|
113
|
+
area: `${line} L ${point(segment.at(-1)!.x, zero)} L ${point(segment[0]!.x, zero)} Z`
|
|
114
|
+
})
|
|
115
|
+
segment = []
|
|
116
|
+
}
|
|
117
|
+
data.forEach((row, rowIndex) => {
|
|
118
|
+
const value = row.values[item.key]
|
|
119
|
+
if (value == null) {
|
|
120
|
+
close()
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
const x = left + step * (rowIndex + 0.5),
|
|
124
|
+
barWidth = (step * 0.75) / series.length,
|
|
125
|
+
barLeft = x - step * 0.375 + seriesIndex * barWidth
|
|
126
|
+
const mark: ChartMark = {
|
|
127
|
+
row: rowIndex,
|
|
128
|
+
series: seriesIndex,
|
|
129
|
+
value,
|
|
130
|
+
x: kind === 'bar' ? barLeft + barWidth / 2 : x,
|
|
131
|
+
y: y(value),
|
|
132
|
+
width: barWidth * 0.9,
|
|
133
|
+
height: Math.abs(y(value) - zero),
|
|
134
|
+
top: Math.min(y(value), zero)
|
|
135
|
+
}
|
|
136
|
+
marks.push(mark)
|
|
137
|
+
segment.push(mark)
|
|
138
|
+
})
|
|
139
|
+
close()
|
|
140
|
+
})
|
|
141
|
+
return {
|
|
142
|
+
width,
|
|
143
|
+
height,
|
|
144
|
+
marks,
|
|
145
|
+
paths,
|
|
146
|
+
ticks,
|
|
147
|
+
zero,
|
|
148
|
+
labels: data.map((row, index) => ({ label: row.label, x: left + step * (index + 0.5) }))
|
|
149
|
+
}
|
|
150
|
+
}
|
package/src/themes/components.ts
CHANGED
|
@@ -162,6 +162,18 @@ export const componentRegistry = {
|
|
|
162
162
|
shadow: 'css',
|
|
163
163
|
gap: 'css'
|
|
164
164
|
},
|
|
165
|
+
chart: {
|
|
166
|
+
gap: 'css',
|
|
167
|
+
height: 'css',
|
|
168
|
+
strokeWidth: 'css',
|
|
169
|
+
gridColor: 'css',
|
|
170
|
+
series1: 'css',
|
|
171
|
+
series2: 'css',
|
|
172
|
+
series3: 'css',
|
|
173
|
+
series4: 'css',
|
|
174
|
+
series5: 'css',
|
|
175
|
+
series6: 'css'
|
|
176
|
+
},
|
|
165
177
|
form: { gap: 'css' },
|
|
166
178
|
carousel: { gap: 'css', slideGap: 'css', radius: 'css' },
|
|
167
179
|
buttonGroup: { radius: 'css' },
|