Skip to content

Commit 9b3e13a

Browse files
committed
Move resolveAccessor out of AreaChart/BarChart and into series.svelte.ts, but will refine further into chart.svelte.ts soon.
1 parent dbc3db0 commit 9b3e13a

File tree

3 files changed

+32
-41
lines changed

3 files changed

+32
-41
lines changed

packages/layerchart/src/lib/components/charts/AreaChart.svelte

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
chartDataArray,
5050
defaultChartPadding,
5151
getObjectOrNull,
52-
type Accessor,
5352
} from '$lib/utils/common.js';
5453
import { SeriesState, type StackLayout } from '$lib/states/series.svelte.js';
5554
import type { BrushDomainType } from '../../states/brush.svelte.js';
@@ -110,32 +109,14 @@
110109
console.timeEnd('AreaChart render');
111110
});
112111
}
113-
114-
function resolveAccessor(acc: Accessor<TData> | undefined) {
115-
if (seriesState.isStacked) {
116-
// For stacked series, collect all y0/y1 values for domain calculation
117-
return (d: TData) => {
118-
const values: number[] = [];
119-
for (const s of seriesState.visibleSeries) {
120-
const stackValue = seriesState.getStackValue(s.key, d);
121-
if (stackValue) {
122-
values.push(stackValue[0], stackValue[1]);
123-
}
124-
}
125-
return values.length ? values : undefined;
126-
};
127-
}
128-
if (acc) return acc;
129-
return seriesState.visibleSeries.map((s) => s.value ?? s.key);
130-
}
131112
</script>
132113

133114
<Chart
134115
bind:context
135116
{data}
136117
{x}
137118
{xDomain}
138-
y={resolveAccessor(y)}
119+
y={seriesState.getValueDomainAccessor(y)}
139120
yBaseline={0}
140121
yNice
141122
{radial}

packages/layerchart/src/lib/components/charts/BarChart.svelte

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
accessor,
8181
chartDataArray,
8282
defaultChartPadding,
83-
type Accessor,
8483
} from '$lib/utils/common.js';
8584
import { isScaleTime, type AnyScale } from '$lib/utils/scales.svelte.js';
8685
import { SeriesState, type StackLayout } from '$lib/states/series.svelte.js';
@@ -224,30 +223,12 @@
224223
console.timeEnd('BarChart render');
225224
});
226225
}
227-
228-
function resolveAccessor(acc: Accessor<TData> | undefined) {
229-
if (acc) return acc;
230-
if (seriesState.isStacked) {
231-
// For stacked series, collect all y0/y1 values for domain calculation
232-
return (d: TData) => {
233-
const values: number[] = [];
234-
for (const s of seriesState.visibleSeries) {
235-
const stackValue = seriesState.getStackValue(s.key, d);
236-
if (stackValue) {
237-
values.push(stackValue[0], stackValue[1]);
238-
}
239-
}
240-
return values.length ? values : undefined;
241-
};
242-
}
243-
return seriesState.visibleSeries.map((s) => s.value ?? s.key);
244-
}
245226
</script>
246227

247228
<Chart
248229
bind:context
249230
{data}
250-
x={resolveAccessor(xProp)}
231+
x={valueAxis === 'x' ? seriesState.getValueDomainAccessor(xProp) : xProp}
251232
{xDomain}
252233
{xScale}
253234
{xBaseline}
@@ -256,7 +237,7 @@
256237
{x1Domain}
257238
{x1Range}
258239
{xInterval}
259-
y={resolveAccessor(yProp)}
240+
y={valueAxis === 'y' ? seriesState.getValueDomainAccessor(yProp) : yProp}
260241
{yScale}
261242
{yBaseline}
262243
yNice={valueAxis === 'y'}

packages/layerchart/src/lib/states/series.svelte.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,35 @@ export class SeriesState<TData, TComponent extends Component> {
122122
};
123123
}
124124

125+
/**
126+
* Resolve accessor for domain calculation, handling stacked and multi-series cases.
127+
* For stacked series, returns a function that collects all y0/y1 values.
128+
* For multi-series without explicit accessor, returns array of series value accessors.
129+
* @param acc - The explicit accessor provided by the user, if any
130+
* @returns The resolved accessor for domain calculation
131+
*/
132+
getValueDomainAccessor<T extends Accessor<TData>>(acc: T | undefined): T | Accessor<TData> {
133+
// If explicit accessor provided, use it
134+
if (acc) return acc;
135+
136+
// For stacked series, collect all y0/y1 values for domain calculation
137+
if (this.isStacked) {
138+
return ((d: TData) => {
139+
const values: number[] = [];
140+
for (const s of this.visibleSeries) {
141+
const stackValue = this.getStackValue(s.key, d);
142+
if (stackValue) {
143+
values.push(stackValue[0], stackValue[1]);
144+
}
145+
}
146+
return values.length ? values : undefined;
147+
}) as Accessor<TData>;
148+
}
149+
150+
// Multi-series: use all visible series accessors
151+
return this.visibleSeries.map((s) => s.value ?? s.key) as Accessor<TData>;
152+
}
153+
125154
/**
126155
* Get all series for the chart.
127156
*/

0 commit comments

Comments
 (0)