@carto/ps-react-ui 4.24.0 → 4.26.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.
Files changed (92) hide show
  1. package/dist/{actions-B1ss030_.js → actions-DXfQMrll.js} +56 -46
  2. package/dist/actions-DXfQMrll.js.map +1 -0
  3. package/dist/category-DuKx_CJX.js +933 -0
  4. package/dist/category-DuKx_CJX.js.map +1 -0
  5. package/dist/item-color-C1cu6EJd.js +20 -0
  6. package/dist/item-color-C1cu6EJd.js.map +1 -0
  7. package/dist/types/widgets-v2/bar/types.d.ts +6 -0
  8. package/dist/types/widgets-v2/category/category-ui.d.ts +10 -2
  9. package/dist/types/widgets-v2/category/category.d.ts +7 -1
  10. package/dist/types/widgets-v2/category/components/category-bar-stacked.d.ts +8 -1
  11. package/dist/types/widgets-v2/category/components/category-bar.d.ts +24 -2
  12. package/dist/types/widgets-v2/category/components/category-row-multi.d.ts +3 -1
  13. package/dist/types/widgets-v2/category/components/category-row-single.d.ts +3 -1
  14. package/dist/types/widgets-v2/category/components/category-row-stacked.d.ts +10 -1
  15. package/dist/types/widgets-v2/category/style.d.ts +33 -6
  16. package/dist/types/widgets-v2/histogram/bins.d.ts +7 -0
  17. package/dist/types/widgets-v2/histogram/download.d.ts +2 -2
  18. package/dist/types/widgets-v2/histogram/index.d.ts +2 -1
  19. package/dist/types/widgets-v2/histogram/options.d.ts +1 -1
  20. package/dist/types/widgets-v2/histogram/transforms.d.ts +5 -4
  21. package/dist/types/widgets-v2/histogram/types.d.ts +22 -5
  22. package/dist/types/widgets-v2/index.d.ts +1 -1
  23. package/dist/types/widgets-v2/pie/types.d.ts +6 -0
  24. package/dist/types/widgets-v2/scatterplot/download.d.ts +2 -2
  25. package/dist/types/widgets-v2/scatterplot/index.d.ts +2 -1
  26. package/dist/types/widgets-v2/scatterplot/points.d.ts +7 -0
  27. package/dist/types/widgets-v2/scatterplot/transforms.d.ts +4 -3
  28. package/dist/types/widgets-v2/scatterplot/types.d.ts +21 -2
  29. package/dist/types/widgets-v2/timeseries/types.d.ts +6 -0
  30. package/dist/types/widgets-v2/utils/item-color.d.ts +26 -0
  31. package/dist/widgets-v2/actions.js +1 -1
  32. package/dist/widgets-v2/bar.js +66 -57
  33. package/dist/widgets-v2/bar.js.map +1 -1
  34. package/dist/widgets-v2/category.js +1 -1
  35. package/dist/widgets-v2/histogram.js +103 -71
  36. package/dist/widgets-v2/histogram.js.map +1 -1
  37. package/dist/widgets-v2/pie.js +98 -78
  38. package/dist/widgets-v2/pie.js.map +1 -1
  39. package/dist/widgets-v2/scatterplot.js +119 -79
  40. package/dist/widgets-v2/scatterplot.js.map +1 -1
  41. package/dist/widgets-v2/timeseries.js +70 -61
  42. package/dist/widgets-v2/timeseries.js.map +1 -1
  43. package/dist/widgets-v2.js +2 -2
  44. package/dist/widgets-v2.js.map +1 -1
  45. package/package.json +3 -3
  46. package/src/widgets-v2/actions/fullscreen/fullscreen-store.test.tsx +77 -13
  47. package/src/widgets-v2/actions/fullscreen/fullscreen.tsx +48 -5
  48. package/src/widgets-v2/bar/options.test.ts +106 -0
  49. package/src/widgets-v2/bar/options.ts +25 -14
  50. package/src/widgets-v2/bar/types.ts +6 -0
  51. package/src/widgets-v2/category/category-ui.test.tsx +139 -0
  52. package/src/widgets-v2/category/category-ui.tsx +64 -11
  53. package/src/widgets-v2/category/category.test.tsx +67 -0
  54. package/src/widgets-v2/category/category.tsx +32 -15
  55. package/src/widgets-v2/category/components/category-bar-stacked.test.tsx +18 -0
  56. package/src/widgets-v2/category/components/category-bar-stacked.tsx +42 -29
  57. package/src/widgets-v2/category/components/category-bar.test.tsx +66 -0
  58. package/src/widgets-v2/category/components/category-bar.tsx +65 -9
  59. package/src/widgets-v2/category/components/category-row-multi.tsx +21 -0
  60. package/src/widgets-v2/category/components/category-row-single.tsx +4 -0
  61. package/src/widgets-v2/category/components/category-row-stacked.tsx +24 -1
  62. package/src/widgets-v2/category/style.ts +58 -14
  63. package/src/widgets-v2/histogram/bins.ts +22 -0
  64. package/src/widgets-v2/histogram/download.test.ts +10 -0
  65. package/src/widgets-v2/histogram/download.ts +7 -3
  66. package/src/widgets-v2/histogram/index.ts +3 -0
  67. package/src/widgets-v2/histogram/options.test.ts +62 -0
  68. package/src/widgets-v2/histogram/options.ts +38 -17
  69. package/src/widgets-v2/histogram/transforms.test.ts +21 -7
  70. package/src/widgets-v2/histogram/transforms.ts +26 -12
  71. package/src/widgets-v2/histogram/types.ts +25 -5
  72. package/src/widgets-v2/index.ts +6 -1
  73. package/src/widgets-v2/pie/options.test.ts +91 -0
  74. package/src/widgets-v2/pie/options.ts +50 -20
  75. package/src/widgets-v2/pie/types.ts +6 -0
  76. package/src/widgets-v2/scatterplot/download.test.ts +9 -0
  77. package/src/widgets-v2/scatterplot/download.ts +5 -3
  78. package/src/widgets-v2/scatterplot/index.ts +3 -0
  79. package/src/widgets-v2/scatterplot/options.test.ts +78 -0
  80. package/src/widgets-v2/scatterplot/options.ts +35 -11
  81. package/src/widgets-v2/scatterplot/points.ts +35 -0
  82. package/src/widgets-v2/scatterplot/transforms.test.ts +19 -0
  83. package/src/widgets-v2/scatterplot/transforms.ts +23 -18
  84. package/src/widgets-v2/scatterplot/types.ts +25 -2
  85. package/src/widgets-v2/timeseries/options.test.ts +65 -0
  86. package/src/widgets-v2/timeseries/options.ts +32 -17
  87. package/src/widgets-v2/timeseries/types.ts +6 -0
  88. package/src/widgets-v2/utils/item-color.test.ts +74 -0
  89. package/src/widgets-v2/utils/item-color.ts +55 -0
  90. package/dist/actions-B1ss030_.js.map +0 -1
  91. package/dist/category-Ca5XDx0N.js +0 -839
  92. package/dist/category-Ca5XDx0N.js.map +0 -1
@@ -61,6 +61,15 @@ describe('createScatterplotDownloadConfig', () => {
61
61
  )
62
62
  })
63
63
 
64
+ it('CSV writes x/y of object points (colour is not exported)', async () => {
65
+ const items = createScatterplotDownloadConfig({
66
+ filename: 's',
67
+ getData: () => [[{ x: 1, y: 10, color: '#f00' }, [2, 20]]],
68
+ })
69
+ await items.find((i) => i.id === 'csv')!.resolve({ id: null })
70
+ expect(csvText).toBe('series,x,y\nseries_1,1,10\nseries_1,2,20')
71
+ })
72
+
64
73
  it('CSV honours supplied seriesNames', async () => {
65
74
  const items = createScatterplotDownloadConfig({
66
75
  filename: 's',
@@ -4,7 +4,8 @@ import {
4
4
  type DownloadContext,
5
5
  type DownloadItem,
6
6
  } from '../actions/download'
7
- import type { ScatterplotWidgetData } from './types'
7
+ import { getScatterplotXY } from './points'
8
+ import type { ScatterplotItem, ScatterplotWidgetData } from './types'
8
9
 
9
10
  /**
10
11
  * Download menu items for the Scatterplot widget. Always includes a CSV
@@ -14,7 +15,7 @@ import type { ScatterplotWidgetData } from './types'
14
15
  */
15
16
  export function createScatterplotDownloadConfig(args: {
16
17
  filename: string
17
- getData: (ctx: DownloadContext) => ScatterplotWidgetData
18
+ getData: (ctx: DownloadContext) => ScatterplotWidgetData<ScatterplotItem>
18
19
  seriesNames?: readonly string[]
19
20
  getCaptureEl?: (ctx: DownloadContext) => HTMLElement | null
20
21
  pngPixelRatio?: number
@@ -39,7 +40,8 @@ export function createScatterplotDownloadConfig(args: {
39
40
  const rows: unknown[][] = [['series', 'x', 'y']]
40
41
  for (const [i, series] of data.entries()) {
41
42
  const seriesName = args.seriesNames?.[i] ?? `series_${i + 1}`
42
- for (const [x, y] of series) {
43
+ for (const point of series) {
44
+ const [x, y] = getScatterplotXY(point)
43
45
  rows.push([seriesName, x, y])
44
46
  }
45
47
  }
@@ -5,8 +5,11 @@ export {
5
5
  toRelativeScatterplotData,
6
6
  filterScatterplotByLockedPoints,
7
7
  } from './transforms'
8
+ export { getScatterplotXY } from './points'
8
9
  export type {
9
10
  ScatterplotDatum,
11
+ ScatterplotPoint,
12
+ ScatterplotItem,
10
13
  ScatterplotWidgetData,
11
14
  ScatterplotOptionsInput,
12
15
  ScatterplotOptionFactoryInput,
@@ -1,4 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest'
2
+ import * as echarts from 'echarts'
2
3
  import { createTheme, type Theme } from '@mui/material'
3
4
  import { createScatterplotOptionFactory, scatterplotOptions } from './options'
4
5
 
@@ -409,3 +410,80 @@ describe('createScatterplotOptionFactory', () => {
409
410
  expect(insideOnly.grid.right).toBe(baseline.grid.right)
410
411
  })
411
412
  })
413
+
414
+ describe('createScatterplotOptionFactory — per-item colour', () => {
415
+ interface Out {
416
+ dataset: { source: number[][] }[]
417
+ series: { itemStyle: { color: (p: unknown) => string } }[]
418
+ xAxis: { min: number; max: number }
419
+ yAxis: { min: number; max: number }
420
+ }
421
+ const dim = (c: string) => echarts.color.modifyAlpha(c, 0.15)
422
+ const data = [
423
+ [{ x: 1, y: 2, color: '#00ff00' }, [3, 4] as const, { x: -5, y: 60 }],
424
+ ]
425
+
426
+ it('flattens object points to [x, y] rows and bounds them', () => {
427
+ const merge = createScatterplotOptionFactory({ theme })
428
+ const out = merge({}, data) as Out
429
+ expect(out.dataset[0]!.source).toEqual([
430
+ [1, 2],
431
+ [3, 4],
432
+ [-5, 60],
433
+ ])
434
+ expect(out.xAxis.min).toBeLessThanOrEqual(-5)
435
+ expect(out.xAxis.max).toBeGreaterThanOrEqual(3)
436
+ expect(out.yAxis.max).toBeGreaterThanOrEqual(60)
437
+ })
438
+
439
+ it('keeps tuple-only series as the dataset source', () => {
440
+ const merge = createScatterplotOptionFactory({ theme })
441
+ const series = [[1, 2] as const, [3, 4] as const]
442
+ const out = merge({}, [series]) as Out
443
+ expect(out.dataset[0]!.source).toBe(series)
444
+ })
445
+
446
+ it('point colour wins over the series colour; template next; dims', () => {
447
+ const merge = createScatterplotOptionFactory({
448
+ theme,
449
+ series: [{ name: 'S', color: '#0000ff' }],
450
+ selection: ['0:0', '0:1'],
451
+ })
452
+ const out = merge(
453
+ { series: [{ type: 'scatter', itemStyle: { color: '#123456' } }] },
454
+ data,
455
+ ) as Out
456
+ const colorFn = out.series[0]!.itemStyle.color
457
+ expect(colorFn({ dataIndex: 0, color: '#0000ff' })).toBe('#00ff00')
458
+ expect(colorFn({ dataIndex: 1, color: '#0000ff' })).toBe('#123456')
459
+ expect(colorFn({ dataIndex: 2, color: '#0000ff' })).toBe(dim('#123456'))
460
+ })
461
+
462
+ it('calls a function template itemStyle.color via optionsOverride', () => {
463
+ const factory = createScatterplotOptionFactory({
464
+ theme,
465
+ optionsOverride: {
466
+ series: [
467
+ {
468
+ type: 'scatter',
469
+ itemStyle: {
470
+ color: (p: { dataIndex: number }) => `#c${p.dataIndex}`,
471
+ },
472
+ },
473
+ ],
474
+ },
475
+ })
476
+ const out = factory(factory(undefined, undefined), [[[1, 2]]]) as Out
477
+ expect(out.series[0]!.itemStyle.color({ dataIndex: 0, color: '#x' })).toBe(
478
+ '#c0',
479
+ )
480
+ })
481
+
482
+ it('keeps the series / palette colour for plain tuples', () => {
483
+ const merge = createScatterplotOptionFactory({ theme })
484
+ const out = merge({}, [[[1, 2]]]) as Out
485
+ expect(
486
+ out.series[0]!.itemStyle.color({ dataIndex: 0, color: '#abc' }),
487
+ ).toBe('#abc')
488
+ })
489
+ })
@@ -12,10 +12,13 @@ import {
12
12
  import { ZOOM_LAYOUT } from '../actions/zoom-toggle'
13
13
  import type { OptionFactory } from '../echart'
14
14
  import { mergeOptions, resolveThemeColor } from '../utils'
15
+ import { createItemColorResolver } from '../utils/item-color'
16
+ import { getScatterplotXY } from './points'
15
17
  import type {
16
18
  ScatterplotEChartsOption,
17
19
  ScatterplotOptionFactoryInput,
18
20
  ScatterplotOptionsInput,
21
+ ScatterplotItem,
19
22
  ScatterplotWidgetData,
20
23
  } from './types'
21
24
 
@@ -156,7 +159,9 @@ export function createScatterplotOptionFactory(
156
159
  : structural
157
160
  }
158
161
 
159
- const seriesArr = Array.isArray(data) ? (data as ScatterplotWidgetData) : []
162
+ const seriesArr = Array.isArray(data)
163
+ ? (data as ScatterplotWidgetData<ScatterplotItem>)
164
+ : []
160
165
  if (seriesArr.length === 0) {
161
166
  return { ...option, dataset: [], series: [] }
162
167
  }
@@ -198,15 +203,26 @@ export function createScatterplotOptionFactory(
198
203
  // Keeping the key always-present means a plain setOption merge swaps
199
204
  // the callback in place, no `replaceMerge` and no entry-animation
200
205
  // flash on selection on/off.
201
- const makeDimColor =
202
- (seriesIdx: number) => (params: CallbackDataParams) => {
203
- const base = params.color as string
206
+ //
207
+ // The base colour is the point's own `color` (object points), else the
208
+ // template's `itemStyle.color`, else the series colour / palette.
209
+ const seriesTemplates = Array.isArray(option.series) ? option.series : []
210
+ const makeDimColor = (seriesIdx: number, datums: readonly unknown[]) => {
211
+ const baseColor = createItemColorResolver({
212
+ theme,
213
+ datums,
214
+ template: seriesTemplates[seriesIdx] ?? seriesTemplates[0],
215
+ fallback: (params) => params.color as string,
216
+ })
217
+ return (params: CallbackDataParams) => {
218
+ const base = baseColor(params)
204
219
  if (!selectionSet) return base
205
220
  const key = `${seriesIdx}:${params.dataIndex}`
206
221
  return selectionSet.has(key)
207
222
  ? base
208
223
  : echarts.color.modifyAlpha(base, 0.15)
209
224
  }
225
+ }
210
226
 
211
227
  // Zoom slider layout: when ZoomToggle has installed `dataZoom`,
212
228
  // reserve grid space and lift any x-slider above the legend (if
@@ -232,9 +248,15 @@ export function createScatterplotOptionFactory(
232
248
  return {
233
249
  ...option,
234
250
  // ECharts dataset.source wants a mutable [number, number][] shape; we
235
- // hold readonly tuples internally, so cast at the boundary.
236
- dataset: seriesArr.map((s) => ({ source: s as unknown as number[][] })),
237
- series: seriesArr.map((_, i) => {
251
+ // hold readonly tuples internally, so cast at the boundary. Object
252
+ // points are flattened to `[x, y]` so the positional encode and the
253
+ // tooltip keep reading tuples.
254
+ dataset: seriesArr.map((s) => ({
255
+ source: (s.every(Array.isArray)
256
+ ? s
257
+ : s.map(getScatterplotXY)) as unknown as number[][],
258
+ })),
259
+ series: seriesArr.map((s, i) => {
238
260
  const overrideColor = resolveThemeColor(theme, series?.[i]?.color)
239
261
  return {
240
262
  type: 'scatter' as const,
@@ -243,7 +265,7 @@ export function createScatterplotOptionFactory(
243
265
  encode: { x: 0, y: 1 },
244
266
  symbolSize,
245
267
  emphasis: { focus: 'series' },
246
- itemStyle: { color: makeDimColor(i) },
268
+ itemStyle: { color: makeDimColor(i, s) },
247
269
  ...(overrideColor ? { color: overrideColor } : {}),
248
270
  }
249
271
  }),
@@ -386,7 +408,9 @@ function buildScatterTooltipFormatter(
386
408
  })
387
409
  }
388
410
 
389
- function computeScatterBounds(seriesArr: ScatterplotWidgetData): {
411
+ function computeScatterBounds(
412
+ seriesArr: ScatterplotWidgetData<ScatterplotItem>,
413
+ ): {
390
414
  niceMinX: number
391
415
  niceMaxX: number
392
416
  niceMinY: number
@@ -398,8 +422,8 @@ function computeScatterBounds(seriesArr: ScatterplotWidgetData): {
398
422
  let maxY = -Infinity
399
423
  for (const series of seriesArr) {
400
424
  for (const point of series) {
401
- const x = point?.[0]
402
- const y = point?.[1]
425
+ if (point == null) continue
426
+ const [x, y] = getScatterplotXY(point)
403
427
  if (typeof x === 'number' && Number.isFinite(x)) {
404
428
  if (x < minX) minX = x
405
429
  if (x > maxX) maxX = x
@@ -0,0 +1,35 @@
1
+ import type { ScatterplotItem } from './types'
2
+
3
+ /** `[x, y]` of a {@link ScatterplotItem}, whichever form it takes. */
4
+ export function getScatterplotXY(
5
+ point: ScatterplotItem,
6
+ ): readonly [number, number] {
7
+ return isTuple(point) ? point : [point.x, point.y]
8
+ }
9
+
10
+ /** Whether `v` is a valid {@link ScatterplotItem} with finite coordinates. */
11
+ export function isScatterplotDatum(v: unknown): v is ScatterplotItem {
12
+ if (Array.isArray(v)) {
13
+ return v.length === 2 && isFiniteNumber(v[0]) && isFiniteNumber(v[1])
14
+ }
15
+ if (v == null || typeof v !== 'object') return false
16
+ const { x, y } = v as { x?: unknown; y?: unknown }
17
+ return isFiniteNumber(x) && isFiniteNumber(y)
18
+ }
19
+
20
+ /** Same point with new coordinates; keeps the object form (and `color`). */
21
+ export function withScatterplotXY(
22
+ point: ScatterplotItem,
23
+ x: number,
24
+ y: number,
25
+ ): ScatterplotItem {
26
+ return isTuple(point) ? [x, y] : { ...point, x, y }
27
+ }
28
+
29
+ function isTuple(point: ScatterplotItem): point is readonly [number, number] {
30
+ return Array.isArray(point)
31
+ }
32
+
33
+ function isFiniteNumber(v: unknown): v is number {
34
+ return typeof v === 'number' && Number.isFinite(v)
35
+ }
@@ -99,6 +99,25 @@ describe('toRelativeScatterplotData', () => {
99
99
  })
100
100
  })
101
101
 
102
+ describe('toRelativeScatterplotData (object points)', () => {
103
+ it('relativizes y of object points, keeping x and colour', () => {
104
+ expect(
105
+ toRelativeScatterplotData([[{ x: 1, y: 5, color: '#f00' }, [2, 15]]]),
106
+ ).toEqual([[{ x: 1, y: 25, color: '#f00' }, [2, 75]]])
107
+ })
108
+ })
109
+
110
+ describe('filterScatterplotByLockedPoints (object points)', () => {
111
+ it('keeps locked object points as they are', () => {
112
+ expect(
113
+ filterScatterplotByLockedPoints(
114
+ [[{ x: 1, y: 2, color: '#f00' }, { x: 3, y: 4 }, [5, 6]]],
115
+ ['0:0', '0:2'],
116
+ ),
117
+ ).toEqual([[{ x: 1, y: 2, color: '#f00' }, [5, 6]]])
118
+ })
119
+ })
120
+
102
121
  describe('filterScatterplotByLockedPoints', () => {
103
122
  const DATA = [
104
123
  [
@@ -1,6 +1,14 @@
1
+ import {
2
+ getScatterplotXY,
3
+ isScatterplotDatum,
4
+ withScatterplotXY,
5
+ } from './points'
6
+ import type { ScatterplotItem } from './types'
7
+
1
8
  /**
2
9
  * Scatterplot-specific `RelativeData` transform. Scatter data is
3
- * `[number, number][]` — each series is a list of `[x, y]` tuples.
10
+ * `ScatterplotItem[][]` — each series is a list of `[x, y]` tuples or
11
+ * `{ x, y, color? }` points (whose shape is kept).
4
12
  * Relative is a values-axis concept, so this rewrites `y` to its
5
13
  * share of the series's total `y` (0–100) and leaves `x` raw — x is
6
14
  * coordinate space, not part of the cohort total.
@@ -11,34 +19,31 @@
11
19
  * y values produce sane signed shares-of-magnitude. A series whose
12
20
  * total y-magnitude is zero (all-zero or empty input) is returned
13
21
  * unchanged so a stalled or empty data set doesn't show misleading 0%
14
- * values. Inputs whose shape isn't `[number, number][]` fall through
22
+ * values. Inputs whose shape isn't `ScatterplotItem[]` fall through
15
23
  * untouched.
16
24
  */
17
25
  export const toRelativeScatterplotData = (input: unknown): unknown => {
18
26
  if (!Array.isArray(input)) return input
19
27
  return input.map((series: unknown): unknown => {
20
- if (!isXyTupleArray(series)) return series
21
- const total = series.reduce((acc, [, y]) => acc + Math.abs(y), 0)
28
+ if (!isPointArray(series)) return series
29
+ const total = series.reduce(
30
+ (acc, p) => acc + Math.abs(getScatterplotXY(p)[1]),
31
+ 0,
32
+ )
22
33
  if (total <= 0) return series
23
- return series.map(([x, y]) => [x, (y / total) * 100] as [number, number])
34
+ return series.map((p) => {
35
+ const [x, y] = getScatterplotXY(p)
36
+ return withScatterplotXY(p, x, (y / total) * 100)
37
+ })
24
38
  })
25
39
  }
26
40
 
27
- function isXyTupleArray(v: unknown): v is [number, number][] {
28
- if (!Array.isArray(v)) return false
29
- return v.every(
30
- (item) =>
31
- Array.isArray(item) &&
32
- item.length === 2 &&
33
- typeof item[0] === 'number' &&
34
- Number.isFinite(item[0]) &&
35
- typeof item[1] === 'number' &&
36
- Number.isFinite(item[1]),
37
- )
41
+ function isPointArray(v: unknown): v is ScatterplotItem[] {
42
+ return Array.isArray(v) && v.every(isScatterplotDatum)
38
43
  }
39
44
 
40
45
  /**
41
- * `LockSelection` filter for Scatterplot. Data is `[number, number][][]` —
46
+ * `LockSelection` filter for Scatterplot. Data is `ScatterplotItem[][]` —
42
47
  * points have no `name`, so selection (and locking) is keyed on a synthetic
43
48
  * `${seriesIndex}:${dataIndex}` string (same format `useChartSelection`'s
44
49
  * `keyFromClick`/`keysFromBrush` produce for this widget). Unlike Histogram,
@@ -53,7 +58,7 @@ export function filterScatterplotByLockedPoints(
53
58
  if (!Array.isArray(input)) return input
54
59
  const locked = new Set<string>(lockedItems.map(String))
55
60
  return input.map((series: unknown, seriesIndex: number): unknown => {
56
- if (!isXyTupleArray(series)) return series
61
+ if (!isPointArray(series)) return series
57
62
  return series.filter((_, dataIndex) =>
58
63
  locked.has(`${seriesIndex}:${dataIndex}`),
59
64
  )
@@ -5,8 +5,31 @@ import type { WidgetSeries } from '../types'
5
5
  /** A single point — `[x, y]` tuple. */
6
6
  export type ScatterplotDatum = readonly [number, number]
7
7
 
8
- /** Scatterplot widget data — array of series, each a list of `[x, y]` tuples. */
9
- export type ScatterplotWidgetData = readonly (readonly ScatterplotDatum[])[]
8
+ /**
9
+ * Object form of a point, to give it its own colour. `color` (CSS colour or
10
+ * theme palette path such as `'primary.main'`) wins over the template's
11
+ * `itemStyle.color` and the series colour; selection dimming still applies.
12
+ */
13
+ export interface ScatterplotPoint {
14
+ x: number
15
+ y: number
16
+ color?: string
17
+ }
18
+
19
+ /** Any accepted point: an `[x, y]` tuple or a {@link ScatterplotPoint}. */
20
+ export type ScatterplotItem = ScatterplotDatum | ScatterplotPoint
21
+
22
+ /**
23
+ * Scatterplot widget data — array of series, each a list of points.
24
+ *
25
+ * Defaults to `[x, y]` tuples so existing code that destructures points
26
+ * keeps type-checking; use `ScatterplotWidgetData<ScatterplotItem>` to also
27
+ * accept `{ x, y, color? }` points. Every scatterplot API (option factory,
28
+ * transforms, download) accepts both forms.
29
+ */
30
+ export type ScatterplotWidgetData<
31
+ P extends ScatterplotItem = ScatterplotDatum,
32
+ > = readonly (readonly P[])[]
10
33
 
11
34
  /** Inputs to the structural-only {@link scatterplotOptions} builder. */
12
35
  export interface ScatterplotOptionsInput {
@@ -1,4 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest'
2
+ import * as echarts from 'echarts'
2
3
  import { createTheme, type Theme } from '@mui/material'
3
4
  import { createTimeseriesOptionFactory, timeseriesOptions } from './options'
4
5
 
@@ -397,3 +398,67 @@ describe('createTimeseriesOptionFactory', () => {
397
398
  expect(out.grid.bottom).toBe(96)
398
399
  })
399
400
  })
401
+
402
+ describe('createTimeseriesOptionFactory — per-item colour', () => {
403
+ interface Out {
404
+ series: {
405
+ showSymbol: boolean
406
+ itemStyle: { color: (p: unknown) => string }
407
+ }[]
408
+ }
409
+ const data = [
410
+ [
411
+ { name: 1, value: 1, color: '#00ff00' },
412
+ { name: 2, value: 2 },
413
+ ],
414
+ ]
415
+ const p = (dataIndex: number, name: number) => ({
416
+ dataIndex,
417
+ color: '#0000ff',
418
+ value: { name },
419
+ })
420
+ const dim = (c: string) => echarts.color.modifyAlpha(c, 0.15)
421
+
422
+ it('datum colour wins over the series colour; template next', () => {
423
+ const merge = createTimeseriesOptionFactory({
424
+ theme,
425
+ series: [{ name: 'S', color: '#0000ff' }],
426
+ })
427
+ const out = merge(
428
+ { series: [{ type: 'line', itemStyle: { color: '#123456' } }] },
429
+ data,
430
+ ) as Out
431
+ const colorFn = out.series[0]!.itemStyle.color
432
+ expect(colorFn(p(0, 1))).toBe('#00ff00')
433
+ expect(colorFn(p(1, 2))).toBe('#123456')
434
+ })
435
+
436
+ it('calls a function template itemStyle.color', () => {
437
+ const factory = createTimeseriesOptionFactory({
438
+ theme,
439
+ optionsOverride: {
440
+ series: [{ type: 'line', itemStyle: { color: () => '#ff0000' } }],
441
+ },
442
+ })
443
+ const out = factory(factory(undefined, undefined), [
444
+ [{ name: 1, value: 1 }],
445
+ ]) as Out
446
+ expect(out.series[0]!.itemStyle.color(p(0, 1))).toBe('#ff0000')
447
+ })
448
+
449
+ it('dims item colours outside the selection', () => {
450
+ const merge = createTimeseriesOptionFactory({ theme, selection: [2] })
451
+ const out = merge({}, data) as Out
452
+ const colorFn = out.series[0]!.itemStyle.color
453
+ expect(colorFn(p(0, 1))).toBe(dim('#00ff00'))
454
+ expect(colorFn(p(1, 2))).toBe('#0000ff')
455
+ })
456
+
457
+ it('shows symbols when a point has its own colour, not otherwise', () => {
458
+ const merge = createTimeseriesOptionFactory({ theme })
459
+ expect((merge({}, data) as Out).series[0]!.showSymbol).toBe(true)
460
+ const plain = merge({}, [[{ name: 1, value: 1 }]]) as Out
461
+ expect(plain.series[0]!.showSymbol).toBe(false)
462
+ expect(plain.series[0]!.itemStyle.color(p(0, 1))).toBe('#0000ff')
463
+ })
464
+ })
@@ -12,6 +12,7 @@ import {
12
12
  import { ZOOM_LAYOUT } from '../actions/zoom-toggle'
13
13
  import type { OptionFactory } from '../echart'
14
14
  import { mergeOptions, resolveThemeColor } from '../utils'
15
+ import { createItemColorResolver } from '../utils/item-color'
15
16
  import { positionDataZoomForLegend } from '../utils/data-zoom-layout'
16
17
  import type {
17
18
  TimeseriesEChartsOption,
@@ -237,24 +238,39 @@ export function createTimeseriesOptionFactory(
237
238
  // and points would stay dimmed forever after an external clear.
238
239
  // Always emitting lets normal merge swap the callback in place, no
239
240
  // `replaceMerge` and no entry-animation flash on selection on/off.
240
- const dimItemStyle = {
241
- color: (params: CallbackDataParams) => {
242
- const datum = params.value as
243
- | { name?: Date | number | string }
244
- | undefined
245
- const raw = datum?.name
246
- const base = params.color as string
247
- if (!selectionSet || raw == null) return base
248
- return selectionSet.has(normalizeName(raw))
249
- ? base
250
- : echarts.color.modifyAlpha(base, 0.15)
251
- },
241
+ //
242
+ // The base colour is the point's own `color`, else the template's
243
+ // `itemStyle.color`, else the series colour / palette (`params.color`).
244
+ const makeDimItemStyle = (datums: readonly object[], template: unknown) => {
245
+ const baseColor = createItemColorResolver({
246
+ theme,
247
+ datums,
248
+ template,
249
+ fallback: (params) => params.color as string,
250
+ })
251
+ return {
252
+ color: (params: CallbackDataParams) => {
253
+ const datum = params.value as
254
+ { name?: Date | number | string } | undefined
255
+ const raw = datum?.name
256
+ const base = baseColor(params)
257
+ if (!selectionSet || raw == null) return base
258
+ return selectionSet.has(normalizeName(raw))
259
+ ? base
260
+ : echarts.color.modifyAlpha(base, 0.15)
261
+ },
262
+ }
252
263
  }
264
+ // Per-point colours only show on markers, so surface them whenever a
265
+ // point carries its own `color` (same reason as the selection case).
266
+ const hasDatumColor = seriesArr.some((s) =>
267
+ s.some((d) => typeof d?.color === 'string' && d.color !== ''),
268
+ )
253
269
 
254
270
  return {
255
271
  ...option,
256
272
  dataset: seriesArr.map((s) => ({ source: s as readonly object[] })),
257
- series: seriesArr.map((_, i) => {
273
+ series: seriesArr.map((s, i) => {
258
274
  const template =
259
275
  (seriesTemplates[i] as object | undefined) ??
260
276
  (broadcastTemplate as object)
@@ -272,10 +288,10 @@ export function createTimeseriesOptionFactory(
272
288
  // When a selection is active, surface markers so the per-point
273
289
  // color callback has something to dim — a continuous line would
274
290
  // hide the visual selection feedback.
275
- showSymbol: selectionSet != null,
291
+ showSymbol: selectionSet != null || hasDatumColor,
276
292
  ...(area ? { areaStyle: {} } : {}),
277
293
  emphasis: { focus: 'series' },
278
- itemStyle: dimItemStyle,
294
+ itemStyle: makeDimItemStyle(s, template),
279
295
  ...(overrideColor
280
296
  ? { color: overrideColor, lineStyle: { color: overrideColor } }
281
297
  : {}),
@@ -327,8 +343,7 @@ function buildTimeseriesTooltipFormatter(
327
343
  ) {
328
344
  return createTooltipFormatter((item) => {
329
345
  const row = item.value as
330
- | { name?: Date | number | string; value?: number }
331
- | undefined
346
+ { name?: Date | number | string; value?: number } | undefined
332
347
  const raw = row?.value
333
348
  const formattedValue =
334
349
  typeof raw === 'number' && formatter ? formatter(raw) : (raw ?? '')
@@ -10,6 +10,12 @@ import type { WidgetSeries } from '../types'
10
10
  export interface TimeseriesDatum {
11
11
  name: Date | number | string
12
12
  value: number
13
+ /**
14
+ * Optional colour for this item (CSS colour or theme palette path such as
15
+ * `'primary.main'`). Wins over the template's `itemStyle.color` and the
16
+ * series colour; selection dimming still applies on top.
17
+ */
18
+ color?: string
13
19
  }
14
20
 
15
21
  /** Timeseries widget data — one array of points per series. */
@@ -0,0 +1,74 @@
1
+ import { describe, it, expect, vi } from 'vitest'
2
+ import { createTheme } from '@mui/material'
3
+ import type { CallbackDataParams } from 'echarts/types/dist/shared'
4
+ import { createItemColorResolver } from './item-color'
5
+
6
+ const theme = createTheme({ palette: { primary: { main: '#1976d2' } } })
7
+ const params = (dataIndex: number) =>
8
+ ({ dataIndex, color: '#fallback' }) as unknown as CallbackDataParams
9
+ const fallback = (p: CallbackDataParams) => p.color as string
10
+
11
+ describe('createItemColorResolver', () => {
12
+ it('uses the datum colour first', () => {
13
+ const resolve = createItemColorResolver({
14
+ theme,
15
+ datums: [{ color: '#00ff00' }],
16
+ template: { itemStyle: { color: '#123456' } },
17
+ fallback,
18
+ })
19
+ expect(resolve(params(0))).toBe('#00ff00')
20
+ })
21
+
22
+ it('uses a string template itemStyle.color when the datum has none', () => {
23
+ const resolve = createItemColorResolver({
24
+ theme,
25
+ datums: [{}, 3],
26
+ template: { itemStyle: { color: '#123456' } },
27
+ fallback,
28
+ })
29
+ expect(resolve(params(0))).toBe('#123456')
30
+ expect(resolve(params(1))).toBe('#123456')
31
+ })
32
+
33
+ it('calls a function template itemStyle.color with the ECharts params', () => {
34
+ const color = vi.fn(() => '#abcdef')
35
+ const resolve = createItemColorResolver({
36
+ theme,
37
+ datums: [{}],
38
+ template: { itemStyle: { color } },
39
+ fallback,
40
+ })
41
+ const p = params(0)
42
+ expect(resolve(p)).toBe('#abcdef')
43
+ expect(color).toHaveBeenCalledWith(p)
44
+ })
45
+
46
+ it('falls back when neither datum nor template give a colour', () => {
47
+ const resolve = createItemColorResolver({
48
+ theme,
49
+ datums: [{ value: 1 }],
50
+ template: { itemStyle: { color: () => undefined } },
51
+ fallback,
52
+ })
53
+ expect(resolve(params(0))).toBe('#fallback')
54
+ expect(resolve(params(5))).toBe('#fallback')
55
+ const noTemplate = createItemColorResolver({
56
+ theme,
57
+ datums: [],
58
+ template: undefined,
59
+ fallback,
60
+ })
61
+ expect(noTemplate(params(0))).toBe('#fallback')
62
+ })
63
+
64
+ it('resolves theme palette paths on datum and template colours', () => {
65
+ const resolve = createItemColorResolver({
66
+ theme,
67
+ datums: [{ color: 'primary.main' }, {}],
68
+ template: { itemStyle: { color: () => 'primary.main' } },
69
+ fallback,
70
+ })
71
+ expect(resolve(params(0))).toBe('#1976d2')
72
+ expect(resolve(params(1))).toBe('#1976d2')
73
+ })
74
+ })