svelteplot 0.2.9-pr-104.1 → 0.2.9-pr-104.3

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,6 +6,7 @@ import { resolveProp, toChannelOption } from './resolve.js';
6
6
  import isDataRecord from './isDataRecord.js';
7
7
  import { createProjection } from './projection.js';
8
8
  import { maybeInterval } from './autoTicks.js';
9
+ import { IS_SORTED } from '../transforms/sort.js';
9
10
  /**
10
11
  * compute the plot scales
11
12
  */
@@ -54,7 +55,7 @@ export function createScale(name, scaleOptions, marks, plotOptions, plotWidth, p
54
55
  // we're deliberately checking for !== undefined and not for != null
55
56
  // since the explicit sort transforms like shuffle will set the
56
57
  // sort channel to null to we know that there's an explicit order
57
- if (name === 'x' && mark.options.sort != null) {
58
+ if ((name === 'x' || name === 'y') && mark.options[IS_SORTED] != undefined) {
58
59
  sortOrdinalDomain = false;
59
60
  }
60
61
  for (const channel of mark.channels) {
@@ -136,6 +137,12 @@ export function createScale(name, scaleOptions, marks, plotOptions, plotWidth, p
136
137
  }
137
138
  }
138
139
  }
140
+ if ((name === 'x' || name === 'y') && scaleOptions.sort) {
141
+ sortOrdinalDomain = true;
142
+ }
143
+ if ((name === 'x' || name === 'y') && scaleOptions.sort === false) {
144
+ sortOrdinalDomain = false;
145
+ }
139
146
  // construct domain from data values
140
147
  const valueArr = [...dataValues.values(), ...(scaleOptions.domain || [])].filter((d) => d != null);
141
148
  const type = scaleOptions.type === 'auto'
@@ -99,7 +99,7 @@
99
99
  const ticks: RawValue[] = $derived(
100
100
  data.length > 0
101
101
  ? // use custom tick values if user passed any as prop
102
- data
102
+ Array.from(new Set(data))
103
103
  : // use custom scale tick values if user passed any as plot scale option
104
104
  autoTicks(
105
105
  plot.scales.x.type,
@@ -49,7 +49,6 @@
49
49
  return d3Symbol(maybeSymbol(symbolType), size)();
50
50
  }
51
51
 
52
- const { getTestFacet } = getContext<FacetContext>('svelteplot/facet');
53
52
  const { dotRadius } = getContext<PlotDefaults>('svelteplot/_defaults');
54
53
 
55
54
  const args = $derived(
@@ -1,10 +1,11 @@
1
1
  import type { DataRecord, DataRow, TransformArg } from '../types.js';
2
2
  export declare const SORT_KEY: unique symbol;
3
+ export declare const IS_SORTED: unique symbol;
3
4
  export declare function sort({ data, ...channels }: TransformArg<DataRecord>, options?: {
4
5
  reverse?: boolean;
5
6
  }): any;
6
7
  /**
7
- * reverses the data row order
8
+ * shuffles the data row order
8
9
  */
9
10
  export declare function shuffle({ data, ...channels }: TransformArg<DataRow[]>, options?: {
10
11
  seed?: number;
@@ -3,6 +3,7 @@ import { resolveChannel } from '../helpers/resolve.js';
3
3
  import { shuffler } from 'd3-array';
4
4
  import { randomLcg } from 'd3-random';
5
5
  export const SORT_KEY = Symbol('sortKey');
6
+ export const IS_SORTED = Symbol('isSorted');
6
7
  export function sort({ data, ...channels }, options = {}) {
7
8
  if (!Array.isArray(data))
8
9
  return { data, ...channels };
@@ -14,19 +15,26 @@ export function sort({ data, ...channels }, options = {}) {
14
15
  sort.channel = sort.channel.substring(1);
15
16
  sort.order = 'descending';
16
17
  }
18
+ // if sort is a function that does not take exactly one argument, we treat it
19
+ // as comparator function, as you would pass to array.sort
20
+ const isComparator = typeof channels.sort === 'function' && channels.sort.length !== 1;
17
21
  // sort data
18
22
  return {
19
- data: data
20
- .map((d) => ({
21
- ...d,
22
- [SORT_KEY]: resolveChannel('sort', d, { ...channels, sort })
23
- }))
24
- .toSorted((a, b) => (a[SORT_KEY] > b[SORT_KEY] ? 1 : a[SORT_KEY] < b[SORT_KEY] ? -1 : 0) *
25
- (options.reverse || (isDataRecord(sort) && sort?.order === 'descending')
26
- ? -1
27
- : 1))
28
- .map(({ [SORT_KEY]: a, ...rest }) => rest),
23
+ data: isComparator
24
+ ? data.toSorted(channels.sort)
25
+ : data
26
+ .map((d) => ({
27
+ ...d,
28
+ [SORT_KEY]: resolveChannel('sort', d, { ...channels, sort })
29
+ }))
30
+ .toSorted((a, b) => (a[SORT_KEY] > b[SORT_KEY] ? 1 : a[SORT_KEY] < b[SORT_KEY] ? -1 : 0) *
31
+ (options.reverse ||
32
+ (isDataRecord(sort) && sort?.order === 'descending')
33
+ ? -1
34
+ : 1))
35
+ .map(({ [SORT_KEY]: a, ...rest }) => rest),
29
36
  ...channels,
37
+ [IS_SORTED]: sort,
30
38
  // set the sort channel to null to disable the implicit alphabetical
31
39
  // ordering of ordinal domains, and also to avoid double sorting in case
32
40
  // this transform is used "outside" a mark
@@ -39,7 +47,7 @@ export function sort({ data, ...channels }, options = {}) {
39
47
  };
40
48
  }
41
49
  /**
42
- * reverses the data row order
50
+ * shuffles the data row order
43
51
  */
44
52
  export function shuffle({ data, ...channels }, options = {}) {
45
53
  const random = randomLcg(options.seed);
@@ -49,7 +57,8 @@ export function shuffle({ data, ...channels }, options = {}) {
49
57
  ...channels,
50
58
  // set the sort channel to null to disable the implicit
51
59
  // alphabetical ordering of ordinal domains
52
- sort: null
60
+ sort: null,
61
+ [IS_SORTED]: true
53
62
  };
54
63
  }
55
64
  /**
@@ -61,6 +70,7 @@ export function reverse({ data, ...channels }) {
61
70
  ...channels,
62
71
  // set the sort channel to null to disable the implicit
63
72
  // alphabetical ordering of ordinal domains
64
- sort: null
73
+ sort: null,
74
+ [IS_SORTED]: true
65
75
  };
66
76
  }
package/dist/types.d.ts CHANGED
@@ -345,7 +345,7 @@ export type PlotDefaults = {
345
345
  unknown: string;
346
346
  css: (d: string) => string | undefined;
347
347
  };
348
- export type GenericMarkOptions = Record<string, any>;
348
+ export type GenericMarkOptions = Record<string | symbol, any>;
349
349
  export type DataRecord = Record<string | symbol, RawValue> & {
350
350
  ___orig___?: RawValue | [RawValue, RawValue];
351
351
  };
@@ -512,6 +512,12 @@ export type BaseMarkProps = Partial<{
512
512
  dy: ConstantAccessor<number>;
513
513
  fill: ConstantAccessor<string>;
514
514
  fillOpacity: ConstantAccessor<number>;
515
+ sort: string | ConstantAccessor<RawValue> | ((a: RawValue, b: RawValue) => number) | {
516
+ /** sort data using an already defined channel */
517
+ channel: string;
518
+ /** sort order */
519
+ order?: 'ascending' | 'descending';
520
+ };
515
521
  stroke: ConstantAccessor<string>;
516
522
  strokeWidth: ConstantAccessor<number>;
517
523
  strokeOpacity: ConstantAccessor<number>;
@@ -577,7 +583,7 @@ export type BaseRectMarkProps = {
577
583
  borderRadius?: BorderRadius;
578
584
  };
579
585
  export type Channels = Record<string, ChannelAccessor | ConstantAccessor<string | number | boolean | symbol>>;
580
- export type TransformArg<K> = Channels & {
586
+ export type TransformArg<K> = Channels & BaseMarkProps & {
581
587
  data: K[];
582
588
  };
583
589
  export type MapArg<K> = Channels & {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelteplot",
3
- "version": "0.2.9-pr-104.1",
3
+ "version": "0.2.9-pr-104.3",
4
4
  "license": "ISC",
5
5
  "author": {
6
6
  "name": "Gregor Aisch",