Skip to content

Commit e9a30f6

Browse files
committed
fix sort typing, fix default sorting
1 parent ab10b25 commit e9a30f6

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

src/lib/helpers/scales.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import isDataRecord from './isDataRecord.js';
3030

3131
import { createProjection } from './projection.js';
3232
import { maybeInterval } from './autoTicks.js';
33+
import { IS_SORTED } from 'svelteplot/transforms/sort.js';
3334

3435
/**
3536
* compute the plot scales
@@ -183,7 +184,8 @@ export function createScale<T extends ScaleOptions>(
183184
// we're deliberately checking for !== undefined and not for != null
184185
// since the explicit sort transforms like shuffle will set the
185186
// sort channel to null to we know that there's an explicit order
186-
if (name === 'x' && mark.options.sort != null) {
187+
if (name === 'y') console.log(mark.type, mark.options[IS_SORTED]);
188+
if (mark.options[IS_SORTED] != null) {
187189
sortOrdinalDomain = false;
188190
}
189191
for (const channel of mark.channels) {

src/lib/transforms/sort.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { shuffler } from 'd3-array';
55
import { randomLcg } from 'd3-random';
66

77
export const SORT_KEY = Symbol('sortKey');
8+
export const IS_SORTED = Symbol('isSorted');
89

910
export function sort(
1011
{ data, ...channels }: TransformArg<DataRecord>,
@@ -38,6 +39,7 @@ export function sort(
3839
.map(({ [SORT_KEY]: a, ...rest }) => rest),
3940

4041
...channels,
42+
[IS_SORTED]: sort,
4143
// set the sort channel to null to disable the implicit alphabetical
4244
// ordering of ordinal domains, and also to avoid double sorting in case
4345
// this transform is used "outside" a mark

src/lib/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ export type PlotDefaults = {
445445
css: (d: string) => string | undefined;
446446
};
447447

448-
export type GenericMarkOptions = Record<string, any>;
448+
export type GenericMarkOptions = Record<string | symbol, any>;
449449

450450
export type DataRecord = Record<string | symbol, RawValue> & {
451451
___orig___?: RawValue | [RawValue, RawValue];
@@ -657,6 +657,15 @@ export type BaseMarkProps = Partial<{
657657
dy: ConstantAccessor<number>;
658658
fill: ConstantAccessor<string>;
659659
fillOpacity: ConstantAccessor<number>;
660+
sort:
661+
| string
662+
| ConstantAccessor<RawValue>
663+
| {
664+
/** sort data using an already defined channel */
665+
channel: string;
666+
/** sort order */
667+
order?: 'ascending' | 'descending';
668+
};
660669
stroke: ConstantAccessor<string>;
661670
strokeWidth: ConstantAccessor<number>;
662671
strokeOpacity: ConstantAccessor<number>;

src/routes/examples/+layout.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { loadDatasets, loadJSON } from '$lib/helpers/data.js';
2-
import type { PageLoad } from './$types.js';
2+
import type { PageServerLoad } from '../$types';
33

44
export const ssr = true;
55

6-
export const load: PageLoad = async ({ fetch }) => {
6+
export const load: PageServerLoad = async ({ fetch }) => {
77
return {
88
data: {
99
world: await loadJSON(fetch, 'countries-110m'),
@@ -16,6 +16,7 @@ export const load: PageLoad = async ({ fetch }) => {
1616
'co2',
1717
'crimea',
1818
'driving',
19+
'languages',
1920
'penguins',
2021
'riaa',
2122
'stateage',
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script module>
2+
export const title = 'Dot plot';
3+
</script>
4+
5+
<script>
6+
import { Plot, Dot, GridY } from 'svelteplot';
7+
import { page } from '$app/state';
8+
let { languages } = $derived(page.data.data);
9+
</script>
10+
11+
<Plot
12+
frame
13+
inset={20}
14+
x={{
15+
type: 'log',
16+
axis: 'both',
17+
label: 'NUMBER OF SPEAKERS',
18+
labelAnchor: 'center'
19+
}}
20+
y={{ type: 'point', label: '' }}>
21+
<GridY strokeDasharray="1,3" strokeOpacity={0.5} />
22+
<Dot
23+
data={languages.filter((d) => d['Total speakers'] >= 70e6)}
24+
fill="currentColor"
25+
sort={{ channel: '-x' }}
26+
y="Language"
27+
x="Total speakers" />
28+
</Plot>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy