@tylix/charts 0.5.1 → 0.7.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/LICENSE +21 -0
- package/components/AreaChart.tyx +40 -0
- package/components/BarChart.tyx +159 -0
- package/components/Donut.tyx +37 -0
- package/components/LineChart.tyx +297 -0
- package/components/PieChart.tyx +118 -0
- package/components/Sparkline.tyx +57 -0
- package/package.json +32 -7
- package/src/area.js +119 -0
- package/src/band.js +37 -0
- package/src/bar.js +218 -0
- package/src/crosshair.js +162 -0
- package/src/index.d.ts +292 -6
- package/src/index.js +14 -2
- package/src/line.js +1 -1
- package/src/path.js +92 -16
- package/src/pie.js +187 -0
- package/src/svg.js +380 -25
- package/tylix-components.json +8 -0
package/src/pie.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { DEFAULT_PALETTE } from './line.js'
|
|
2
|
+
|
|
3
|
+
const TAU = Math.PI * 2
|
|
4
|
+
const fmt = (n) => Math.round(n * 100) / 100
|
|
5
|
+
|
|
6
|
+
// Angles start at 12 o'clock and run clockwise.
|
|
7
|
+
const polar = (cx, cy, r, a) => ({ x: cx + r * Math.sin(a), y: cy - r * Math.cos(a) })
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Annular sector (or pie wedge when innerRadius is 0) as SVG path data,
|
|
11
|
+
* with optionally rounded corners. Render with fill-rule="evenodd" so a
|
|
12
|
+
* full-circle ring keeps its hole.
|
|
13
|
+
*/
|
|
14
|
+
export function arcPath({
|
|
15
|
+
cx = 0,
|
|
16
|
+
cy = 0,
|
|
17
|
+
innerRadius = 0,
|
|
18
|
+
outerRadius = 1,
|
|
19
|
+
startAngle = 0,
|
|
20
|
+
endAngle = TAU,
|
|
21
|
+
cornerRadius = 0,
|
|
22
|
+
} = {}) {
|
|
23
|
+
const R = outerRadius
|
|
24
|
+
const r = Math.max(0, innerRadius)
|
|
25
|
+
const span = endAngle - startAngle
|
|
26
|
+
if (!(span > 0) || !(R > 0)) return ''
|
|
27
|
+
|
|
28
|
+
const P = (rad, a) => {
|
|
29
|
+
const p = polar(cx, cy, rad, a)
|
|
30
|
+
return `${fmt(p.x)},${fmt(p.y)}`
|
|
31
|
+
}
|
|
32
|
+
const A = (rad, large, sweep, to) => `A${fmt(rad)},${fmt(rad)} 0 ${large} ${sweep} ${to}`
|
|
33
|
+
|
|
34
|
+
// Full circle / ring: an arc cannot start and end on the same point.
|
|
35
|
+
if (span >= TAU - 1e-6) {
|
|
36
|
+
const circle = (rad, sweep) =>
|
|
37
|
+
`M${fmt(cx)},${fmt(cy - rad)}A${fmt(rad)},${fmt(rad)} 0 1 ${sweep} ${fmt(cx)},${fmt(cy + rad)}` +
|
|
38
|
+
`A${fmt(rad)},${fmt(rad)} 0 1 ${sweep} ${fmt(cx)},${fmt(cy - rad)}Z`
|
|
39
|
+
return r > 0 ? circle(R, 1) + circle(r, 0) : circle(R, 1)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Corner radius limited by the ring thickness and the slice's angular width.
|
|
43
|
+
let c = Math.max(0, Math.min(cornerRadius, (R - r) / 2))
|
|
44
|
+
if (c > 0) {
|
|
45
|
+
const s = span / 2 >= Math.PI / 2 ? 1 : Math.sin(span / 2)
|
|
46
|
+
c = Math.min(c, (R * s) / (1 + s))
|
|
47
|
+
if (r > 0 && s < 1) c = Math.min(c, (r * s) / (1 - s))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const large = span > Math.PI ? 1 : 0
|
|
51
|
+
|
|
52
|
+
if (c < 0.25) {
|
|
53
|
+
const arc = A(R, large, 1, P(R, endAngle))
|
|
54
|
+
if (r <= 0) return `M${fmt(cx)},${fmt(cy)}L${P(R, startAngle)}${arc}Z`
|
|
55
|
+
return `M${P(R, startAngle)}${arc}L${P(r, endAngle)}${A(r, large, 0, P(r, startAngle))}Z`
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const dO = Math.asin(c / (R - c))
|
|
59
|
+
const o0 = startAngle + dO
|
|
60
|
+
const o1 = endAngle - dO
|
|
61
|
+
const dist = (R - c) * Math.cos(dO)
|
|
62
|
+
const largeO = o1 - o0 > Math.PI ? 1 : 0
|
|
63
|
+
|
|
64
|
+
if (r <= 0) {
|
|
65
|
+
return (
|
|
66
|
+
`M${fmt(cx)},${fmt(cy)}L${P(dist, startAngle)}` +
|
|
67
|
+
A(c, 0, 1, P(R, o0)) +
|
|
68
|
+
A(R, largeO, 1, P(R, o1)) +
|
|
69
|
+
A(c, 0, 1, P(dist, endAngle)) +
|
|
70
|
+
'Z'
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const dI = Math.asin(c / (r + c))
|
|
75
|
+
const i0 = startAngle + dI
|
|
76
|
+
const i1 = endAngle - dI
|
|
77
|
+
const distI = (r + c) * Math.cos(dI)
|
|
78
|
+
const largeI = i1 - i0 > Math.PI ? 1 : 0
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
`M${P(distI, startAngle)}L${P(dist, startAngle)}` +
|
|
82
|
+
A(c, 0, 1, P(R, o0)) +
|
|
83
|
+
A(R, largeO, 1, P(R, o1)) +
|
|
84
|
+
A(c, 0, 1, P(dist, endAngle)) +
|
|
85
|
+
`L${P(distI, endAngle)}` +
|
|
86
|
+
A(c, 0, 1, P(r, i1)) +
|
|
87
|
+
A(r, largeI, 0, P(r, i0)) +
|
|
88
|
+
A(c, 0, 1, P(distI, startAngle)) +
|
|
89
|
+
'Z'
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Computes pie / donut geometry.
|
|
95
|
+
*
|
|
96
|
+
* data: [{ label, value, color? }] or plain numbers
|
|
97
|
+
* options:
|
|
98
|
+
* width, height - box size (chart is centered, radius = min / 2 - margin)
|
|
99
|
+
* innerRadius - 0..1, fraction of the outer radius (0 = pie)
|
|
100
|
+
* cornerRadius - px, rounded slice corners
|
|
101
|
+
* gap - px gap between slices
|
|
102
|
+
* startAngle - degrees, 0 = 12 o'clock
|
|
103
|
+
* sort - 'none' | 'desc' | 'asc'
|
|
104
|
+
* margin - px kept free around the chart
|
|
105
|
+
*/
|
|
106
|
+
export function pieChart(
|
|
107
|
+
data = [],
|
|
108
|
+
{
|
|
109
|
+
width = 240,
|
|
110
|
+
height = width,
|
|
111
|
+
innerRadius = 0,
|
|
112
|
+
cornerRadius = 4,
|
|
113
|
+
gap = 2,
|
|
114
|
+
startAngle = 0,
|
|
115
|
+
sort = 'none',
|
|
116
|
+
margin = 4,
|
|
117
|
+
formatValue = (v) => String(v),
|
|
118
|
+
formatPercent = (p) => (p > 0 && p < 0.01 ? '<1%' : `${Math.round(p * 100)}%`),
|
|
119
|
+
} = {}
|
|
120
|
+
) {
|
|
121
|
+
const items = data.map((d, i) => {
|
|
122
|
+
const o = typeof d === 'number' ? { value: d } : d ?? {}
|
|
123
|
+
const value = typeof o.value === 'number' && Number.isFinite(o.value) && o.value > 0 ? o.value : 0
|
|
124
|
+
return {
|
|
125
|
+
index: i,
|
|
126
|
+
label: o.label ?? `Item ${i + 1}`,
|
|
127
|
+
value,
|
|
128
|
+
color: o.color ?? DEFAULT_PALETTE[i % DEFAULT_PALETTE.length],
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
if (sort === 'desc') items.sort((a, b) => b.value - a.value)
|
|
132
|
+
else if (sort === 'asc') items.sort((a, b) => a.value - b.value)
|
|
133
|
+
|
|
134
|
+
const total = items.reduce((sum, d) => sum + d.value, 0)
|
|
135
|
+
const cx = width / 2
|
|
136
|
+
const cy = height / 2
|
|
137
|
+
const outer = Math.max(0, Math.min(width, height) / 2 - margin)
|
|
138
|
+
const inner = outer * Math.min(0.95, Math.max(0, innerRadius))
|
|
139
|
+
const mid = (outer + inner) / 2
|
|
140
|
+
const visible = items.filter((d) => d.value > 0).length
|
|
141
|
+
const pad = visible > 1 && mid > 0 ? gap / mid : 0
|
|
142
|
+
|
|
143
|
+
let angle = (startAngle * Math.PI) / 180
|
|
144
|
+
const slices = items.map((d) => {
|
|
145
|
+
const percent = total > 0 ? d.value / total : 0
|
|
146
|
+
const a0 = angle
|
|
147
|
+
const a1 = angle + percent * TAU
|
|
148
|
+
angle = a1
|
|
149
|
+
const mA = (a0 + a1) / 2
|
|
150
|
+
let s0 = a0 + pad / 2
|
|
151
|
+
let s1 = a1 - pad / 2
|
|
152
|
+
if (s1 <= s0) {
|
|
153
|
+
s0 = a0
|
|
154
|
+
s1 = a1
|
|
155
|
+
}
|
|
156
|
+
const labelPt = polar(cx, cy, outer + 14, mA)
|
|
157
|
+
const center = polar(cx, cy, mid, mA)
|
|
158
|
+
return {
|
|
159
|
+
index: d.index,
|
|
160
|
+
label: d.label,
|
|
161
|
+
value: d.value,
|
|
162
|
+
valueLabel: formatValue(d.value),
|
|
163
|
+
percent,
|
|
164
|
+
percentLabel: formatPercent(percent),
|
|
165
|
+
color: d.color,
|
|
166
|
+
startAngle: a0,
|
|
167
|
+
endAngle: a1,
|
|
168
|
+
midAngle: mA,
|
|
169
|
+
path:
|
|
170
|
+
percent > 0
|
|
171
|
+
? arcPath({
|
|
172
|
+
cx, cy, innerRadius: inner, outerRadius: outer,
|
|
173
|
+
startAngle: s0, endAngle: s1, cornerRadius,
|
|
174
|
+
})
|
|
175
|
+
: '',
|
|
176
|
+
centroid: { x: center.x, y: center.y },
|
|
177
|
+
labelPoint: { x: labelPt.x, y: labelPt.y },
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
return { width, height, cx, cy, outerRadius: outer, innerRadius: inner, total, slices }
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** pieChart() with a hole in the middle (innerRadius defaults to 0.62). */
|
|
185
|
+
export function donutChart(data, options = {}) {
|
|
186
|
+
return pieChart(data, { innerRadius: 0.62, ...options })
|
|
187
|
+
}
|
package/src/svg.js
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { sparkline } from './sparkline.js'
|
|
2
|
-
import { lineChart } from './line.js'
|
|
2
|
+
import { lineChart, DEFAULT_PALETTE } from './line.js'
|
|
3
|
+
import { areaChart } from './area.js'
|
|
4
|
+
import { barChart } from './bar.js'
|
|
5
|
+
import { pieChart } from './pie.js'
|
|
6
|
+
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// Shared helpers
|
|
9
|
+
//
|
|
10
|
+
// Theming (all optional CSS custom properties, set them on any ancestor):
|
|
11
|
+
// --tylix-chart-1 … --tylix-chart-5 series colors
|
|
12
|
+
// --tylix-chart-bg surface color (rings around dots)
|
|
13
|
+
// Text and gridlines use currentColor, so they follow your text color.
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
3
15
|
|
|
4
16
|
const escapeXml = (s) =>
|
|
5
17
|
String(s)
|
|
@@ -8,6 +20,93 @@ const escapeXml = (s) =>
|
|
|
8
20
|
.replace(/>/g, '>')
|
|
9
21
|
.replace(/"/g, '"')
|
|
10
22
|
|
|
23
|
+
const fmt = (n) => Math.round(n * 100) / 100
|
|
24
|
+
const BG = 'var(--tylix-chart-bg, #fff)'
|
|
25
|
+
const CHAR_W = 6.2
|
|
26
|
+
|
|
27
|
+
let uid = 0
|
|
28
|
+
const nextId = () => `tc${++uid}`
|
|
29
|
+
|
|
30
|
+
const open = (id, w, h, label, kind) =>
|
|
31
|
+
`<svg xmlns="http://www.w3.org/2000/svg" class="tylix-chart tylix-chart--${kind} ${id}" ` +
|
|
32
|
+
`width="${w}" height="${h}" viewBox="0 0 ${w} ${h}" role="img" aria-label="${escapeXml(label)}" ` +
|
|
33
|
+
`font-family="system-ui, -apple-system, 'Segoe UI', sans-serif" font-size="11">`
|
|
34
|
+
|
|
35
|
+
const MUTED = 'fill="currentColor" fill-opacity="0.6"'
|
|
36
|
+
|
|
37
|
+
function seriesMeta(series = []) {
|
|
38
|
+
return series.map((s, i) => ({
|
|
39
|
+
name: s.name ?? `Series ${i + 1}`,
|
|
40
|
+
color: s.color ?? DEFAULT_PALETTE[i % DEFAULT_PALETTE.length],
|
|
41
|
+
}))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function legendLayout(items, maxWidth) {
|
|
45
|
+
const rows = []
|
|
46
|
+
let row = []
|
|
47
|
+
let x = 0
|
|
48
|
+
for (const it of items) {
|
|
49
|
+
const w = 16 + String(it.name).length * CHAR_W + 14
|
|
50
|
+
if (row.length && x + w > maxWidth) {
|
|
51
|
+
rows.push(row)
|
|
52
|
+
row = []
|
|
53
|
+
x = 0
|
|
54
|
+
}
|
|
55
|
+
row.push({ ...it, x, w })
|
|
56
|
+
x += w
|
|
57
|
+
}
|
|
58
|
+
if (row.length) rows.push(row)
|
|
59
|
+
return { rows, height: rows.length * 18 }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function legendMarkup(layout, x0, y0) {
|
|
63
|
+
let out = ''
|
|
64
|
+
layout.rows.forEach((row, r) => {
|
|
65
|
+
const cy = y0 + r * 18 + 9
|
|
66
|
+
for (const it of row) {
|
|
67
|
+
out +=
|
|
68
|
+
`<rect x="${fmt(x0 + it.x)}" y="${fmt(cy - 5)}" width="10" height="10" rx="3" fill="${escapeXml(it.color)}"/>` +
|
|
69
|
+
`<text x="${fmt(x0 + it.x + 16)}" y="${fmt(cy)}" dominant-baseline="middle" fill="currentColor" fill-opacity="0.75">${escapeXml(it.name)}</text>`
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
return out
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Reserves room above the plot for a legend (default: shown for 2+ series).
|
|
76
|
+
function planLegend(options, items) {
|
|
77
|
+
const show = options.legend ?? items.length > 1
|
|
78
|
+
if (!show || !items.length) return { layout: null, options }
|
|
79
|
+
const layout = legendLayout(items, (options.width ?? 480) - 24)
|
|
80
|
+
const top = (options.margin?.top ?? 12) + layout.height + 6
|
|
81
|
+
return { layout, options: { ...options, margin: { ...options.margin, top } } }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function gridlines(ticks, plot, horizontalLines) {
|
|
85
|
+
let out = ''
|
|
86
|
+
for (const t of ticks) {
|
|
87
|
+
const zero = t.value === 0
|
|
88
|
+
const stroke = zero
|
|
89
|
+
? 'stroke="currentColor" stroke-opacity="0.3"'
|
|
90
|
+
: 'stroke="currentColor" stroke-opacity="0.1" stroke-dasharray="3 4"'
|
|
91
|
+
if (horizontalLines) {
|
|
92
|
+
const y = t.y ?? t.pos
|
|
93
|
+
out +=
|
|
94
|
+
`<line x1="${plot.x}" x2="${plot.x + plot.width}" y1="${fmt(y)}" y2="${fmt(y)}" ${stroke}/>` +
|
|
95
|
+
`<text x="${plot.x - 8}" y="${fmt(y)}" text-anchor="end" dominant-baseline="middle" ${MUTED}>${escapeXml(t.label)}</text>`
|
|
96
|
+
} else {
|
|
97
|
+
const x = t.pos
|
|
98
|
+
out +=
|
|
99
|
+
`<line x1="${fmt(x)}" x2="${fmt(x)}" y1="${plot.y}" y2="${plot.y + plot.height}" ${stroke}/>` +
|
|
100
|
+
`<text x="${fmt(x)}" y="${plot.y + plot.height + 16}" text-anchor="middle" ${MUTED}>${escapeXml(t.label)}</text>`
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return out
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
// Sparkline
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
|
|
11
110
|
/** Renders a sparkline as an SVG string. Colors inherit via currentColor. */
|
|
12
111
|
export function sparklineSvg(data, options = {}) {
|
|
13
112
|
const s = sparkline(data, options)
|
|
@@ -24,48 +123,304 @@ export function sparklineSvg(data, options = {}) {
|
|
|
24
123
|
)
|
|
25
124
|
}
|
|
26
125
|
if (showLastPoint && s.last) {
|
|
27
|
-
parts.push(`<circle cx="${s.last.x}" cy="${s.last.y}" r="2" fill="currentColor"/>`)
|
|
126
|
+
parts.push(`<circle cx="${fmt(s.last.x)}" cy="${fmt(s.last.y)}" r="2" fill="currentColor"/>`)
|
|
28
127
|
}
|
|
29
128
|
parts.push('</svg>')
|
|
30
129
|
return parts.join('')
|
|
31
130
|
}
|
|
32
131
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
132
|
+
// ---------------------------------------------------------------------------
|
|
133
|
+
// Line + area (shared renderer)
|
|
134
|
+
// ---------------------------------------------------------------------------
|
|
135
|
+
|
|
136
|
+
function xyChartSvg(c, o) {
|
|
137
|
+
const { id, label, kind, fill, dots, formatX, formatY, layout, interactive } = o
|
|
37
138
|
const { plot } = c
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
]
|
|
139
|
+
const stacked = !!c.stacked
|
|
140
|
+
const out = [open(id, c.width, c.height, label, kind)]
|
|
41
141
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
`<line x1="${plot.x}" x2="${plot.x + plot.width}" y1="${t.y}" y2="${t.y}" stroke="currentColor" stroke-opacity="0.12"/>`,
|
|
46
|
-
`<text x="${plot.x - 6}" y="${t.y}" text-anchor="end" dominant-baseline="middle" fill="currentColor" fill-opacity="0.65">${escapeXml(t.label)}</text>`
|
|
142
|
+
if (interactive && dots === 'hover') {
|
|
143
|
+
out.push(
|
|
144
|
+
`<style>.${id} .tc-pt .tc-dot{opacity:0;transition:opacity .12s}.${id} .tc-pt:hover .tc-dot{opacity:1}</style>`
|
|
47
145
|
)
|
|
48
146
|
}
|
|
49
147
|
|
|
50
|
-
|
|
148
|
+
if (fill && !stacked) {
|
|
149
|
+
out.push('<defs>')
|
|
150
|
+
c.series.forEach((s, i) => {
|
|
151
|
+
out.push(
|
|
152
|
+
`<linearGradient id="${id}-g${i}" x1="0" y1="0" x2="0" y2="1">` +
|
|
153
|
+
`<stop offset="0" style="stop-color:${escapeXml(s.color)};stop-opacity:0.32"/>` +
|
|
154
|
+
`<stop offset="1" style="stop-color:${escapeXml(s.color)};stop-opacity:0.02"/></linearGradient>`
|
|
155
|
+
)
|
|
156
|
+
})
|
|
157
|
+
out.push('</defs>')
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (layout) out.push(legendMarkup(layout, plot.x, 4))
|
|
161
|
+
|
|
162
|
+
out.push(gridlines(c.yTicks, plot, true))
|
|
51
163
|
for (const t of c.xTicks) {
|
|
52
|
-
|
|
53
|
-
`<text x="${t.x}" y="${plot.y + plot.height + 16}" text-anchor="middle"
|
|
164
|
+
out.push(
|
|
165
|
+
`<text x="${fmt(t.x)}" y="${plot.y + plot.height + 16}" text-anchor="middle" ${MUTED}>${escapeXml(t.label)}</text>`
|
|
54
166
|
)
|
|
55
167
|
}
|
|
56
168
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
169
|
+
c.series.forEach((s, i) => {
|
|
170
|
+
if (fill && s.areaPath) {
|
|
171
|
+
const paint = stacked
|
|
172
|
+
? `fill="${escapeXml(s.color)}" fill-opacity="0.8"`
|
|
173
|
+
: `fill="url(#${id}-g${i})"`
|
|
174
|
+
out.push(`<path d="${s.areaPath}" ${paint} stroke="none"/>`)
|
|
61
175
|
}
|
|
62
176
|
if (s.path) {
|
|
63
|
-
|
|
64
|
-
`<path d="${s.path}" fill="none" stroke="${s.color}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>`
|
|
177
|
+
out.push(
|
|
178
|
+
`<path d="${s.path}" fill="none" stroke="${escapeXml(s.color)}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>`
|
|
65
179
|
)
|
|
66
180
|
}
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
if (dots !== 'none') {
|
|
184
|
+
c.series.forEach((s) => {
|
|
185
|
+
const color = escapeXml(s.color)
|
|
186
|
+
let lastIdx = -1
|
|
187
|
+
s.points.forEach((p, k) => {
|
|
188
|
+
if (p.py !== null) lastIdx = k
|
|
189
|
+
})
|
|
190
|
+
s.points.forEach((p, k) => {
|
|
191
|
+
if (p.py === null) return
|
|
192
|
+
const tip = `<title>${escapeXml(`${s.name} · ${formatX(p.x)}: ${formatY(p.value ?? 0)}`)}</title>`
|
|
193
|
+
const dot = `fill="${color}" stroke="${BG}" stroke-width="2"`
|
|
194
|
+
if (dots === 'all' || (k === lastIdx && !stacked)) {
|
|
195
|
+
out.push(`<circle cx="${fmt(p.px)}" cy="${fmt(p.py)}" r="4" ${dot}>${tip}</circle>`)
|
|
196
|
+
} else if (interactive && p.value !== null) {
|
|
197
|
+
out.push(
|
|
198
|
+
`<g class="tc-pt"><circle cx="${fmt(p.px)}" cy="${fmt(p.py)}" r="9" fill="transparent"/>` +
|
|
199
|
+
`<circle class="tc-dot" cx="${fmt(p.px)}" cy="${fmt(p.py)}" r="4" ${dot}/>${tip}</g>`
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
})
|
|
67
204
|
}
|
|
68
205
|
|
|
69
|
-
|
|
70
|
-
return
|
|
206
|
+
out.push('</svg>')
|
|
207
|
+
return out.join('')
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Renders a line chart as an SVG string.
|
|
212
|
+
* Extra options: id, legend (default: 2+ series), dots ('hover' | 'all' | 'none'),
|
|
213
|
+
* interactive (hover tooltips + dots, default true).
|
|
214
|
+
*/
|
|
215
|
+
export function lineChartSvg(series, options = {}) {
|
|
216
|
+
const id = options.id ?? nextId()
|
|
217
|
+
const plan = planLegend(options, seriesMeta(series))
|
|
218
|
+
const c = lineChart(series, plan.options)
|
|
219
|
+
return xyChartSvg(c, {
|
|
220
|
+
id,
|
|
221
|
+
kind: 'line',
|
|
222
|
+
label: options.label ?? 'Line chart',
|
|
223
|
+
fill: !!options.area,
|
|
224
|
+
dots: options.dots ?? 'hover',
|
|
225
|
+
formatX: options.formatX ?? ((v) => String(v)),
|
|
226
|
+
formatY: options.formatY ?? ((v) => String(v)),
|
|
227
|
+
layout: plan.layout,
|
|
228
|
+
interactive: options.interactive ?? true,
|
|
229
|
+
})
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Renders an area chart (optionally stacked) as an SVG string. Same extras as lineChartSvg. */
|
|
233
|
+
export function areaChartSvg(series, options = {}) {
|
|
234
|
+
const id = options.id ?? nextId()
|
|
235
|
+
const plan = planLegend(options, seriesMeta(series))
|
|
236
|
+
const c = areaChart(series, plan.options)
|
|
237
|
+
return xyChartSvg(c, {
|
|
238
|
+
id,
|
|
239
|
+
kind: 'area',
|
|
240
|
+
label: options.label ?? 'Area chart',
|
|
241
|
+
fill: true,
|
|
242
|
+
dots: options.dots ?? 'hover',
|
|
243
|
+
formatX: options.formatX ?? ((v) => String(v)),
|
|
244
|
+
formatY: options.formatY ?? ((v) => String(v)),
|
|
245
|
+
layout: plan.layout,
|
|
246
|
+
interactive: options.interactive ?? true,
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ---------------------------------------------------------------------------
|
|
251
|
+
// Bar
|
|
252
|
+
// ---------------------------------------------------------------------------
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Renders a bar chart as an SVG string.
|
|
256
|
+
* Extra options: id, legend, interactive, valueLabels (print values on bars, not stacked).
|
|
257
|
+
*/
|
|
258
|
+
export function barChartSvg(series, options = {}) {
|
|
259
|
+
const id = options.id ?? nextId()
|
|
260
|
+
const { label = 'Bar chart', valueLabels = false, interactive = true } = options
|
|
261
|
+
const plan = planLegend(options, seriesMeta(series))
|
|
262
|
+
const c = barChart(series, plan.options)
|
|
263
|
+
const { plot } = c
|
|
264
|
+
const horizontal = c.orientation === 'horizontal'
|
|
265
|
+
const out = [open(id, c.width, c.height, label, 'bar')]
|
|
266
|
+
|
|
267
|
+
if (interactive) {
|
|
268
|
+
out.push(
|
|
269
|
+
`<style>.${id} .tc-bar{opacity:.9;transition:opacity .15s}.${id} .tc-bar:hover{opacity:1}</style>`
|
|
270
|
+
)
|
|
271
|
+
}
|
|
272
|
+
if (plan.layout) out.push(legendMarkup(plan.layout, plot.x, 4))
|
|
273
|
+
|
|
274
|
+
out.push(gridlines(c.valueTicks, plot, !horizontal))
|
|
275
|
+
|
|
276
|
+
// baseline (zero line)
|
|
277
|
+
out.push(
|
|
278
|
+
horizontal
|
|
279
|
+
? `<line x1="${fmt(c.baseline)}" x2="${fmt(c.baseline)}" y1="${plot.y}" y2="${plot.y + plot.height}" stroke="currentColor" stroke-opacity="0.3"/>`
|
|
280
|
+
: `<line x1="${plot.x}" x2="${plot.x + plot.width}" y1="${fmt(c.baseline)}" y2="${fmt(c.baseline)}" stroke="currentColor" stroke-opacity="0.3"/>`
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
// category labels (skip some when they would collide)
|
|
284
|
+
const step = c.categoryScale.step()
|
|
285
|
+
const longest = Math.max(1, ...c.categoryTicks.map((t) => t.label.length))
|
|
286
|
+
const skip = horizontal ? 1 : Math.max(1, Math.ceil((longest * CHAR_W + 6) / (step || 1)))
|
|
287
|
+
c.categoryTicks.forEach((t, i) => {
|
|
288
|
+
if (i % skip !== 0) return
|
|
289
|
+
out.push(
|
|
290
|
+
horizontal
|
|
291
|
+
? `<text x="${plot.x - 8}" y="${fmt(t.pos)}" text-anchor="end" dominant-baseline="middle" ${MUTED}>${escapeXml(t.label)}</text>`
|
|
292
|
+
: `<text x="${fmt(t.pos)}" y="${plot.y + plot.height + 16}" text-anchor="middle" ${MUTED}>${escapeXml(t.label)}</text>`
|
|
293
|
+
)
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
const single = c.series.length === 1
|
|
297
|
+
for (const s of c.series) {
|
|
298
|
+
for (const b of s.bars) {
|
|
299
|
+
if (!b.path) continue
|
|
300
|
+
const tip = single ? `${b.categoryLabel}: ${b.label}` : `${b.categoryLabel} · ${s.name}: ${b.label}`
|
|
301
|
+
out.push(
|
|
302
|
+
`<path class="tc-bar" d="${b.path}" fill="${escapeXml(s.color)}"><title>${escapeXml(tip)}</title></path>`
|
|
303
|
+
)
|
|
304
|
+
if (valueLabels && !c.stacked) {
|
|
305
|
+
const neg = b.value < 0
|
|
306
|
+
out.push(
|
|
307
|
+
horizontal
|
|
308
|
+
? `<text x="${fmt(neg ? b.x - 6 : b.x + b.width + 6)}" y="${fmt(b.y + b.height / 2)}" text-anchor="${neg ? 'end' : 'start'}" dominant-baseline="middle" fill="currentColor" fill-opacity="0.8">${escapeXml(b.label)}</text>`
|
|
309
|
+
: `<text x="${fmt(b.x + b.width / 2)}" y="${fmt(neg ? b.y + b.height + 12 : b.y - 5)}" text-anchor="middle" fill="currentColor" fill-opacity="0.8">${escapeXml(b.label)}</text>`
|
|
310
|
+
)
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
out.push('</svg>')
|
|
316
|
+
return out.join('')
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ---------------------------------------------------------------------------
|
|
320
|
+
// Pie / donut
|
|
321
|
+
// ---------------------------------------------------------------------------
|
|
322
|
+
|
|
323
|
+
function pieSvg(data, options, defaultInner, kind) {
|
|
324
|
+
const id = options.id ?? nextId()
|
|
325
|
+
const size = options.size ?? 220
|
|
326
|
+
const legendMode = options.legend === undefined ? 'right' : options.legend
|
|
327
|
+
const interactive = options.interactive ?? true
|
|
328
|
+
const label = options.label ?? (kind === 'donut' ? 'Donut chart' : 'Pie chart')
|
|
329
|
+
|
|
330
|
+
const g = pieChart(data, {
|
|
331
|
+
innerRadius: defaultInner,
|
|
332
|
+
...options,
|
|
333
|
+
width: size,
|
|
334
|
+
height: size,
|
|
335
|
+
})
|
|
336
|
+
const items = g.slices.map((s) => ({ name: s.label, color: s.color, percent: s.percentLabel }))
|
|
337
|
+
|
|
338
|
+
let W = size
|
|
339
|
+
let H = size
|
|
340
|
+
let chartX = 0
|
|
341
|
+
let chartY = 0
|
|
342
|
+
let legend = ''
|
|
343
|
+
|
|
344
|
+
if (legendMode === 'right' && items.length) {
|
|
345
|
+
const rowH = 24
|
|
346
|
+
const longest = Math.max(...items.map((i) => String(i.name).length))
|
|
347
|
+
const lw = Math.min(220, longest * CHAR_W + 16 + 44)
|
|
348
|
+
W = size + 20 + lw
|
|
349
|
+
H = Math.max(size, items.length * rowH)
|
|
350
|
+
chartY = (H - size) / 2
|
|
351
|
+
const y0 = (H - items.length * rowH) / 2
|
|
352
|
+
const x0 = size + 20
|
|
353
|
+
items.forEach((it, i) => {
|
|
354
|
+
const cy = y0 + i * rowH + rowH / 2
|
|
355
|
+
legend +=
|
|
356
|
+
`<rect x="${x0}" y="${fmt(cy - 5)}" width="10" height="10" rx="3" fill="${escapeXml(it.color)}"/>` +
|
|
357
|
+
`<text x="${x0 + 16}" y="${fmt(cy)}" dominant-baseline="middle" fill="currentColor" fill-opacity="0.85">${escapeXml(it.name)}</text>` +
|
|
358
|
+
`<text x="${x0 + lw}" y="${fmt(cy)}" text-anchor="end" dominant-baseline="middle" ${MUTED}>${escapeXml(it.percent)}</text>`
|
|
359
|
+
})
|
|
360
|
+
} else if (legendMode === 'bottom' && items.length) {
|
|
361
|
+
const layout = legendLayout(items, size)
|
|
362
|
+
H = size + 8 + layout.height
|
|
363
|
+
const rowW = (row) => row[row.length - 1].x + row[row.length - 1].w - 14
|
|
364
|
+
layout.rows.forEach((row, r) => {
|
|
365
|
+
const x0 = (W - rowW(row)) / 2
|
|
366
|
+
legend += legendMarkup({ rows: [row.map((it) => ({ ...it }))], height: 18 }, x0, size + 8 + r * 18)
|
|
367
|
+
})
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const out = [open(id, fmt(W), fmt(H), label, kind)]
|
|
371
|
+
if (interactive) {
|
|
372
|
+
out.push(
|
|
373
|
+
`<style>.${id} .tc-slice{transition:opacity .15s}.${id}:hover .tc-slice{opacity:.45}.${id} .tc-slice:hover{opacity:1}</style>`
|
|
374
|
+
)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
out.push(`<g transform="translate(${chartX},${fmt(chartY)})">`)
|
|
378
|
+
if (g.total === 0) {
|
|
379
|
+
out.push(
|
|
380
|
+
`<circle cx="${fmt(g.cx)}" cy="${fmt(g.cy)}" r="${fmt((g.outerRadius + g.innerRadius) / 2)}" fill="none" stroke="currentColor" stroke-opacity="0.1" stroke-width="${fmt(Math.max(2, g.outerRadius - g.innerRadius))}"/>`
|
|
381
|
+
)
|
|
382
|
+
}
|
|
383
|
+
for (const s of g.slices) {
|
|
384
|
+
if (!s.path) continue
|
|
385
|
+
out.push(
|
|
386
|
+
`<path class="tc-slice" d="${s.path}" fill="${escapeXml(s.color)}" fill-rule="evenodd"><title>${escapeXml(`${s.label}: ${s.valueLabel} (${s.percentLabel})`)}</title></path>`
|
|
387
|
+
)
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// center text (donut only)
|
|
391
|
+
const hole = g.innerRadius
|
|
392
|
+
if (hole >= 34 && options.centerLabel !== false) {
|
|
393
|
+
const big = options.centerLabel ?? (options.formatValue ?? String)(g.total)
|
|
394
|
+
const sub = options.centerSubLabel === undefined ? 'Total' : options.centerSubLabel
|
|
395
|
+
const fs = Math.max(14, Math.min(28, hole * 0.5))
|
|
396
|
+
out.push(
|
|
397
|
+
`<text x="${fmt(g.cx)}" y="${fmt(g.cy - (sub ? 4 : 0))}" text-anchor="middle" dominant-baseline="${sub ? 'auto' : 'middle'}" font-size="${fmt(fs)}" font-weight="600" fill="currentColor">${escapeXml(big)}</text>`
|
|
398
|
+
)
|
|
399
|
+
if (sub) {
|
|
400
|
+
out.push(
|
|
401
|
+
`<text x="${fmt(g.cx)}" y="${fmt(g.cy + 6)}" text-anchor="middle" dominant-baseline="hanging" ${MUTED}>${escapeXml(sub)}</text>`
|
|
402
|
+
)
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
out.push('</g>')
|
|
406
|
+
out.push(legend)
|
|
407
|
+
out.push('</svg>')
|
|
408
|
+
return out.join('')
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Renders a pie chart as an SVG string.
|
|
413
|
+
* options: size (px, default 220), legend ('right' | 'bottom' | false), id, interactive,
|
|
414
|
+
* plus every pieChart() option (cornerRadius, gap, startAngle, sort, innerRadius …).
|
|
415
|
+
*/
|
|
416
|
+
export function pieChartSvg(data, options = {}) {
|
|
417
|
+
return pieSvg(data, options, 0, 'pie')
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Renders a donut chart as an SVG string. Adds centerLabel / centerSubLabel
|
|
422
|
+
* (defaults: the total and "Total"; pass false to hide).
|
|
423
|
+
*/
|
|
424
|
+
export function donutChartSvg(data, options = {}) {
|
|
425
|
+
return pieSvg(data, options, 0.62, 'donut')
|
|
71
426
|
}
|