Skip to content

Commit 9561ad4

Browse files
authored
Rule marks (#622)
* feat(Rule): Support using as data-driven mark (ex. candlestick) by passing `x`/`y` as accessor (ex. `<Rule y={["high", "low"]} />`) * breaking(Axis): Rename x="left|right" and `y="top|bottom"` props with `$` prefix (ex. `<Axis x="$left">`) * Add TODO to replace `<Points links>` with `<Rule>` * Fix band scale use cases (string value from domain) * Remove lines out of the bounds of display (range) * Add brushing and remove redudant cScale (default) * Set xDomain on chart to have smoother scrubing * fix(Rule): Properly band scales * breaking(Points): Remove `<Points links>` prop. Use `<Rule>` with x/y accessor instead * Tweak candlestick example * Update related * feat(Rule): Support single value/accessor for both dimensions (lollipop use case) * Add lollipop example * fix(Rule): Improve single property accessors for both dimensions (ex. lollipop chart) * Add isScaleNumber() util * Simplify data and time series data generators * Simplify some Rule examples (if no accessor promised, will be data driven based on Chart accessors) * docs(Rule): Add data driven examples * Update changesets
1 parent 292a244 commit 9561ad4

23 files changed

Lines changed: 577 additions & 195 deletions

File tree

.changeset/evil-hoops-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': patch
3+
---
4+
5+
breaking(Axis): Rename `x="left|right"` and `y="top|bottom"` props with `$` prefix (ex. `<Axis x="$left">`)

.changeset/large-spiders-stay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': minor
3+
---
4+
5+
feat(Rule): Support using as data-driven mark (ex. candlestick, lollipop) by default (`<Rule>` using Chart accessors) or passing explicit `x`/`y` accessors (ex. `<Rule y={["high", "low"]} />`). Resolves #64

.changeset/proud-melons-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': minor
3+
---
4+
5+
breaking(Points): Remove `<Points links>` prop. Use `<Rule>` with x/y accessor instead

packages/layerchart/src/lib/components/Axis.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@
439439
{#if rule !== false}
440440
{@const ruleProps = extractLayerProps(rule, 'axis-rule')}
441441
<Rule
442-
x={placement === 'left' || placement === 'right' ? placement : placement === 'angle'}
443-
y={placement === 'top' || placement === 'bottom' ? placement : placement === 'radius'}
442+
x={placement === 'left' ? '$left' : placement === 'right' ? '$right' : placement === 'angle'}
443+
y={placement === 'top' ? '$top' : placement === 'bottom' ? '$bottom' : placement === 'radius'}
444444
{motion}
445445
{...ruleProps}
446446
class={cls('stroke-surface-content/50', classes.rule, ruleProps?.class)}

packages/layerchart/src/lib/components/Bars.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import Bar, { type BarProps, type BarPropsWithoutHTML } from './Bar.svelte';
3333
import Group from './Group.svelte';
3434
35-
import { chartDataArray } from '../utils/common.js';
3635
import { getChartContext } from './Chart.svelte';
36+
import { chartDataArray } from '../utils/common.js';
3737
import { extractLayerProps, layerClass } from '$lib/utils/attributes.js';
3838
3939
let {

packages/layerchart/src/lib/components/Points.svelte

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts" module>
22
import type { CommonStyleProps, Without } from '$lib/utils/types.js';
3-
import type { ComponentProps, Snippet } from 'svelte';
3+
import type { Snippet } from 'svelte';
44
55
export type Point = { x: number; y: number; r: number; xValue: any; yValue: any; data: any };
66
type Offset = number | ((value: number, context: any) => number) | undefined;
@@ -37,13 +37,6 @@
3737
*/
3838
offsetY?: Offset;
3939
40-
/**
41-
* Enable showing links between related points (array x/y accessors)
42-
*
43-
* @default false
44-
*/
45-
links?: boolean | Partial<ComponentProps<typeof Link>>;
46-
4740
children?: Snippet<[{ points: Point[] }]>;
4841
} & CommonStyleProps;
4942
@@ -71,7 +64,6 @@
7164
r = 5,
7265
offsetX,
7366
offsetY,
74-
links = false,
7567
fill,
7668
fillOpacity,
7769
stroke,
@@ -136,66 +128,11 @@
136128
return [];
137129
}) as Point[]
138130
);
139-
140-
const _links = $derived(
141-
pointsData.flatMap((d: any) => {
142-
const xValue = xAccessor(d);
143-
const yValue = yAccessor(d);
144-
145-
if (Array.isArray(xValue)) {
146-
/*
147-
x={["prop1" ,"prop2"]}
148-
y="prop3"
149-
*/
150-
const [xMin, xMax] = extent(ctx.xGet(d)) as unknown as [number, number];
151-
const y = ctx.yGet(d) + getOffset(ctx.yGet(d), offsetY, ctx.yScale);
152-
return {
153-
source: {
154-
x: xMin + getOffset(xMin, offsetX, ctx.xScale) + (ctx.config.r ? ctx.rGet(d) : r),
155-
y,
156-
},
157-
target: {
158-
x: xMax + getOffset(xMax, offsetX, ctx.xScale) - (ctx.config.r ? ctx.rGet(d) : r),
159-
y: y,
160-
},
161-
data: d,
162-
};
163-
} else if (Array.isArray(yValue)) {
164-
/*
165-
x="prop1"
166-
y={["prop2" ,"prop3"]}
167-
*/
168-
const x = ctx.xGet(d) + getOffset(ctx.xGet(d), offsetX, ctx.xScale);
169-
const [yMin, yMax] = extent(ctx.yGet(d)) as unknown as [number, number];
170-
return {
171-
source: {
172-
x: x,
173-
y: yMin + getOffset(yMin, offsetY, ctx.yScale),
174-
},
175-
target: {
176-
x: x,
177-
y: yMax + getOffset(yMax, offsetY, ctx.yScale),
178-
},
179-
data: d,
180-
};
181-
}
182-
})
183-
);
184131
</script>
185132

186133
{#if children}
187134
{@render children({ points })}
188135
{:else}
189-
{#if links}
190-
{#each _links as link}
191-
<Link
192-
data={link}
193-
stroke={fill ?? (ctx.config.c ? ctx.cGet(link.data) : null)}
194-
{...extractLayerProps(links, 'points-link')}
195-
/>
196-
{/each}
197-
{/if}
198-
199136
{#each points as point}
200137
<Circle
201138
cx={point.x}

0 commit comments

Comments
 (0)