@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.
package/src/core/axes.ts CHANGED
@@ -10,20 +10,20 @@ import { xCenter } from './scales'
10
10
  * never as svg `<text>` (androidsvg/SVGKit text is degraded). `x`/`y` is the
11
11
  * box origin in chart space; `width` bounds it for alignment. */
12
12
  export interface LabelSpec {
13
- x: number
14
- y: number
15
- width: number
16
- align: 'start' | 'center' | 'end'
17
- text: string
18
- axis: 'x' | 'y'
13
+ x: number
14
+ y: number
15
+ width: number
16
+ align: 'start' | 'center' | 'end'
17
+ text: string
18
+ axis: 'x' | 'y'
19
19
  }
20
20
 
21
21
  export interface AxisModel {
22
- labels: LabelSpec[]
23
- /** Spine and tick stubs — painted over marks. */
24
- frame: Mark[]
25
- /** Gridlines — painted under marks. */
26
- grid: Mark[]
22
+ labels: LabelSpec[]
23
+ /** Spine and tick stubs — painted over marks. */
24
+ frame: Mark[]
25
+ /** Gridlines — painted under marks. */
26
+ grid: Mark[]
27
27
  }
28
28
 
29
29
  const AXIS_COLOR = '#9aa3ad'
@@ -37,175 +37,182 @@ const day = timeFormat('%b %d')
37
37
  /** Default tick label formatting — SI suffixes for big magnitudes, plain
38
38
  * significant digits otherwise, short month-day for dates. */
39
39
  export function defaultFormat(value: number | string | Date): string {
40
- if (value instanceof Date) {
41
- return day(value)
42
- }
40
+ if (value instanceof Date) {
41
+ return day(value)
42
+ }
43
43
 
44
- if (typeof value === 'number') {
45
- return Math.abs(value) >= 1000 ? si(value) : plain(value)
46
- }
44
+ if (typeof value === 'number') {
45
+ return Math.abs(value) >= 1000 ? si(value) : plain(value)
46
+ }
47
47
 
48
- return String(value)
48
+ return String(value)
49
49
  }
50
50
 
51
51
  /** Cap categorical label count so boxes don't collide — keep every nth. */
52
52
  function thinDomain(domain: string[], plotWidth: number): string[] {
53
- const maxLabels = Math.max(2, Math.floor(plotWidth / 56))
54
- if (domain.length <= maxLabels) {
55
- return domain
56
- }
53
+ const maxLabels = Math.max(2, Math.floor(plotWidth / 56))
57
54
 
58
- const step = Math.ceil(domain.length / maxLabels)
59
- return domain.filter((_, i) => i % step === 0)
55
+ if (domain.length <= maxLabels) {
56
+ return domain
57
+ }
58
+
59
+ const step = Math.ceil(domain.length / maxLabels)
60
+
61
+ return domain.filter((_, i) => i % step === 0)
60
62
  }
61
63
 
62
64
  export function xAxisModel(
63
- kind: XScaleKind,
64
- scale: XScale,
65
- spec: AxisSpec | undefined,
66
- plot: PlotBox,
65
+ kind: XScaleKind,
66
+ scale: XScale,
67
+ spec: AxisSpec | undefined,
68
+ plot: PlotBox,
67
69
  ): AxisModel {
68
- const labels = spec?.labels !== false
69
- const stubs = spec?.ticks !== false
70
- const grid = spec?.grid === true
71
- const fmt = spec?.format ?? defaultFormat
72
-
73
- const frame: Mark[] = [
74
- {
75
- kind: 'line',
76
- x1: plot.x,
77
- y1: plot.y + plot.height,
78
- x2: plot.x + plot.width,
79
- y2: plot.y + plot.height,
80
- stroke: AXIS_COLOR,
81
- strokeWidth: 1,
82
- },
83
- ]
84
-
85
- const gridMarks: Mark[] = []
86
- const out: LabelSpec[] = []
87
-
88
- let values: (string | number | Date)[]
89
- let format: (v: number | string | Date) => string = fmt
90
- if (kind === 'band' || kind === 'point') {
91
- values = thinDomain((scale as ScaleBand<string> | ScalePoint<string>).domain(), plot.width)
92
- } else {
93
- const continuous = scale as ScaleLinear<number, number> | ScaleTime<number, number>
94
- const count = spec?.tickCount ?? 6
95
- values = continuous.ticks(count) as (number | Date)[]
96
- if (!spec?.format && kind === 'time') {
97
- const tf = (continuous as ScaleTime<number, number>).tickFormat(count)
98
- format = (v) => tf(v as Date)
99
- }
100
- }
101
-
102
- for (const v of values) {
103
- const px = plot.x + xCenter(kind, scale, v)
104
- if (stubs) {
105
- frame.push({
106
- kind: 'line',
107
- x1: px,
108
- y1: plot.y + plot.height,
109
- x2: px,
110
- y2: plot.y + plot.height + 4,
111
- stroke: AXIS_COLOR,
112
- strokeWidth: 1,
113
- })
114
- }
115
-
116
- if (grid) {
117
- gridMarks.push({
118
- kind: 'line',
119
- x1: px,
120
- y1: plot.y,
121
- x2: px,
122
- y2: plot.y + plot.height,
123
- stroke: GRID_COLOR,
124
- strokeOpacity: GRID_OPACITY,
125
- strokeWidth: 1,
126
- })
127
- }
128
-
129
- if (labels) {
130
- const width = Math.min(72, plot.width / Math.max(1, values.length))
131
- const boxX = Math.min(
132
- Math.max(0, px - width / 2),
133
- Math.max(0, plot.x + plot.width + 8 - width),
134
- )
135
-
136
- out.push({
137
- x: boxX,
138
- y: plot.y + plot.height + 6,
139
- width,
140
- align: 'center',
141
- text: format(v),
142
- axis: 'x',
143
- })
144
- }
145
- }
146
-
147
- return { labels: out, frame, grid: gridMarks }
70
+ const labels = spec?.labels !== false
71
+ const stubs = spec?.ticks !== false
72
+ const grid = spec?.grid === true
73
+ const fmt = spec?.format ?? defaultFormat
74
+
75
+ const frame: Mark[] = [
76
+ {
77
+ kind: 'line',
78
+ x1: plot.x,
79
+ y1: plot.y + plot.height,
80
+ x2: plot.x + plot.width,
81
+ y2: plot.y + plot.height,
82
+ stroke: AXIS_COLOR,
83
+ strokeWidth: 1,
84
+ },
85
+ ]
86
+
87
+ const gridMarks: Mark[] = []
88
+ const out: LabelSpec[] = []
89
+
90
+ let values: (string | number | Date)[]
91
+ let format: (v: number | string | Date) => string = fmt
92
+
93
+ if (kind === 'band' || kind === 'point') {
94
+ values = thinDomain((scale as ScaleBand<string> | ScalePoint<string>).domain(), plot.width)
95
+ } else {
96
+ const continuous = scale as ScaleLinear<number, number> | ScaleTime<number, number>
97
+ const count = spec?.tickCount ?? 6
98
+
99
+ values = continuous.ticks(count) as (number | Date)[]
100
+ if (!spec?.format && kind === 'time') {
101
+ const tf = (continuous as ScaleTime<number, number>).tickFormat(count)
102
+
103
+ format = (v) => tf(v as Date)
104
+ }
105
+ }
106
+
107
+ for (const v of values) {
108
+ const px = plot.x + xCenter(kind, scale, v)
109
+
110
+ if (stubs) {
111
+ frame.push({
112
+ kind: 'line',
113
+ x1: px,
114
+ y1: plot.y + plot.height,
115
+ x2: px,
116
+ y2: plot.y + plot.height + 4,
117
+ stroke: AXIS_COLOR,
118
+ strokeWidth: 1,
119
+ })
120
+ }
121
+
122
+ if (grid) {
123
+ gridMarks.push({
124
+ kind: 'line',
125
+ x1: px,
126
+ y1: plot.y,
127
+ x2: px,
128
+ y2: plot.y + plot.height,
129
+ stroke: GRID_COLOR,
130
+ strokeOpacity: GRID_OPACITY,
131
+ strokeWidth: 1,
132
+ })
133
+ }
134
+
135
+ if (labels) {
136
+ const width = Math.min(72, plot.width / Math.max(1, values.length))
137
+ const boxX = Math.min(
138
+ Math.max(0, px - width / 2),
139
+ Math.max(0, plot.x + plot.width + 8 - width),
140
+ )
141
+
142
+ out.push({
143
+ x: boxX,
144
+ y: plot.y + plot.height + 6,
145
+ width,
146
+ align: 'center',
147
+ text: format(v),
148
+ axis: 'x',
149
+ })
150
+ }
151
+ }
152
+
153
+ return { labels: out, frame, grid: gridMarks }
148
154
  }
149
155
 
150
156
  export function yAxisModel(scale: YScale, spec: AxisSpec | undefined, plot: PlotBox): AxisModel {
151
- const labels = spec?.labels !== false
152
- const stubs = spec?.ticks !== false
153
- const grid = spec?.grid === true
154
- const fmt = spec?.format ?? defaultFormat
155
-
156
- const frame: Mark[] = [
157
- {
158
- kind: 'line',
159
- x1: plot.x,
160
- y1: plot.y,
161
- x2: plot.x,
162
- y2: plot.y + plot.height,
163
- stroke: AXIS_COLOR,
164
- strokeWidth: 1,
165
- },
166
- ]
167
-
168
- const gridMarks: Mark[] = []
169
- const out: LabelSpec[] = []
170
-
171
- for (const v of scale.ticks(spec?.tickCount ?? 5)) {
172
- const py = plot.y + scale(v)
173
- if (stubs) {
174
- frame.push({
175
- kind: 'line',
176
- x1: plot.x - 4,
177
- y1: py,
178
- x2: plot.x,
179
- y2: py,
180
- stroke: AXIS_COLOR,
181
- strokeWidth: 1,
182
- })
183
- }
184
-
185
- if (grid) {
186
- gridMarks.push({
187
- kind: 'line',
188
- x1: plot.x,
189
- y1: py,
190
- x2: plot.x + plot.width,
191
- y2: py,
192
- stroke: GRID_COLOR,
193
- strokeOpacity: GRID_OPACITY,
194
- strokeWidth: 1,
195
- })
196
- }
197
-
198
- if (labels) {
199
- out.push({
200
- x: 0,
201
- y: py - 6,
202
- width: Math.max(0, plot.x - 8),
203
- align: 'end',
204
- text: fmt(v),
205
- axis: 'y',
206
- })
207
- }
208
- }
209
-
210
- return { labels: out, frame, grid: gridMarks }
157
+ const labels = spec?.labels !== false
158
+ const stubs = spec?.ticks !== false
159
+ const grid = spec?.grid === true
160
+ const fmt = spec?.format ?? defaultFormat
161
+
162
+ const frame: Mark[] = [
163
+ {
164
+ kind: 'line',
165
+ x1: plot.x,
166
+ y1: plot.y,
167
+ x2: plot.x,
168
+ y2: plot.y + plot.height,
169
+ stroke: AXIS_COLOR,
170
+ strokeWidth: 1,
171
+ },
172
+ ]
173
+
174
+ const gridMarks: Mark[] = []
175
+ const out: LabelSpec[] = []
176
+
177
+ for (const v of scale.ticks(spec?.tickCount ?? 5)) {
178
+ const py = plot.y + scale(v)
179
+
180
+ if (stubs) {
181
+ frame.push({
182
+ kind: 'line',
183
+ x1: plot.x - 4,
184
+ y1: py,
185
+ x2: plot.x,
186
+ y2: py,
187
+ stroke: AXIS_COLOR,
188
+ strokeWidth: 1,
189
+ })
190
+ }
191
+
192
+ if (grid) {
193
+ gridMarks.push({
194
+ kind: 'line',
195
+ x1: plot.x,
196
+ y1: py,
197
+ x2: plot.x + plot.width,
198
+ y2: py,
199
+ stroke: GRID_COLOR,
200
+ strokeOpacity: GRID_OPACITY,
201
+ strokeWidth: 1,
202
+ })
203
+ }
204
+
205
+ if (labels) {
206
+ out.push({
207
+ x: 0,
208
+ y: py - 6,
209
+ width: Math.max(0, plot.x - 8),
210
+ align: 'end',
211
+ text: fmt(v),
212
+ axis: 'y',
213
+ })
214
+ }
215
+ }
216
+
217
+ return { labels: out, frame, grid: gridMarks }
211
218
  }