@tylix/charts 0.0.0-stage → 0.5.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 +35 -2
- package/package.json +17 -3
- package/src/index.d.ts +97 -0
- package/src/index.js +6 -0
- package/src/line.js +111 -0
- package/src/path.js +68 -0
- package/src/scales.js +33 -0
- package/src/sparkline.js +58 -0
- package/src/svg.js +71 -0
- package/src/ticks.js +47 -0
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tylix/charts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Hand-rolled SVG charts for Tylix. No third-party dependencies.
|
|
4
|
+
|
|
5
|
+
## What's included
|
|
6
|
+
|
|
7
|
+
- `sparkline()` and `lineChart()` return plain geometry (paths, ticks,
|
|
8
|
+
plot area) so a `.tyx` template can render them with `{{#each}}`
|
|
9
|
+
- `sparklineSvg()` and `lineChartSvg()` return finished SVG strings
|
|
10
|
+
- Building blocks: `linearScale`, `niceTicks`, `extent`, `linePath`, `areaPath`
|
|
11
|
+
- Gap support (`null` values break the line), `linear` and `step` curves,
|
|
12
|
+
optional area fill, multiple series
|
|
13
|
+
- TypeScript declarations (`index.d.ts`)
|
|
14
|
+
- Themeable colors via `--tylix-chart-1` through `--tylix-chart-5`;
|
|
15
|
+
sparklines inherit `currentColor`
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { sparkline, lineChart } from '@tylix/charts'
|
|
21
|
+
|
|
22
|
+
const s = sparkline([12, 18, 15, 30, 22], { width: 120, height: 32 })
|
|
23
|
+
// s.path, s.areaPath, s.last
|
|
24
|
+
|
|
25
|
+
const c = lineChart(
|
|
26
|
+
[{ name: 'CPU', data: [10, 40, 30, 80] }],
|
|
27
|
+
{ width: 480, height: 240, formatY: (v) => `${v}%` }
|
|
28
|
+
)
|
|
29
|
+
// c.plot, c.xTicks, c.yTicks, c.series[0].path
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Run the tests with `node --test`.
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tylix/charts",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Hand-rolled SVG charts for Tylix: sparkline and line chart",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"default": "./src/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["src"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "node --test"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"publishConfig": { "access": "public" }
|
|
6
20
|
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
export interface LinearScale {
|
|
2
|
+
(value: number): number
|
|
3
|
+
invert(px: number): number
|
|
4
|
+
domain(): [number, number]
|
|
5
|
+
range(): [number, number]
|
|
6
|
+
ticks(count?: number): number[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function linearScale(options?: {
|
|
10
|
+
domain?: [number, number]
|
|
11
|
+
range?: [number, number]
|
|
12
|
+
clamp?: boolean
|
|
13
|
+
}): LinearScale
|
|
14
|
+
|
|
15
|
+
export function niceTicks(min: number, max: number, count?: number): number[]
|
|
16
|
+
export function extent(values: Iterable<unknown>): [number, number] | null
|
|
17
|
+
|
|
18
|
+
export interface PixelPoint {
|
|
19
|
+
x: number | null
|
|
20
|
+
y: number | null
|
|
21
|
+
}
|
|
22
|
+
export type Curve = 'linear' | 'step'
|
|
23
|
+
|
|
24
|
+
export function segments<T extends PixelPoint>(points: Array<T | null>): T[][]
|
|
25
|
+
export function linePath(points: Array<PixelPoint | null>, options?: { curve?: Curve }): string
|
|
26
|
+
export function areaPath(
|
|
27
|
+
points: Array<PixelPoint | null>,
|
|
28
|
+
baselineY: number,
|
|
29
|
+
options?: { curve?: Curve }
|
|
30
|
+
): string
|
|
31
|
+
|
|
32
|
+
export interface SparklineOptions {
|
|
33
|
+
width?: number
|
|
34
|
+
height?: number
|
|
35
|
+
padding?: number
|
|
36
|
+
curve?: Curve
|
|
37
|
+
min?: number
|
|
38
|
+
max?: number
|
|
39
|
+
showLastPoint?: boolean
|
|
40
|
+
label?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface SparklineGeometry {
|
|
44
|
+
width: number
|
|
45
|
+
height: number
|
|
46
|
+
path: string
|
|
47
|
+
areaPath: string
|
|
48
|
+
last: { x: number; y: number; value: number; index: number } | null
|
|
49
|
+
min: number | null
|
|
50
|
+
max: number | null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function sparkline(data?: Array<number | null>, options?: SparklineOptions): SparklineGeometry
|
|
54
|
+
export function sparklineSvg(data?: Array<number | null>, options?: SparklineOptions): string
|
|
55
|
+
|
|
56
|
+
export interface LineSeriesInput {
|
|
57
|
+
name?: string
|
|
58
|
+
color?: string
|
|
59
|
+
data: Array<number | null | { x?: number; y: number | null }>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface LineChartOptions {
|
|
63
|
+
width?: number
|
|
64
|
+
height?: number
|
|
65
|
+
margin?: Partial<{ top: number; right: number; bottom: number; left: number }>
|
|
66
|
+
xDomain?: [number, number]
|
|
67
|
+
yDomain?: [number, number]
|
|
68
|
+
xTicks?: number
|
|
69
|
+
yTicks?: number
|
|
70
|
+
curve?: Curve
|
|
71
|
+
area?: boolean
|
|
72
|
+
zeroBaseline?: boolean
|
|
73
|
+
formatX?: (value: number) => string
|
|
74
|
+
formatY?: (value: number) => string
|
|
75
|
+
label?: string
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface LineChartGeometry {
|
|
79
|
+
width: number
|
|
80
|
+
height: number
|
|
81
|
+
plot: { x: number; y: number; width: number; height: number }
|
|
82
|
+
xScale: LinearScale
|
|
83
|
+
yScale: LinearScale
|
|
84
|
+
xTicks: Array<{ value: number; x: number; label: string }>
|
|
85
|
+
yTicks: Array<{ value: number; y: number; label: string }>
|
|
86
|
+
series: Array<{
|
|
87
|
+
name: string
|
|
88
|
+
color: string
|
|
89
|
+
points: Array<{ value: number | null; x: number; px: number; py: number | null }>
|
|
90
|
+
path: string
|
|
91
|
+
areaPath: string
|
|
92
|
+
}>
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export const DEFAULT_PALETTE: string[]
|
|
96
|
+
export function lineChart(series?: LineSeriesInput[], options?: LineChartOptions): LineChartGeometry
|
|
97
|
+
export function lineChartSvg(series?: LineSeriesInput[], options?: LineChartOptions): string
|
package/src/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { linearScale } from './scales.js'
|
|
2
|
+
export { niceTicks, extent } from './ticks.js'
|
|
3
|
+
export { linePath, areaPath, segments } from './path.js'
|
|
4
|
+
export { sparkline } from './sparkline.js'
|
|
5
|
+
export { lineChart, DEFAULT_PALETTE } from './line.js'
|
|
6
|
+
export { sparklineSvg, lineChartSvg } from './svg.js'
|
package/src/line.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { linearScale } from './scales.js'
|
|
2
|
+
import { linePath, areaPath } from './path.js'
|
|
3
|
+
import { extent } from './ticks.js'
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_PALETTE = [
|
|
6
|
+
'var(--tylix-chart-1, #2563eb)',
|
|
7
|
+
'var(--tylix-chart-2, #16a34a)',
|
|
8
|
+
'var(--tylix-chart-3, #f59e0b)',
|
|
9
|
+
'var(--tylix-chart-4, #dc2626)',
|
|
10
|
+
'var(--tylix-chart-5, #7c3aed)',
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
// Accepts [1, 2, 3] (x = index) or [{ x, y }, ...]
|
|
14
|
+
function normalizeData(data = []) {
|
|
15
|
+
return data.map((d, i) => {
|
|
16
|
+
if (typeof d === 'number') return { x: i, y: d }
|
|
17
|
+
if (d == null) return { x: i, y: null }
|
|
18
|
+
return { x: d.x ?? i, y: d.y ?? null }
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Computes line chart geometry. Returns plain data so a .tyx template can
|
|
24
|
+
* render it with {{#each}}, or pass it to lineChartSvg() for a string.
|
|
25
|
+
*
|
|
26
|
+
* series: [{ name, color?, data: number[] | { x, y }[] }]
|
|
27
|
+
*/
|
|
28
|
+
export function lineChart(
|
|
29
|
+
series = [],
|
|
30
|
+
{
|
|
31
|
+
width = 480,
|
|
32
|
+
height = 240,
|
|
33
|
+
margin = {},
|
|
34
|
+
xDomain,
|
|
35
|
+
yDomain,
|
|
36
|
+
xTicks = 6,
|
|
37
|
+
yTicks = 5,
|
|
38
|
+
curve = 'linear',
|
|
39
|
+
area = false,
|
|
40
|
+
zeroBaseline = false,
|
|
41
|
+
formatX = (v) => String(v),
|
|
42
|
+
formatY = (v) => String(v),
|
|
43
|
+
} = {}
|
|
44
|
+
) {
|
|
45
|
+
const m = { top: 12, right: 12, bottom: 28, left: 40, ...margin }
|
|
46
|
+
const plot = {
|
|
47
|
+
x: m.left,
|
|
48
|
+
y: m.top,
|
|
49
|
+
width: Math.max(0, width - m.left - m.right),
|
|
50
|
+
height: Math.max(0, height - m.top - m.bottom),
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const normalized = series.map((s, i) => ({
|
|
54
|
+
name: s.name ?? `Series ${i + 1}`,
|
|
55
|
+
color: s.color ?? DEFAULT_PALETTE[i % DEFAULT_PALETTE.length],
|
|
56
|
+
data: normalizeData(s.data),
|
|
57
|
+
}))
|
|
58
|
+
|
|
59
|
+
const allPoints = normalized.flatMap((s) => s.data)
|
|
60
|
+
const xExt = extent(allPoints.map((p) => p.x)) ?? [0, 1]
|
|
61
|
+
const yExt = extent(allPoints.map((p) => p.y)) ?? [0, 1]
|
|
62
|
+
|
|
63
|
+
const xd = xDomain ?? xExt
|
|
64
|
+
let yd = yDomain ?? yExt
|
|
65
|
+
if (!yDomain) {
|
|
66
|
+
if (zeroBaseline) yd = [Math.min(0, yd[0]), Math.max(0, yd[1])]
|
|
67
|
+
// Extend to nice tick bounds so gridlines frame the data.
|
|
68
|
+
const ticks = linearScale({ domain: yd }).ticks(yTicks)
|
|
69
|
+
if (ticks.length > 1) yd = [ticks[0], ticks[ticks.length - 1]]
|
|
70
|
+
else if (yd[0] === yd[1]) yd = [yd[0] - 1, yd[1] + 1]
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const x = linearScale({ domain: xd, range: [plot.x, plot.x + plot.width] })
|
|
74
|
+
const y = linearScale({ domain: yd, range: [plot.y + plot.height, plot.y] })
|
|
75
|
+
// Fill down to zero when it is inside the domain, otherwise to the bottom edge.
|
|
76
|
+
const baselineY = yd[0] <= 0 && yd[1] >= 0 ? y(0) : y(yd[0])
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
width,
|
|
80
|
+
height,
|
|
81
|
+
plot,
|
|
82
|
+
xScale: x,
|
|
83
|
+
yScale: y,
|
|
84
|
+
xTicks: x.ticks(xTicks).filter((v) => v >= xd[0] && v <= xd[1]).map((v) => ({
|
|
85
|
+
value: v,
|
|
86
|
+
x: x(v),
|
|
87
|
+
label: formatX(v),
|
|
88
|
+
})),
|
|
89
|
+
yTicks: y.ticks(yTicks).filter((v) => v >= yd[0] && v <= yd[1]).map((v) => ({
|
|
90
|
+
value: v,
|
|
91
|
+
y: y(v),
|
|
92
|
+
label: formatY(v),
|
|
93
|
+
})),
|
|
94
|
+
series: normalized.map((s) => {
|
|
95
|
+
const points = s.data.map((p) => ({
|
|
96
|
+
value: p.y,
|
|
97
|
+
x: p.x,
|
|
98
|
+
px: x(p.x),
|
|
99
|
+
py: p.y === null ? null : y(p.y),
|
|
100
|
+
}))
|
|
101
|
+
const px = points.map((p) => ({ x: p.px, y: p.py }))
|
|
102
|
+
return {
|
|
103
|
+
name: s.name,
|
|
104
|
+
color: s.color,
|
|
105
|
+
points,
|
|
106
|
+
path: linePath(px, { curve }),
|
|
107
|
+
areaPath: area ? areaPath(px, baselineY, { curve }) : '',
|
|
108
|
+
}
|
|
109
|
+
}),
|
|
110
|
+
}
|
|
111
|
+
}
|
package/src/path.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// SVG path-data builders. Points are { x, y } in pixel space.
|
|
2
|
+
// A point with a null / non-finite x or y breaks the line (a gap).
|
|
3
|
+
|
|
4
|
+
const fmt = (n) => Math.round(n * 100) / 100
|
|
5
|
+
|
|
6
|
+
const isValid = (p) =>
|
|
7
|
+
p != null &&
|
|
8
|
+
typeof p.x === 'number' &&
|
|
9
|
+
typeof p.y === 'number' &&
|
|
10
|
+
Number.isFinite(p.x) &&
|
|
11
|
+
Number.isFinite(p.y)
|
|
12
|
+
|
|
13
|
+
/** Splits points into runs of consecutive valid points. */
|
|
14
|
+
export function segments(points) {
|
|
15
|
+
const runs = []
|
|
16
|
+
let current = []
|
|
17
|
+
for (const p of points) {
|
|
18
|
+
if (isValid(p)) {
|
|
19
|
+
current.push(p)
|
|
20
|
+
} else if (current.length) {
|
|
21
|
+
runs.push(current)
|
|
22
|
+
current = []
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (current.length) runs.push(current)
|
|
26
|
+
return runs
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function runPath(run, curve) {
|
|
30
|
+
let d = `M${fmt(run[0].x)},${fmt(run[0].y)}`
|
|
31
|
+
for (let i = 1; i < run.length; i++) {
|
|
32
|
+
const p = run[i]
|
|
33
|
+
d +=
|
|
34
|
+
curve === 'step'
|
|
35
|
+
? `H${fmt(p.x)}V${fmt(p.y)}`
|
|
36
|
+
: `L${fmt(p.x)},${fmt(p.y)}`
|
|
37
|
+
}
|
|
38
|
+
return d
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Line path. curve: 'linear' (default) | 'step'.
|
|
43
|
+
* A lone point renders as a zero-length segment so it stays visible with round caps.
|
|
44
|
+
*/
|
|
45
|
+
export function linePath(points, { curve = 'linear' } = {}) {
|
|
46
|
+
return segments(points)
|
|
47
|
+
.map((run) =>
|
|
48
|
+
run.length === 1
|
|
49
|
+
? `M${fmt(run[0].x)},${fmt(run[0].y)}h0`
|
|
50
|
+
: runPath(run, curve)
|
|
51
|
+
)
|
|
52
|
+
.join('')
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Closed area path between the line and a horizontal baseline (pixel y). */
|
|
56
|
+
export function areaPath(points, baselineY, { curve = 'linear' } = {}) {
|
|
57
|
+
return segments(points)
|
|
58
|
+
.filter((run) => run.length > 1)
|
|
59
|
+
.map((run) => {
|
|
60
|
+
const first = run[0]
|
|
61
|
+
const last = run[run.length - 1]
|
|
62
|
+
return (
|
|
63
|
+
runPath(run, curve) +
|
|
64
|
+
`L${fmt(last.x)},${fmt(baselineY)}L${fmt(first.x)},${fmt(baselineY)}Z`
|
|
65
|
+
)
|
|
66
|
+
})
|
|
67
|
+
.join('')
|
|
68
|
+
}
|
package/src/scales.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { niceTicks } from './ticks.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Linear scale mapping a numeric domain to a numeric range.
|
|
5
|
+
*
|
|
6
|
+
* const y = linearScale({ domain: [0, 100], range: [200, 0] })
|
|
7
|
+
* y(50) // 100
|
|
8
|
+
* y.invert(100) // 50
|
|
9
|
+
* y.ticks(5) // [0, 20, 40, 60, 80, 100]
|
|
10
|
+
*/
|
|
11
|
+
export function linearScale({ domain = [0, 1], range = [0, 1], clamp = false } = {}) {
|
|
12
|
+
const [d0, d1] = domain
|
|
13
|
+
const [r0, r1] = range
|
|
14
|
+
const span = d1 - d0
|
|
15
|
+
|
|
16
|
+
const scale = (value) => {
|
|
17
|
+
let t = span === 0 ? 0.5 : (value - d0) / span
|
|
18
|
+
if (clamp) t = Math.min(1, Math.max(0, t))
|
|
19
|
+
return r0 + t * (r1 - r0)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
scale.invert = (px) => {
|
|
23
|
+
const rangeSpan = r1 - r0
|
|
24
|
+
const t = rangeSpan === 0 ? 0 : (px - r0) / rangeSpan
|
|
25
|
+
return d0 + t * span
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
scale.domain = () => [d0, d1]
|
|
29
|
+
scale.range = () => [r0, r1]
|
|
30
|
+
scale.ticks = (count = 5) => niceTicks(d0, d1, count)
|
|
31
|
+
|
|
32
|
+
return scale
|
|
33
|
+
}
|
package/src/sparkline.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { linearScale } from './scales.js'
|
|
2
|
+
import { linePath, areaPath } from './path.js'
|
|
3
|
+
import { extent } from './ticks.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Computes sparkline geometry from an array of numbers (or null for gaps).
|
|
7
|
+
* Returns plain data so a .tyx template can render it:
|
|
8
|
+
*
|
|
9
|
+
* <svg width="{{ s.width }}" height="{{ s.height }}">
|
|
10
|
+
* <path d="{{ s.areaPath }}" />
|
|
11
|
+
* <path d="{{ s.path }}" />
|
|
12
|
+
* </svg>
|
|
13
|
+
*/
|
|
14
|
+
export function sparkline(
|
|
15
|
+
data = [],
|
|
16
|
+
{ width = 120, height = 32, padding = 2, curve = 'linear', min, max } = {}
|
|
17
|
+
) {
|
|
18
|
+
const values = data.map((v) => (typeof v === 'number' ? v : null))
|
|
19
|
+
const ext = extent(values)
|
|
20
|
+
|
|
21
|
+
if (!ext) {
|
|
22
|
+
return { width, height, path: '', areaPath: '', last: null, min: null, max: null }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const lo = min ?? ext[0]
|
|
26
|
+
const hi = max ?? ext[1]
|
|
27
|
+
// A flat series would collapse to a line on the edge, so pad the domain.
|
|
28
|
+
const flat = lo === hi
|
|
29
|
+
const domain = flat ? [lo - 1, hi + 1] : [lo, hi]
|
|
30
|
+
|
|
31
|
+
const x = linearScale({
|
|
32
|
+
domain: [0, Math.max(values.length - 1, 1)],
|
|
33
|
+
range: [padding, width - padding],
|
|
34
|
+
})
|
|
35
|
+
const y = linearScale({ domain, range: [height - padding, padding] })
|
|
36
|
+
|
|
37
|
+
const points = values.map((v, i) =>
|
|
38
|
+
v === null ? null : { x: x(i), y: y(v), value: v, index: i }
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
let last = null
|
|
42
|
+
for (let i = points.length - 1; i >= 0; i--) {
|
|
43
|
+
if (points[i]) {
|
|
44
|
+
last = points[i]
|
|
45
|
+
break
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
width,
|
|
51
|
+
height,
|
|
52
|
+
path: linePath(points, { curve }),
|
|
53
|
+
areaPath: areaPath(points, height - padding, { curve }),
|
|
54
|
+
last,
|
|
55
|
+
min: ext[0],
|
|
56
|
+
max: ext[1],
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/svg.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { sparkline } from './sparkline.js'
|
|
2
|
+
import { lineChart } from './line.js'
|
|
3
|
+
|
|
4
|
+
const escapeXml = (s) =>
|
|
5
|
+
String(s)
|
|
6
|
+
.replace(/&/g, '&')
|
|
7
|
+
.replace(/</g, '<')
|
|
8
|
+
.replace(/>/g, '>')
|
|
9
|
+
.replace(/"/g, '"')
|
|
10
|
+
|
|
11
|
+
/** Renders a sparkline as an SVG string. Colors inherit via currentColor. */
|
|
12
|
+
export function sparklineSvg(data, options = {}) {
|
|
13
|
+
const s = sparkline(data, options)
|
|
14
|
+
const { showLastPoint = true, label = 'Sparkline' } = options
|
|
15
|
+
const parts = [
|
|
16
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="${s.width}" height="${s.height}" viewBox="0 0 ${s.width} ${s.height}" role="img" aria-label="${escapeXml(label)}">`,
|
|
17
|
+
]
|
|
18
|
+
if (s.areaPath) {
|
|
19
|
+
parts.push(`<path d="${s.areaPath}" fill="currentColor" fill-opacity="0.12" stroke="none"/>`)
|
|
20
|
+
}
|
|
21
|
+
if (s.path) {
|
|
22
|
+
parts.push(
|
|
23
|
+
`<path d="${s.path}" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>`
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
if (showLastPoint && s.last) {
|
|
27
|
+
parts.push(`<circle cx="${s.last.x}" cy="${s.last.y}" r="2" fill="currentColor"/>`)
|
|
28
|
+
}
|
|
29
|
+
parts.push('</svg>')
|
|
30
|
+
return parts.join('')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Renders a line chart (axes, gridlines, series) as an SVG string. */
|
|
34
|
+
export function lineChartSvg(series, options = {}) {
|
|
35
|
+
const c = lineChart(series, options)
|
|
36
|
+
const { label = 'Line chart' } = options
|
|
37
|
+
const { plot } = c
|
|
38
|
+
const parts = [
|
|
39
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="${c.width}" height="${c.height}" viewBox="0 0 ${c.width} ${c.height}" role="img" aria-label="${escapeXml(label)}" font-family="system-ui, sans-serif" font-size="11">`,
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
// Gridlines + y labels
|
|
43
|
+
for (const t of c.yTicks) {
|
|
44
|
+
parts.push(
|
|
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>`
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// x labels
|
|
51
|
+
for (const t of c.xTicks) {
|
|
52
|
+
parts.push(
|
|
53
|
+
`<text x="${t.x}" y="${plot.y + plot.height + 16}" text-anchor="middle" fill="currentColor" fill-opacity="0.65">${escapeXml(t.label)}</text>`
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Series
|
|
58
|
+
for (const s of c.series) {
|
|
59
|
+
if (s.areaPath) {
|
|
60
|
+
parts.push(`<path d="${s.areaPath}" fill="${s.color}" fill-opacity="0.12" stroke="none"/>`)
|
|
61
|
+
}
|
|
62
|
+
if (s.path) {
|
|
63
|
+
parts.push(
|
|
64
|
+
`<path d="${s.path}" fill="none" stroke="${s.color}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>`
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
parts.push('</svg>')
|
|
70
|
+
return parts.join('')
|
|
71
|
+
}
|
package/src/ticks.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Nice-number tick generation (Heckbert's algorithm).
|
|
2
|
+
|
|
3
|
+
function niceNumber(range, round) {
|
|
4
|
+
const exp = Math.floor(Math.log10(range))
|
|
5
|
+
const f = range / Math.pow(10, exp)
|
|
6
|
+
let nf
|
|
7
|
+
if (round) {
|
|
8
|
+
nf = f < 1.5 ? 1 : f < 3 ? 2 : f < 7 ? 5 : 10
|
|
9
|
+
} else {
|
|
10
|
+
nf = f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10
|
|
11
|
+
}
|
|
12
|
+
return nf * Math.pow(10, exp)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns evenly spaced "nice" tick values covering [min, max].
|
|
17
|
+
* The first tick is <= min and the last tick is >= max.
|
|
18
|
+
*/
|
|
19
|
+
export function niceTicks(min, max, count = 5) {
|
|
20
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) return []
|
|
21
|
+
if (min === max) return [min]
|
|
22
|
+
if (min > max) [min, max] = [max, min]
|
|
23
|
+
|
|
24
|
+
const range = niceNumber(max - min, false)
|
|
25
|
+
const step = niceNumber(range / (Math.max(count, 2) - 1), true)
|
|
26
|
+
const start = Math.floor(min / step) * step
|
|
27
|
+
const end = Math.ceil(max / step) * step
|
|
28
|
+
const decimals = Math.min(10, Math.max(0, -Math.floor(Math.log10(step))))
|
|
29
|
+
|
|
30
|
+
const ticks = []
|
|
31
|
+
for (let v = start; v <= end + step / 2; v += step) {
|
|
32
|
+
ticks.push(Number(v.toFixed(decimals)))
|
|
33
|
+
}
|
|
34
|
+
return ticks
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Returns [min, max] of the finite numbers in `values`, or null if none. */
|
|
38
|
+
export function extent(values) {
|
|
39
|
+
let min = Infinity
|
|
40
|
+
let max = -Infinity
|
|
41
|
+
for (const v of values) {
|
|
42
|
+
if (typeof v !== 'number' || !Number.isFinite(v)) continue
|
|
43
|
+
if (v < min) min = v
|
|
44
|
+
if (v > max) max = v
|
|
45
|
+
}
|
|
46
|
+
return min === Infinity ? null : [min, max]
|
|
47
|
+
}
|