@octane-xplat/charts 0.19.0 → 0.20.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.
@@ -6,167 +6,180 @@ import type { ChartType, SeriesSpec } from '../props'
6
6
  export type XScaleKind = 'band' | 'point' | 'linear' | 'time'
7
7
 
8
8
  export type XScale =
9
- | ScaleBand<string>
10
- | ScalePoint<string>
11
- | ScaleLinear<number, number>
12
- | ScaleTime<number, number>
9
+ | ScaleBand<string>
10
+ | ScalePoint<string>
11
+ | ScaleLinear<number, number>
12
+ | ScaleTime<number, number>
13
13
 
14
14
  export type YScale = ScaleLinear<number, number>
15
15
 
16
16
  export interface PlotBox {
17
- x: number
18
- y: number
19
- width: number
20
- height: number
17
+ x: number
18
+ y: number
19
+ width: number
20
+ height: number
21
21
  }
22
22
 
23
23
  export interface ResolvedSeries {
24
- name: string
25
- color: string
26
- values: { x: number | string | Date; y: number; color?: string }[]
24
+ name: string
25
+ color: string
26
+ values: { x: number | string | Date; y: number; color?: string }[]
27
27
  }
28
28
 
29
29
  /** Categorical x values across all series, first-seen order — the band/point
30
30
  * domain. */
31
31
  export function categoryDomain(data: SeriesSpec[]): string[] {
32
- const seen = new Set<string>()
33
- const out: string[] = []
34
- for (const series of data) {
35
- for (const d of series.values) {
36
- const key = String(d.x)
37
- if (!seen.has(key)) {
38
- seen.add(key)
39
- out.push(key)
40
- }
41
- }
42
- }
43
-
44
- return out
32
+ const seen = new Set<string>()
33
+ const out: string[] = []
34
+
35
+ for (const series of data) {
36
+ for (const d of series.values) {
37
+ const key = String(d.x)
38
+
39
+ if (!seen.has(key)) {
40
+ seen.add(key)
41
+ out.push(key)
42
+ }
43
+ }
44
+ }
45
+
46
+ return out
45
47
  }
46
48
 
47
49
  /** A categorical x lookup — band/point domains key on String(x). */
48
50
  export function categoryKey(x: number | string | Date): string {
49
- return String(x)
51
+ return String(x)
50
52
  }
51
53
 
52
54
  /** Pick the x scale family: bar is always band, other cartesian types infer
53
55
  * from the first datum — Date → time, string → point, number → linear. */
54
56
  export function inferXKind(type: ChartType, data: SeriesSpec[]): XScaleKind {
55
- if (type === 'bar') {
56
- return 'band'
57
- }
57
+ if (type === 'bar') {
58
+ return 'band'
59
+ }
60
+
61
+ const first = data[0]?.values[0]?.x
58
62
 
59
- const first = data[0]?.values[0]?.x
60
- if (first instanceof Date) {
61
- return 'time'
62
- }
63
+ if (first instanceof Date) {
64
+ return 'time'
65
+ }
63
66
 
64
- if (typeof first === 'string') {
65
- return 'point'
66
- }
67
+ if (typeof first === 'string') {
68
+ return 'point'
69
+ }
67
70
 
68
- return 'linear'
71
+ return 'linear'
69
72
  }
70
73
 
71
74
  export function buildXScale(kind: XScaleKind, data: SeriesSpec[], width: number): XScale {
72
- switch (kind) {
73
- case 'band':
74
- return scaleBand<string>()
75
- .domain(categoryDomain(data))
76
- .range([0, width])
77
- .paddingInner(0.25)
78
- .paddingOuter(0.15)
79
- case 'point': {
80
- const domain = categoryDomain(data)
81
- return scalePoint<string>()
82
- .domain(domain)
83
- .range([0, width])
84
- .padding(domain.length <= 1 ? 0 : 0.5)
85
- }
86
- case 'time': {
87
- const values = data.flatMap((s) => s.values.map((d) => d.x as Date))
88
- const [lo, hi] = extent(values)
89
- if (lo === undefined || hi === undefined) {
90
- const now = new Date()
91
- return scaleTime().domain([now, now]).range([0, width])
92
- }
93
-
94
- return scaleTime()
95
- .domain(padDomain(+lo, +hi).map((v) => new Date(v)))
96
- .range([0, width])
97
- .nice()
98
- }
99
- case 'linear': {
100
- const values = data.flatMap((s) => s.values.map((d) => Number(d.x)))
101
- const [lo, hi] = extent(values)
102
- return scaleLinear()
103
- .domain(padDomain(lo ?? 0, hi ?? 1))
104
- .range([0, width])
105
- .nice()
106
- }
107
- }
75
+ switch (kind) {
76
+ case 'band':
77
+ return scaleBand<string>()
78
+ .domain(categoryDomain(data))
79
+ .range([0, width])
80
+ .paddingInner(0.25)
81
+ .paddingOuter(0.15)
82
+ case 'point': {
83
+ const domain = categoryDomain(data)
84
+
85
+ return scalePoint<string>()
86
+ .domain(domain)
87
+ .range([0, width])
88
+ .padding(domain.length <= 1 ? 0 : 0.5)
89
+ }
90
+ case 'time': {
91
+ const values = data.flatMap((s) => s.values.map((d) => d.x as Date))
92
+ const [lo, hi] = extent(values)
93
+
94
+ if (lo === undefined || hi === undefined) {
95
+ const now = new Date()
96
+
97
+ return scaleTime().domain([now, now]).range([0, width])
98
+ }
99
+
100
+ return scaleTime()
101
+ .domain(padDomain(+lo, +hi).map((v) => new Date(v)))
102
+ .range([0, width])
103
+ .nice()
104
+ }
105
+ case 'linear': {
106
+ const values = data.flatMap((s) => s.values.map((d) => Number(d.x)))
107
+ const [lo, hi] = extent(values)
108
+
109
+ return scaleLinear()
110
+ .domain(padDomain(lo ?? 0, hi ?? 1))
111
+ .range([0, width])
112
+ .nice()
113
+ }
114
+ }
108
115
  }
109
116
 
110
117
  /** Degenerate domains (one value) get a unit pad so the scale still maps
111
118
  * somewhere sensible. */
112
119
  function padDomain(lo: number, hi: number): [number, number] {
113
- return lo === hi ? [lo - 0.5, hi + 0.5] : [lo, hi]
120
+ return lo === hi ? [lo - 0.5, hi + 0.5] : [lo, hi]
114
121
  }
115
122
 
116
123
  /** Y domain: bar/area pin the zero baseline; line/scatter take the data
117
124
  * extent; stacked bars sum per category (positive stack only — negative
118
125
  * values in stacked mode stack below zero independently). */
119
126
  export function yDomain(data: SeriesSpec[], type: ChartType, stacked: boolean): [number, number] {
120
- if (stacked && type === 'bar') {
121
- const pos = new Map<string, number>()
122
- const neg = new Map<string, number>()
123
- for (const series of data) {
124
- for (const d of series.values) {
125
- if (!Number.isFinite(d.y)) {
126
- continue
127
- }
128
-
129
- const key = String(d.x)
130
- if (d.y >= 0) {
131
- pos.set(key, (pos.get(key) ?? 0) + d.y)
132
- } else {
133
- neg.set(key, (neg.get(key) ?? 0) + d.y)
134
- }
135
- }
136
- }
137
-
138
- const hi = Math.max(0, ...pos.values())
139
- const lo = Math.min(0, ...neg.values())
140
- return lo === hi ? [0, 1] : [lo, hi]
141
- }
142
-
143
- const values = data.flatMap((s) => s.values.map((d) => d.y))
144
- const [lo, hi] = extent(values)
145
- const min = lo ?? 0
146
- const max = hi ?? 1
147
- if (type === 'bar' || type === 'area') {
148
- const domain: [number, number] = [Math.min(0, min), Math.max(0, max)]
149
- return domain[0] === domain[1] ? [0, 1] : domain
150
- }
151
-
152
- return padDomain(min, max)
127
+ if (stacked && type === 'bar') {
128
+ const pos = new Map<string, number>()
129
+ const neg = new Map<string, number>()
130
+
131
+ for (const series of data) {
132
+ for (const d of series.values) {
133
+ if (!Number.isFinite(d.y)) {
134
+ continue
135
+ }
136
+
137
+ const key = String(d.x)
138
+
139
+ if (d.y >= 0) {
140
+ pos.set(key, (pos.get(key) ?? 0) + d.y)
141
+ } else {
142
+ neg.set(key, (neg.get(key) ?? 0) + d.y)
143
+ }
144
+ }
145
+ }
146
+
147
+ const hi = Math.max(0, ...pos.values())
148
+ const lo = Math.min(0, ...neg.values())
149
+
150
+ return lo === hi ? [0, 1] : [lo, hi]
151
+ }
152
+
153
+ const values = data.flatMap((s) => s.values.map((d) => d.y))
154
+ const [lo, hi] = extent(values)
155
+ const min = lo ?? 0
156
+ const max = hi ?? 1
157
+
158
+ if (type === 'bar' || type === 'area') {
159
+ const domain: [number, number] = [Math.min(0, min), Math.max(0, max)]
160
+
161
+ return domain[0] === domain[1] ? [0, 1] : domain
162
+ }
163
+
164
+ return padDomain(min, max)
153
165
  }
154
166
 
155
167
  export function buildYScale(domain: [number, number], height: number): YScale {
156
- return scaleLinear().domain(domain).range([height, 0]).nice()
168
+ return scaleLinear().domain(domain).range([height, 0]).nice()
157
169
  }
158
170
 
159
171
  /** Pixel center of an x value — band offsets by half the band, point and
160
172
  * continuous scales map directly. */
161
173
  export function xCenter(kind: XScaleKind, scale: XScale, value: number | string | Date): number {
162
- if (kind === 'band') {
163
- const band = scale as ScaleBand<string>
164
- return (band(String(value)) ?? NaN) + band.bandwidth() / 2
165
- }
174
+ if (kind === 'band') {
175
+ const band = scale as ScaleBand<string>
176
+
177
+ return (band(String(value)) ?? NaN) + band.bandwidth() / 2
178
+ }
166
179
 
167
- if (kind === 'point') {
168
- return (scale as ScalePoint<string>)(String(value)) ?? NaN
169
- }
180
+ if (kind === 'point') {
181
+ return (scale as ScalePoint<string>)(String(value)) ?? NaN
182
+ }
170
183
 
171
- return (scale as ScaleLinear<number, number> | ScaleTime<number, number>)(value as never)
184
+ return (scale as ScaleLinear<number, number> | ScaleTime<number, number>)(value as never)
172
185
  }
package/src/core/scene.ts CHANGED
@@ -10,136 +10,139 @@ import { svgDocument } from './svg'
10
10
  /** Default categorical palette — Tableau 10, embedded so the leaf stays free
11
11
  * of d3-scale-chromatic. */
12
12
  export const PALETTE: string[] = [
13
- '#4e79a7',
14
- '#f28e2b',
15
- '#e15759',
16
- '#76b7b2',
17
- '#59a14f',
18
- '#edc948',
19
- '#b07aa1',
20
- '#ff9da7',
21
- '#9c755f',
22
- '#bab0ac',
13
+ '#4e79a7',
14
+ '#f28e2b',
15
+ '#e15759',
16
+ '#76b7b2',
17
+ '#59a14f',
18
+ '#edc948',
19
+ '#b07aa1',
20
+ '#ff9da7',
21
+ '#9c755f',
22
+ '#bab0ac',
23
23
  ]
24
24
 
25
25
  export interface LegendItem {
26
- color: string
27
- text: string
26
+ color: string
27
+ text: string
28
28
  }
29
29
 
30
30
  /** The fully-resolved chart: svg markup, overlay label boxes, legend items,
31
31
  * and per-datum hit targets in chart space. `computeScene` is the core's one
32
32
  * entry point — the leaves only render it. */
33
33
  export interface ChartScene {
34
- type: ChartProps['type']
35
- width: number
36
- height: number
37
- plot: PlotBox
38
- markup: string
39
- labels: LabelSpec[]
40
- legend: LegendItem[]
41
- series: { name: string; color: string }[]
42
- points: ScenePoint[]
43
- pie: PieScene | null
34
+ type: ChartProps['type']
35
+ width: number
36
+ height: number
37
+ plot: PlotBox
38
+ markup: string
39
+ labels: LabelSpec[]
40
+ legend: LegendItem[]
41
+ series: { name: string; color: string }[]
42
+ points: ScenePoint[]
43
+ pie: PieScene | null
44
44
  }
45
45
 
46
46
  export function computeScene(props: ChartProps, width: number, height: number): ChartScene | null {
47
- if (!(width > 0) || !(height > 0) || !props.data?.length) {
48
- return null
49
- }
50
-
51
- const pie = props.type === 'pie'
52
- const xLabels = !pie && props.xAxis?.labels !== false
53
- const yLabels = !pie && props.yAxis?.labels !== false
54
- const margin = {
55
- top: 8,
56
- right: 10,
57
- bottom: pie ? 8 : xLabels ? 26 : 10,
58
- left: pie ? 8 : yLabels ? 46 : 10,
59
- ...props.margin,
60
- }
61
-
62
- const plot: PlotBox = {
63
- x: margin.left,
64
- y: margin.top,
65
- width: Math.max(0, width - margin.left - margin.right),
66
- height: Math.max(0, height - margin.top - margin.bottom),
67
- }
68
-
69
- if (!(plot.width > 0) || !(plot.height > 0)) {
70
- return null
71
- }
72
-
73
- const series = props.data.map((s, i) => ({
74
- name: s.name,
75
- color: s.color ?? PALETTE[i % PALETTE.length],
76
- values: s.values,
77
- }))
78
-
79
- let marks: Mark[]
80
- let points: ScenePoint[]
81
- let pieScene: PieScene | null = null
82
- let labels: LabelSpec[] = []
83
- let grid: Mark[] = []
84
- let frame: Mark[] = []
85
-
86
- if (pie) {
87
- const built = buildMarks(
88
- 'pie',
89
- // Pie doesn't consume the cartesian scales — placeholders only.
90
- { plot, xKind: 'band', x: buildXScale('band', [], 0), y: buildYScale([0, 1], 1) },
91
- series,
92
- { innerRadiusRatio: props.innerRadiusRatio, palette: PALETTE },
93
- )
94
-
95
- marks = built.marks
96
- points = []
97
- const center = { cx: built.pie!.cx + plot.x, cy: built.pie!.cy + plot.y }
98
- pieScene = {
99
- ...built.pie!,
100
- ...center,
101
- slices: built.pie!.slices.map((s) => ({ ...s, cx: s.cx + center.cx, cy: s.cy + center.cy })),
102
- }
103
- } else {
104
- const xKind = inferXKind(props.type, props.data)
105
- const layout = {
106
- plot,
107
- xKind,
108
- x: buildXScale(xKind, props.data, plot.width),
109
- y: buildYScale(yDomain(props.data, props.type, props.stacked === true), plot.height),
110
- }
111
-
112
- const built = buildMarks(props.type, layout, series, {
113
- stacked: props.stacked,
114
- palette: PALETTE,
115
- })
116
-
117
- marks = built.marks
118
- // Marks stay plot-space in the markup (the serializer wraps them in a
119
- // translate group); hit targets publish chart space.
120
- points = built.points.map((p) => ({ ...p, cx: p.cx + plot.x, cy: p.cy + plot.y }))
121
-
122
- const xAxis = xAxisModel(xKind, layout.x, props.xAxis, plot)
123
- const yAxis = yAxisModel(layout.y, props.yAxis, plot)
124
- labels = [...xAxis.labels, ...yAxis.labels]
125
- grid = [...xAxis.grid, ...yAxis.grid]
126
- frame = [...xAxis.frame, ...yAxis.frame]
127
- }
128
-
129
- const legend = pieScene
130
- ? pieScene.slices.map((s) => ({ color: s.color, text: s.label }))
131
- : series.map((s) => ({ color: s.color, text: s.name }))
132
-
133
- return {
134
- type: props.type,
135
- width,
136
- height,
137
- plot,
138
- markup: svgDocument(marks, grid, frame, plot, width, height),
139
- labels,
140
- legend,
141
- series,
142
- points,
143
- pie: pieScene,
144
- }
47
+ if (!(width > 0) || !(height > 0) || !props.data?.length) {
48
+ return null
49
+ }
50
+
51
+ const pie = props.type === 'pie'
52
+ const xLabels = !pie && props.xAxis?.labels !== false
53
+ const yLabels = !pie && props.yAxis?.labels !== false
54
+ const margin = {
55
+ top: 8,
56
+ right: 10,
57
+ bottom: pie ? 8 : xLabels ? 26 : 10,
58
+ left: pie ? 8 : yLabels ? 46 : 10,
59
+ ...props.margin,
60
+ }
61
+
62
+ const plot: PlotBox = {
63
+ x: margin.left,
64
+ y: margin.top,
65
+ width: Math.max(0, width - margin.left - margin.right),
66
+ height: Math.max(0, height - margin.top - margin.bottom),
67
+ }
68
+
69
+ if (!(plot.width > 0) || !(plot.height > 0)) {
70
+ return null
71
+ }
72
+
73
+ const series = props.data.map((s, i) => ({
74
+ name: s.name,
75
+ color: s.color ?? PALETTE[i % PALETTE.length],
76
+ values: s.values,
77
+ }))
78
+
79
+ let marks: Mark[]
80
+ let points: ScenePoint[]
81
+ let pieScene: PieScene | null = null
82
+ let labels: LabelSpec[] = []
83
+ let grid: Mark[] = []
84
+ let frame: Mark[] = []
85
+
86
+ if (pie) {
87
+ const built = buildMarks(
88
+ 'pie',
89
+ // Pie doesn't consume the cartesian scales — placeholders only.
90
+ { plot, xKind: 'band', x: buildXScale('band', [], 0), y: buildYScale([0, 1], 1) },
91
+ series,
92
+ { innerRadiusRatio: props.innerRadiusRatio, palette: PALETTE },
93
+ )
94
+
95
+ marks = built.marks
96
+ points = []
97
+
98
+ const center = { cx: built.pie!.cx + plot.x, cy: built.pie!.cy + plot.y }
99
+
100
+ pieScene = {
101
+ ...built.pie!,
102
+ ...center,
103
+ slices: built.pie!.slices.map((s) => ({ ...s, cx: s.cx + center.cx, cy: s.cy + center.cy })),
104
+ }
105
+ } else {
106
+ const xKind = inferXKind(props.type, props.data)
107
+ const layout = {
108
+ plot,
109
+ xKind,
110
+ x: buildXScale(xKind, props.data, plot.width),
111
+ y: buildYScale(yDomain(props.data, props.type, props.stacked === true), plot.height),
112
+ }
113
+
114
+ const built = buildMarks(props.type, layout, series, {
115
+ stacked: props.stacked,
116
+ palette: PALETTE,
117
+ })
118
+
119
+ marks = built.marks
120
+ // Marks stay plot-space in the markup (the serializer wraps them in a
121
+ // translate group); hit targets publish chart space.
122
+ points = built.points.map((p) => ({ ...p, cx: p.cx + plot.x, cy: p.cy + plot.y }))
123
+
124
+ const xAxis = xAxisModel(xKind, layout.x, props.xAxis, plot)
125
+ const yAxis = yAxisModel(layout.y, props.yAxis, plot)
126
+
127
+ labels = [...xAxis.labels, ...yAxis.labels]
128
+ grid = [...xAxis.grid, ...yAxis.grid]
129
+ frame = [...xAxis.frame, ...yAxis.frame]
130
+ }
131
+
132
+ const legend = pieScene
133
+ ? pieScene.slices.map((s) => ({ color: s.color, text: s.label }))
134
+ : series.map((s) => ({ color: s.color, text: s.name }))
135
+
136
+ return {
137
+ type: props.type,
138
+ width,
139
+ height,
140
+ plot,
141
+ markup: svgDocument(marks, grid, frame, plot, width, height),
142
+ labels,
143
+ legend,
144
+ series,
145
+ points,
146
+ pie: pieScene,
147
+ }
145
148
  }