svelteplot 0.2.9-pr-104.2 → 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.
@@ -55,9 +55,7 @@ export function createScale(name, scaleOptions, marks, plotOptions, plotWidth, p
55
55
  // we're deliberately checking for !== undefined and not for != null
56
56
  // since the explicit sort transforms like shuffle will set the
57
57
  // sort channel to null to we know that there's an explicit order
58
- if (name === 'y')
59
- console.log(mark.type, mark.options[IS_SORTED]);
60
- if (mark.options[IS_SORTED] != null) {
58
+ if ((name === 'x' || name === 'y') && mark.options[IS_SORTED] != undefined) {
61
59
  sortOrdinalDomain = false;
62
60
  }
63
61
  for (const channel of mark.channels) {
@@ -139,6 +137,12 @@ export function createScale(name, scaleOptions, marks, plotOptions, plotWidth, p
139
137
  }
140
138
  }
141
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
+ }
142
146
  // construct domain from data values
143
147
  const valueArr = [...dataValues.values(), ...(scaleOptions.domain || [])].filter((d) => d != null);
144
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(
@@ -5,7 +5,7 @@ export declare function sort({ data, ...channels }: TransformArg<DataRecord>, op
5
5
  reverse?: boolean;
6
6
  }): any;
7
7
  /**
8
- * reverses the data row order
8
+ * shuffles the data row order
9
9
  */
10
10
  export declare function shuffle({ data, ...channels }: TransformArg<DataRow[]>, options?: {
11
11
  seed?: number;
@@ -15,18 +15,24 @@ export function sort({ data, ...channels }, options = {}) {
15
15
  sort.channel = sort.channel.substring(1);
16
16
  sort.order = 'descending';
17
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;
18
21
  // sort data
19
22
  return {
20
- data: data
21
- .map((d) => ({
22
- ...d,
23
- [SORT_KEY]: resolveChannel('sort', d, { ...channels, sort })
24
- }))
25
- .toSorted((a, b) => (a[SORT_KEY] > b[SORT_KEY] ? 1 : a[SORT_KEY] < b[SORT_KEY] ? -1 : 0) *
26
- (options.reverse || (isDataRecord(sort) && sort?.order === 'descending')
27
- ? -1
28
- : 1))
29
- .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),
30
36
  ...channels,
31
37
  [IS_SORTED]: sort,
32
38
  // set the sort channel to null to disable the implicit alphabetical
@@ -41,7 +47,7 @@ export function sort({ data, ...channels }, options = {}) {
41
47
  };
42
48
  }
43
49
  /**
44
- * reverses the data row order
50
+ * shuffles the data row order
45
51
  */
46
52
  export function shuffle({ data, ...channels }, options = {}) {
47
53
  const random = randomLcg(options.seed);
@@ -51,7 +57,8 @@ export function shuffle({ data, ...channels }, options = {}) {
51
57
  ...channels,
52
58
  // set the sort channel to null to disable the implicit
53
59
  // alphabetical ordering of ordinal domains
54
- sort: null
60
+ sort: null,
61
+ [IS_SORTED]: true
55
62
  };
56
63
  }
57
64
  /**
@@ -63,6 +70,7 @@ export function reverse({ data, ...channels }) {
63
70
  ...channels,
64
71
  // set the sort channel to null to disable the implicit
65
72
  // alphabetical ordering of ordinal domains
66
- sort: null
73
+ sort: null,
74
+ [IS_SORTED]: true
67
75
  };
68
76
  }
package/dist/types.d.ts CHANGED
@@ -512,7 +512,7 @@ export type BaseMarkProps = Partial<{
512
512
  dy: ConstantAccessor<number>;
513
513
  fill: ConstantAccessor<string>;
514
514
  fillOpacity: ConstantAccessor<number>;
515
- sort: string | ConstantAccessor<RawValue> | {
515
+ sort: string | ConstantAccessor<RawValue> | ((a: RawValue, b: RawValue) => number) | {
516
516
  /** sort data using an already defined channel */
517
517
  channel: string;
518
518
  /** sort order */
@@ -583,7 +583,7 @@ export type BaseRectMarkProps = {
583
583
  borderRadius?: BorderRadius;
584
584
  };
585
585
  export type Channels = Record<string, ChannelAccessor | ConstantAccessor<string | number | boolean | symbol>>;
586
- export type TransformArg<K> = Channels & {
586
+ export type TransformArg<K> = Channels & BaseMarkProps & {
587
587
  data: K[];
588
588
  };
589
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.2",
3
+ "version": "0.2.9-pr-104.3",
4
4
  "license": "ISC",
5
5
  "author": {
6
6
  "name": "Gregor Aisch",