layerchart 2.0.0-next.4 → 2.0.0-next.41
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.
- package/dist/components/AnnotationLine.svelte +15 -2
- package/dist/components/AnnotationPoint.svelte +29 -11
- package/dist/components/AnnotationRange.svelte +18 -4
- package/dist/components/Arc.svelte +5 -5
- package/dist/components/Area.svelte +10 -2
- package/dist/components/Axis.svelte +175 -58
- package/dist/components/Axis.svelte.d.ts +23 -6
- package/dist/components/Bar.svelte +20 -15
- package/dist/components/Bar.svelte.d.ts +2 -2
- package/dist/components/Bars.svelte +4 -4
- package/dist/components/Blur.svelte +7 -6
- package/dist/components/Blur.svelte.d.ts +2 -5
- package/dist/components/BrushContext.svelte +45 -45
- package/dist/components/Calendar.svelte +31 -10
- package/dist/components/Calendar.svelte.d.ts +2 -1
- package/dist/components/Chart.svelte +76 -27
- package/dist/components/Chart.svelte.d.ts +26 -8
- package/dist/components/ChartClipPath.svelte +1 -1
- package/dist/components/Circle.svelte +44 -3
- package/dist/components/CircleClipPath.svelte +8 -1
- package/dist/components/ClipPath.svelte +1 -2
- package/dist/components/ColorRamp.svelte +1 -1
- package/dist/components/ComputedStyles.svelte +9 -2
- package/dist/components/Connector.svelte +3 -3
- package/dist/components/Connector.svelte.d.ts +1 -1
- package/dist/components/Ellipse.svelte +228 -0
- package/dist/components/Ellipse.svelte.d.ts +64 -0
- package/dist/components/ForceSimulation.svelte +184 -50
- package/dist/components/ForceSimulation.svelte.d.ts +95 -21
- package/dist/components/Frame.svelte +1 -1
- package/dist/components/GeoCircle.svelte +1 -1
- package/dist/components/GeoEdgeFade.svelte +1 -1
- package/dist/components/GeoPath.svelte +30 -8
- package/dist/components/GeoPoint.svelte +4 -5
- package/dist/components/GeoSpline.svelte +5 -5
- package/dist/components/GeoSpline.svelte.d.ts +1 -1
- package/dist/components/GeoTile.svelte +1 -1
- package/dist/components/Graticule.svelte +5 -5
- package/dist/components/Grid.svelte +60 -63
- package/dist/components/Group.svelte +13 -8
- package/dist/components/Group.svelte.d.ts +10 -3
- package/dist/components/Highlight.svelte +55 -28
- package/dist/components/Highlight.svelte.d.ts +4 -0
- package/dist/components/Hull.svelte +12 -5
- package/dist/components/Labels.svelte +24 -13
- package/dist/components/Labels.svelte.d.ts +12 -5
- package/dist/components/Legend.svelte +143 -70
- package/dist/components/Legend.svelte.d.ts +12 -8
- package/dist/components/Line.svelte +40 -3
- package/dist/components/LinearGradient.svelte +35 -4
- package/dist/components/Link.svelte +1 -1
- package/dist/components/Marker.svelte +37 -26
- package/dist/components/MonthPath.svelte +26 -12
- package/dist/components/MonthPath.svelte.d.ts +4 -3
- package/dist/components/MotionPath.svelte +1 -1
- package/dist/components/Pack.svelte.d.ts +10 -3
- package/dist/components/Partition.svelte.d.ts +10 -3
- package/dist/components/Pattern.svelte +5 -5
- package/dist/components/Pie.svelte +1 -2
- package/dist/components/Points.svelte +11 -72
- package/dist/components/Points.svelte.d.ts +1 -8
- package/dist/components/Polygon.svelte +309 -0
- package/dist/components/Polygon.svelte.d.ts +115 -0
- package/dist/components/RadialGradient.svelte +4 -6
- package/dist/components/Rect.svelte +55 -5
- package/dist/components/Rect.svelte.d.ts +2 -2
- package/dist/components/RectClipPath.svelte +4 -3
- package/dist/components/RectClipPath.svelte.d.ts +2 -2
- package/dist/components/Rule.svelte +167 -77
- package/dist/components/Rule.svelte.d.ts +7 -2
- package/dist/components/Spline.svelte +59 -28
- package/dist/components/Spline.svelte.d.ts +12 -4
- package/dist/components/Text.svelte +121 -73
- package/dist/components/Text.svelte.d.ts +6 -0
- package/dist/components/TileImage.svelte +19 -4
- package/dist/components/TransformContext.svelte +9 -3
- package/dist/components/TransformControls.svelte +89 -38
- package/dist/components/Tree.svelte.d.ts +10 -3
- package/dist/components/Treemap.svelte +63 -26
- package/dist/components/Treemap.svelte.d.ts +21 -14
- package/dist/components/Voronoi.svelte +12 -13
- package/dist/components/charts/ArcChart.svelte +43 -71
- package/dist/components/charts/ArcChart.svelte.d.ts +10 -3
- package/dist/components/charts/AreaChart.svelte +29 -59
- package/dist/components/charts/AreaChart.svelte.d.ts +10 -3
- package/dist/components/charts/BarChart.svelte +79 -71
- package/dist/components/charts/BarChart.svelte.d.ts +10 -3
- package/dist/components/charts/DefaultTooltip.svelte +3 -3
- package/dist/components/charts/DefaultTooltip.svelte.d.ts +1 -1
- package/dist/components/charts/LineChart.svelte +69 -75
- package/dist/components/charts/LineChart.svelte.d.ts +21 -8
- package/dist/components/charts/PieChart.svelte +44 -71
- package/dist/components/charts/PieChart.svelte.d.ts +10 -3
- package/dist/components/charts/ScatterChart.svelte +10 -39
- package/dist/components/charts/ScatterChart.svelte.d.ts +10 -3
- package/dist/components/charts/utils.svelte.d.ts +1 -19
- package/dist/components/charts/utils.svelte.js +7 -35
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +5 -1
- package/dist/components/layout/Canvas.svelte +96 -69
- package/dist/components/layout/Canvas.svelte.d.ts +6 -0
- package/dist/components/layout/Html.svelte +15 -9
- package/dist/components/layout/Layer.svelte +6 -4
- package/dist/components/layout/Layer.svelte.d.ts +6 -4
- package/dist/components/layout/Svg.svelte +19 -11
- package/dist/components/layout/WebGL.svelte +26 -6
- package/dist/components/layout/WebGL.svelte.d.ts +5 -2
- package/dist/components/tooltip/Tooltip.svelte +73 -36
- package/dist/components/tooltip/Tooltip.svelte.d.ts +10 -3
- package/dist/components/tooltip/TooltipContext.svelte +167 -54
- package/dist/components/tooltip/TooltipContext.svelte.d.ts +19 -5
- package/dist/components/tooltip/TooltipHeader.svelte +32 -18
- package/dist/components/tooltip/TooltipHeader.svelte.d.ts +3 -3
- package/dist/components/tooltip/TooltipItem.svelte +46 -37
- package/dist/components/tooltip/TooltipItem.svelte.d.ts +3 -3
- package/dist/components/tooltip/TooltipList.svelte +12 -10
- package/dist/components/tooltip/TooltipSeparator.svelte +18 -10
- package/dist/components/tooltip/tooltipMetaContext.d.ts +2 -2
- package/dist/docs/Blockquote.svelte +6 -4
- package/dist/docs/Blockquote.svelte.d.ts +4 -19
- package/dist/docs/Code.svelte +70 -28
- package/dist/docs/Code.svelte.d.ts +9 -24
- package/dist/docs/Header1.svelte +4 -2
- package/dist/docs/Header1.svelte.d.ts +4 -28
- package/dist/docs/Json.svelte +11 -3
- package/dist/docs/Json.svelte.d.ts +9 -21
- package/dist/docs/Layout.svelte +10 -7
- package/dist/docs/Layout.svelte.d.ts +4 -19
- package/dist/docs/Link.svelte +7 -3
- package/dist/docs/Link.svelte.d.ts +4 -38
- package/dist/docs/Preview.svelte +22 -23
- package/dist/docs/Preview.svelte.d.ts +5 -6
- package/dist/docs/TilesetField.svelte +20 -19
- package/dist/docs/TilesetField.svelte.d.ts +5 -22
- package/dist/docs/ViewSourceButton.svelte +10 -7
- package/dist/docs/ViewSourceButton.svelte.d.ts +7 -21
- package/dist/states/series.svelte.d.ts +30 -0
- package/dist/states/series.svelte.js +54 -0
- package/dist/styles/daisyui-5.css +6 -0
- package/dist/styles/shadcn-svelte.css +11 -0
- package/dist/styles/skeleton-3.css +15 -0
- package/dist/utils/arcText.svelte.js +4 -4
- package/dist/utils/array.d.ts +11 -0
- package/dist/utils/array.js +23 -0
- package/dist/utils/array.test.d.ts +1 -0
- package/dist/utils/array.test.js +200 -0
- package/dist/utils/attributes.d.ts +3 -13
- package/dist/utils/attributes.js +4 -18
- package/dist/utils/canvas.d.ts +77 -0
- package/dist/utils/canvas.js +105 -41
- package/dist/utils/common.d.ts +9 -0
- package/dist/utils/common.js +18 -1
- package/dist/utils/common.test.js +26 -1
- package/dist/utils/genData.d.ts +22 -8
- package/dist/utils/genData.js +34 -14
- package/dist/utils/graph/dagre.d.ts +4 -4
- package/dist/utils/graph/dagre.js +5 -7
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/math.d.ts +17 -0
- package/dist/utils/math.js +17 -0
- package/dist/utils/motion.svelte.js +1 -1
- package/dist/utils/path.d.ts +10 -0
- package/dist/utils/path.js +30 -0
- package/dist/utils/rect.svelte.d.ts +2 -2
- package/dist/utils/rect.svelte.js +73 -1
- package/dist/utils/scales.svelte.d.ts +9 -3
- package/dist/utils/scales.svelte.js +47 -4
- package/dist/utils/shape.d.ts +43 -0
- package/dist/utils/shape.js +59 -0
- package/dist/utils/stack.js +1 -1
- package/dist/utils/string.d.ts +49 -0
- package/dist/utils/string.js +4 -2
- package/dist/utils/ticks.d.ts +15 -4
- package/dist/utils/ticks.js +140 -159
- package/dist/utils/ticks.test.js +16 -26
- package/dist/utils/treemap.d.ts +1 -1
- package/dist/utils/types.d.ts +15 -2
- package/package.json +36 -35
- package/dist/utils/object.js +0 -2
|
@@ -84,9 +84,8 @@
|
|
|
84
84
|
|
|
85
85
|
<script lang="ts" generics="TData">
|
|
86
86
|
import { onMount, type ComponentProps } from 'svelte';
|
|
87
|
-
import { scaleBand, scaleLinear } from 'd3-scale';
|
|
87
|
+
import { scaleBand, scaleLinear, scaleTime } from 'd3-scale';
|
|
88
88
|
import { stack, stackOffsetDiverging, stackOffsetExpand, stackOffsetNone } from 'd3-shape';
|
|
89
|
-
import { format } from '@layerstack/utils';
|
|
90
89
|
import { cls } from '@layerstack/tailwind';
|
|
91
90
|
|
|
92
91
|
import Axis from '../Axis.svelte';
|
|
@@ -109,8 +108,9 @@
|
|
|
109
108
|
import { asAny } from '../../utils/types.js';
|
|
110
109
|
import type { Insets } from '../../utils/rect.svelte.js';
|
|
111
110
|
import type { SeriesData, SimplifiedChartProps, SimplifiedChartPropsObject } from './types.js';
|
|
112
|
-
import type
|
|
113
|
-
import {
|
|
111
|
+
import { isScaleTime, type AnyScale } from '../../utils/scales.svelte.js';
|
|
112
|
+
import { SeriesState } from '../../states/series.svelte.js';
|
|
113
|
+
import { createLegendProps } from './utils.svelte.js';
|
|
114
114
|
import { setTooltipMetaContext } from '../tooltip/tooltipMetaContext.js';
|
|
115
115
|
import DefaultTooltip from './DefaultTooltip.svelte';
|
|
116
116
|
import ChartAnnotations from './ChartAnnotations.svelte';
|
|
@@ -142,6 +142,8 @@
|
|
|
142
142
|
bandPadding = radial ? 0 : 0.4,
|
|
143
143
|
groupPadding = 0,
|
|
144
144
|
stackPadding = 0,
|
|
145
|
+
xInterval,
|
|
146
|
+
yInterval,
|
|
145
147
|
tooltip = true,
|
|
146
148
|
children: childrenProp,
|
|
147
149
|
aboveContext,
|
|
@@ -160,6 +162,14 @@
|
|
|
160
162
|
? [
|
|
161
163
|
{
|
|
162
164
|
key: 'default',
|
|
165
|
+
label:
|
|
166
|
+
orientation === 'vertical'
|
|
167
|
+
? typeof yProp === 'string'
|
|
168
|
+
? yProp
|
|
169
|
+
: 'value'
|
|
170
|
+
: typeof xProp === 'string'
|
|
171
|
+
? xProp
|
|
172
|
+
: 'value',
|
|
163
173
|
value: orientation === 'vertical' ? yProp : xProp,
|
|
164
174
|
},
|
|
165
175
|
]
|
|
@@ -171,15 +181,60 @@
|
|
|
171
181
|
const isStackSeries = $derived(seriesLayout.startsWith('stack'));
|
|
172
182
|
const isGroupSeries = $derived(seriesLayout === 'group');
|
|
173
183
|
|
|
184
|
+
const chartData: Array<TData & { stackData?: any }> = $derived.by(() => {
|
|
185
|
+
let _chartData = (
|
|
186
|
+
seriesState.allSeriesData.length ? seriesState.allSeriesData : chartDataArray(data)
|
|
187
|
+
) as Array<TData & { stackData?: any }>;
|
|
188
|
+
if (isStackSeries) {
|
|
189
|
+
const seriesKeys = seriesState.visibleSeries.map((s) => s.key);
|
|
190
|
+
|
|
191
|
+
const offset =
|
|
192
|
+
seriesLayout === 'stackExpand'
|
|
193
|
+
? stackOffsetExpand
|
|
194
|
+
: seriesLayout === 'stackDiverging'
|
|
195
|
+
? stackOffsetDiverging
|
|
196
|
+
: stackOffsetNone;
|
|
197
|
+
const stackData = stack()
|
|
198
|
+
.keys(seriesKeys)
|
|
199
|
+
.value((d, key) => {
|
|
200
|
+
const s = series.find((d) => d.key === key)!;
|
|
201
|
+
return accessor(s.value ?? s.key)(d as any);
|
|
202
|
+
})
|
|
203
|
+
.offset(offset)(chartDataArray(data)) as any[];
|
|
204
|
+
|
|
205
|
+
_chartData = _chartData.map((d, i) => {
|
|
206
|
+
return {
|
|
207
|
+
...d,
|
|
208
|
+
stackData: stackData.map((sd) => sd[i]),
|
|
209
|
+
};
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return _chartData;
|
|
213
|
+
});
|
|
214
|
+
|
|
174
215
|
const xScale = $derived(
|
|
175
|
-
xScaleProp ??
|
|
216
|
+
xScaleProp ??
|
|
217
|
+
(xInterval
|
|
218
|
+
? scaleTime()
|
|
219
|
+
: isVertical
|
|
220
|
+
? scaleBand().padding(bandPadding)
|
|
221
|
+
: accessor(xProp)(chartData[0]) instanceof Date // TODO: also check for Array<Date> instances (ex. x={['start', 'end']})
|
|
222
|
+
? scaleTime()
|
|
223
|
+
: scaleLinear())
|
|
176
224
|
);
|
|
177
|
-
const xBaseline = $derived(isVertical ? undefined : 0);
|
|
225
|
+
const xBaseline = $derived(isVertical || isScaleTime(xScale) ? undefined : 0);
|
|
178
226
|
|
|
179
227
|
const yScale = $derived(
|
|
180
|
-
yScaleProp ??
|
|
228
|
+
yScaleProp ??
|
|
229
|
+
(yInterval
|
|
230
|
+
? scaleTime()
|
|
231
|
+
: isVertical
|
|
232
|
+
? accessor(yProp)(chartData[0]) instanceof Date // TODO: also check for Array<Date> instances (ex. y={['start', 'end']})
|
|
233
|
+
? scaleTime()
|
|
234
|
+
: scaleLinear()
|
|
235
|
+
: scaleBand().padding(bandPadding))
|
|
181
236
|
);
|
|
182
|
-
const yBaseline = $derived(isVertical ? 0 : undefined);
|
|
237
|
+
const yBaseline = $derived(isVertical || isScaleTime(yScale) ? 0 : undefined);
|
|
183
238
|
|
|
184
239
|
const x1Scale = $derived(
|
|
185
240
|
isGroupSeries && isVertical ? scaleBand().padding(groupPadding) : undefined
|
|
@@ -214,37 +269,6 @@
|
|
|
214
269
|
return d && typeof d === 'object' && 'stackData' in d;
|
|
215
270
|
}
|
|
216
271
|
|
|
217
|
-
const chartData: Array<TData & { stackData?: any }> = $derived.by(() => {
|
|
218
|
-
let _chartData = (
|
|
219
|
-
seriesState.allSeriesData.length ? seriesState.allSeriesData : chartDataArray(data)
|
|
220
|
-
) as Array<TData & { stackData?: any }>;
|
|
221
|
-
if (isStackSeries) {
|
|
222
|
-
const seriesKeys = seriesState.visibleSeries.map((s) => s.key);
|
|
223
|
-
|
|
224
|
-
const offset =
|
|
225
|
-
seriesLayout === 'stackExpand'
|
|
226
|
-
? stackOffsetExpand
|
|
227
|
-
: seriesLayout === 'stackDiverging'
|
|
228
|
-
? stackOffsetDiverging
|
|
229
|
-
: stackOffsetNone;
|
|
230
|
-
const stackData = stack()
|
|
231
|
-
.keys(seriesKeys)
|
|
232
|
-
.value((d, key) => {
|
|
233
|
-
const s = series.find((d) => d.key === key)!;
|
|
234
|
-
return accessor(s.value ?? s.key)(d as any);
|
|
235
|
-
})
|
|
236
|
-
.offset(offset)(chartDataArray(data)) as any[];
|
|
237
|
-
|
|
238
|
-
_chartData = _chartData.map((d, i) => {
|
|
239
|
-
return {
|
|
240
|
-
...d,
|
|
241
|
-
stackData: stackData.map((sd) => sd[i]),
|
|
242
|
-
};
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
return _chartData;
|
|
246
|
-
});
|
|
247
|
-
|
|
248
272
|
function getBarsProps(s: SeriesData<TData, typeof Bars>, i: number): ComponentProps<typeof Bars> {
|
|
249
273
|
const isFirst = i == 0;
|
|
250
274
|
const isLast = i == seriesState.visibleSeries.length - 1;
|
|
@@ -278,22 +302,21 @@
|
|
|
278
302
|
y: isVertical ? valueAccessor : undefined,
|
|
279
303
|
x1: isVertical && isGroupSeries ? (d) => s.value ?? s.key : undefined,
|
|
280
304
|
y1: !isVertical && isGroupSeries ? (d) => s.value ?? s.key : undefined,
|
|
281
|
-
rounded:
|
|
305
|
+
rounded:
|
|
306
|
+
isStackLayout && i !== seriesState.visibleSeries.length - 1
|
|
307
|
+
? 'none'
|
|
308
|
+
: Array.isArray(xProp) || Array.isArray(yProp)
|
|
309
|
+
? 'all'
|
|
310
|
+
: 'edge',
|
|
282
311
|
radius: 4,
|
|
283
312
|
strokeWidth: 1,
|
|
284
313
|
insets: stackInsets,
|
|
285
314
|
fill: s.color,
|
|
315
|
+
opacity: seriesState.isHighlighted(s.key, true) ? 1 : 0.1,
|
|
286
316
|
onBarClick: (e, detail) => onBarClick(e, { ...detail, series: s }),
|
|
287
317
|
...props.bars,
|
|
288
318
|
...s.props,
|
|
289
|
-
class: cls(
|
|
290
|
-
'transition-opacity',
|
|
291
|
-
seriesState.highlightKey.current &&
|
|
292
|
-
seriesState.highlightKey.current !== s.key &&
|
|
293
|
-
'opacity-10',
|
|
294
|
-
props.bars?.class,
|
|
295
|
-
s.props?.class
|
|
296
|
-
),
|
|
319
|
+
class: cls(props.bars?.class, s.props?.class),
|
|
297
320
|
};
|
|
298
321
|
}
|
|
299
322
|
|
|
@@ -305,16 +328,10 @@
|
|
|
305
328
|
// TODO: Improve placement when using `seriesLayout="group"`
|
|
306
329
|
// data: s.data,
|
|
307
330
|
// y: s.value ?? (s.data ? undefined : s.key),
|
|
331
|
+
opacity: seriesState.isHighlighted(s.key, true) ? 1 : 0.1,
|
|
308
332
|
...props.labels,
|
|
309
333
|
...(typeof labels === 'object' ? labels : null),
|
|
310
|
-
class: cls(
|
|
311
|
-
'stroke-surface-200 transition-opacity',
|
|
312
|
-
seriesState.highlightKey.current &&
|
|
313
|
-
seriesState.highlightKey.current !== s.key &&
|
|
314
|
-
'opacity-10',
|
|
315
|
-
props.labels?.class,
|
|
316
|
-
typeof labels === 'object' && labels.class
|
|
317
|
-
),
|
|
334
|
+
class: cls(props.labels?.class, typeof labels === 'object' && labels.class),
|
|
318
335
|
};
|
|
319
336
|
}
|
|
320
337
|
|
|
@@ -350,26 +367,14 @@
|
|
|
350
367
|
return {
|
|
351
368
|
placement: radial ? 'radius' : 'left',
|
|
352
369
|
|
|
353
|
-
format:
|
|
354
|
-
if (isVertical && seriesLayout === 'stackExpand') {
|
|
355
|
-
return format(value, 'percentRound');
|
|
356
|
-
} else {
|
|
357
|
-
return format(value, undefined, { variant: 'short' });
|
|
358
|
-
}
|
|
359
|
-
},
|
|
370
|
+
format: isVertical && seriesLayout === 'stackExpand' ? 'percentRound' : undefined,
|
|
360
371
|
...(typeof axis === 'object' ? axis : null),
|
|
361
372
|
...props.yAxis,
|
|
362
373
|
};
|
|
363
374
|
}
|
|
364
375
|
return {
|
|
365
376
|
placement: radial ? 'angle' : 'bottom',
|
|
366
|
-
format:
|
|
367
|
-
if (!isVertical && seriesLayout === 'stackExpand') {
|
|
368
|
-
return format(value, 'percentRound');
|
|
369
|
-
} else {
|
|
370
|
-
return format(value, undefined, { variant: 'short' });
|
|
371
|
-
}
|
|
372
|
-
},
|
|
377
|
+
format: !isVertical && seriesLayout === 'stackExpand' ? 'percentRound' : undefined,
|
|
373
378
|
...(typeof axis === 'object' ? axis : null),
|
|
374
379
|
...props.xAxis,
|
|
375
380
|
};
|
|
@@ -426,6 +431,7 @@
|
|
|
426
431
|
{x1Scale}
|
|
427
432
|
{x1Domain}
|
|
428
433
|
{x1Range}
|
|
434
|
+
{xInterval}
|
|
429
435
|
y={resolveAccessor(yProp)}
|
|
430
436
|
{yScale}
|
|
431
437
|
{yBaseline}
|
|
@@ -433,8 +439,9 @@
|
|
|
433
439
|
{y1Scale}
|
|
434
440
|
{y1Domain}
|
|
435
441
|
{y1Range}
|
|
442
|
+
{yInterval}
|
|
436
443
|
c={isVertical ? yProp : xProp}
|
|
437
|
-
cRange={['var(--color-primary)']}
|
|
444
|
+
cRange={['var(--color-primary, currentColor)']}
|
|
438
445
|
{radial}
|
|
439
446
|
padding={radial ? undefined : defaultChartPadding(axis, legend)}
|
|
440
447
|
{...restProps}
|
|
@@ -445,6 +452,7 @@
|
|
|
445
452
|
onclick: onTooltipClick,
|
|
446
453
|
debug,
|
|
447
454
|
...props.tooltip?.context,
|
|
455
|
+
...(typeof tooltip === 'object' ? tooltip : null),
|
|
448
456
|
}}
|
|
449
457
|
brush={brush && (brush === true || brush.mode == undefined || brush.mode === 'integrated')
|
|
450
458
|
? {
|
|
@@ -57,10 +57,17 @@ import Highlight from '../Highlight.svelte';
|
|
|
57
57
|
import Labels from '../Labels.svelte';
|
|
58
58
|
import Rule from '../Rule.svelte';
|
|
59
59
|
import type { SeriesData, SimplifiedChartProps, SimplifiedChartPropsObject } from './types.js';
|
|
60
|
+
declare function $$render<TData>(): {
|
|
61
|
+
props: BarChartProps<TData>;
|
|
62
|
+
exports: {};
|
|
63
|
+
bindings: "context";
|
|
64
|
+
slots: {};
|
|
65
|
+
events: {};
|
|
66
|
+
};
|
|
60
67
|
declare class __sveltets_Render<TData> {
|
|
61
|
-
props():
|
|
62
|
-
events():
|
|
63
|
-
slots():
|
|
68
|
+
props(): ReturnType<typeof $$render<TData>>['props'];
|
|
69
|
+
events(): ReturnType<typeof $$render<TData>>['events'];
|
|
70
|
+
slots(): ReturnType<typeof $$render<TData>>['slots'];
|
|
64
71
|
bindings(): "context";
|
|
65
72
|
exports(): {};
|
|
66
73
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { getChartContext } from '../Chart.svelte';
|
|
5
5
|
import * as Tooltip from '../tooltip/index.js';
|
|
6
6
|
import type { SimplifiedChartPropsObject } from './types.js';
|
|
7
|
-
import type { SeriesState } from '
|
|
7
|
+
import type { SeriesState } from '../../states/series.svelte.js';
|
|
8
8
|
import { format } from '@layerstack/utils';
|
|
9
9
|
import { accessor, findRelatedData } from '../../utils/common.js';
|
|
10
10
|
|
|
@@ -41,14 +41,14 @@
|
|
|
41
41
|
{/each}
|
|
42
42
|
|
|
43
43
|
{#if canHaveTotal && payload.length > 1 && !tooltipProps?.hideTotal}
|
|
44
|
-
<Tooltip.Separator {...tooltipProps?.separator} />
|
|
44
|
+
<Tooltip.Separator {...tooltipProps?.separator} children={undefined} />
|
|
45
45
|
|
|
46
46
|
<Tooltip.Item
|
|
47
47
|
label="total"
|
|
48
48
|
value={sum(seriesState.visibleSeries, (s) => {
|
|
49
49
|
const seriesTooltipData = s.data ? findRelatedData(s.data, data, context.x) : data;
|
|
50
50
|
const valueAccessor = accessor(s.value ?? (s.data ? context.y : s.key));
|
|
51
|
-
return valueAccessor(seriesTooltipData);
|
|
51
|
+
return seriesTooltipData ? valueAccessor(seriesTooltipData) : 0;
|
|
52
52
|
})}
|
|
53
53
|
format="integer"
|
|
54
54
|
valueAlign="right"
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SimplifiedChartPropsObject } from './types.js';
|
|
2
|
-
import type { SeriesState } from '
|
|
2
|
+
import type { SeriesState } from '../../states/series.svelte.js';
|
|
3
3
|
type $$ComponentProps = {
|
|
4
4
|
tooltipProps?: SimplifiedChartPropsObject['tooltip'];
|
|
5
5
|
seriesState: SeriesState<any, any>;
|
|
@@ -44,12 +44,11 @@
|
|
|
44
44
|
LineChartExtraSnippetProps<TData>
|
|
45
45
|
> & {
|
|
46
46
|
/**
|
|
47
|
-
* The
|
|
47
|
+
* The orientation of the line chart.
|
|
48
|
+
*
|
|
49
|
+
* @default 'horizontal'
|
|
48
50
|
*/
|
|
49
|
-
|
|
50
|
-
e: MouseEvent,
|
|
51
|
-
details: { data: HighlightPointData; series: SeriesData<TData, typeof Spline> }
|
|
52
|
-
) => void;
|
|
51
|
+
orientation?: 'vertical' | 'horizontal';
|
|
53
52
|
|
|
54
53
|
props?: LineChartPropsObjProp;
|
|
55
54
|
|
|
@@ -64,13 +63,19 @@
|
|
|
64
63
|
seriesIndex: number;
|
|
65
64
|
}
|
|
66
65
|
>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The event to be dispatched when the point is clicked.
|
|
69
|
+
*/
|
|
70
|
+
onPointClick?: (
|
|
71
|
+
e: MouseEvent,
|
|
72
|
+
details: { data: HighlightPointData; series: SeriesData<TData, typeof Spline> }
|
|
73
|
+
) => void;
|
|
67
74
|
};
|
|
68
75
|
</script>
|
|
69
76
|
|
|
70
77
|
<script lang="ts" generics="TData">
|
|
71
78
|
import { onMount, type ComponentProps } from 'svelte';
|
|
72
|
-
import { scaleLinear, scaleTime } from 'd3-scale';
|
|
73
|
-
import { format } from '@layerstack/utils';
|
|
74
79
|
import { cls } from '@layerstack/tailwind';
|
|
75
80
|
|
|
76
81
|
import Axis from '../Axis.svelte';
|
|
@@ -85,12 +90,7 @@
|
|
|
85
90
|
import Rule from '../Rule.svelte';
|
|
86
91
|
import Spline from '../Spline.svelte';
|
|
87
92
|
|
|
88
|
-
import {
|
|
89
|
-
accessor,
|
|
90
|
-
chartDataArray,
|
|
91
|
-
defaultChartPadding,
|
|
92
|
-
findRelatedData,
|
|
93
|
-
} from '../../utils/common.js';
|
|
93
|
+
import { chartDataArray, defaultChartPadding, findRelatedData } from '../../utils/common.js';
|
|
94
94
|
import { asAny } from '../../utils/types.js';
|
|
95
95
|
import type {
|
|
96
96
|
SeriesData,
|
|
@@ -98,18 +98,22 @@
|
|
|
98
98
|
SimplifiedChartPropsObject,
|
|
99
99
|
SimplifiedChartSnippet,
|
|
100
100
|
} from './types.js';
|
|
101
|
-
import {
|
|
101
|
+
import { SeriesState } from '../../states/series.svelte.js';
|
|
102
|
+
import { createLegendProps } from './utils.svelte.js';
|
|
102
103
|
import { setTooltipMetaContext } from '../tooltip/tooltipMetaContext.js';
|
|
103
|
-
import { layerClass } from '../../utils/attributes.js';
|
|
104
104
|
import DefaultTooltip from './DefaultTooltip.svelte';
|
|
105
105
|
import ChartAnnotations from './ChartAnnotations.svelte';
|
|
106
|
+
import { isScaleTime } from '../../utils/scales.svelte.js';
|
|
106
107
|
|
|
107
108
|
let {
|
|
108
109
|
data = [],
|
|
109
110
|
x: xProp,
|
|
110
|
-
|
|
111
|
+
xScale,
|
|
111
112
|
xDomain,
|
|
113
|
+
y: yProp,
|
|
114
|
+
yScale,
|
|
112
115
|
radial = false,
|
|
116
|
+
orientation = 'horizontal',
|
|
113
117
|
series: seriesProp,
|
|
114
118
|
seriesLayout = 'overlap',
|
|
115
119
|
axis = true,
|
|
@@ -125,7 +129,6 @@
|
|
|
125
129
|
renderContext = 'svg',
|
|
126
130
|
profile = false,
|
|
127
131
|
debug = false,
|
|
128
|
-
xScale: xScaleProp,
|
|
129
132
|
tooltip = true,
|
|
130
133
|
children: childrenProp,
|
|
131
134
|
aboveContext,
|
|
@@ -140,9 +143,25 @@
|
|
|
140
143
|
...restProps
|
|
141
144
|
}: LineChartProps<TData> = $props();
|
|
142
145
|
|
|
146
|
+
const isVertical = $derived(orientation === 'vertical');
|
|
147
|
+
|
|
143
148
|
const series = $derived(
|
|
144
149
|
seriesProp === undefined
|
|
145
|
-
? [
|
|
150
|
+
? [
|
|
151
|
+
{
|
|
152
|
+
key: 'default',
|
|
153
|
+
|
|
154
|
+
label: isVertical
|
|
155
|
+
? typeof xProp === 'string'
|
|
156
|
+
? xProp
|
|
157
|
+
: 'value'
|
|
158
|
+
: typeof yProp === 'string'
|
|
159
|
+
? yProp
|
|
160
|
+
: 'value',
|
|
161
|
+
value: isVertical ? xProp : yProp,
|
|
162
|
+
color: 'var(--color-primary, currentColor)',
|
|
163
|
+
},
|
|
164
|
+
]
|
|
146
165
|
: seriesProp
|
|
147
166
|
);
|
|
148
167
|
const seriesState = new SeriesState(() => series);
|
|
@@ -153,29 +172,18 @@
|
|
|
153
172
|
: chartDataArray(data)) as Array<TData>
|
|
154
173
|
);
|
|
155
174
|
|
|
156
|
-
// Default xScale based on first data's `x` value
|
|
157
|
-
const xScale = $derived(
|
|
158
|
-
xScaleProp ?? (accessor(xProp)(chartData[0]) instanceof Date ? scaleTime() : scaleLinear())
|
|
159
|
-
);
|
|
160
|
-
|
|
161
175
|
function getSplineProps(s: SeriesData<TData, typeof Spline>, i: number) {
|
|
162
176
|
const splineProps: ComponentProps<typeof Spline> = {
|
|
163
177
|
data: s.data,
|
|
164
|
-
|
|
178
|
+
x: isVertical ? (s.value ?? (s.data ? undefined : s.key)) : undefined,
|
|
179
|
+
y: !isVertical ? (s.value ?? (s.data ? undefined : s.key)) : undefined,
|
|
165
180
|
stroke: s.color,
|
|
181
|
+
opacity:
|
|
182
|
+
// Checking `visibleSeries.length <= 1` fixes re-animated tweened areas on hover
|
|
183
|
+
seriesState.visibleSeries.length <= 1 || seriesState.isHighlighted(s.key, true) ? 1 : 0.1,
|
|
166
184
|
...props.spline,
|
|
167
185
|
...s.props,
|
|
168
|
-
class: cls(
|
|
169
|
-
layerClass('line-chart-line'),
|
|
170
|
-
'transition-opacity',
|
|
171
|
-
// Checking `visibleSeries.length > 1` fixes re-animated tweened areas on hover
|
|
172
|
-
seriesState.visibleSeries.length > 1 &&
|
|
173
|
-
seriesState.highlightKey.current &&
|
|
174
|
-
seriesState.highlightKey.current !== s.key &&
|
|
175
|
-
'opacity-10',
|
|
176
|
-
props.spline?.class,
|
|
177
|
-
s.props?.class
|
|
178
|
-
),
|
|
186
|
+
class: cls(props.spline?.class, s.props?.class),
|
|
179
187
|
};
|
|
180
188
|
|
|
181
189
|
return splineProps;
|
|
@@ -184,18 +192,14 @@
|
|
|
184
192
|
function getPointsProps(s: SeriesData<TData, typeof Spline>, i: number) {
|
|
185
193
|
const pointsProps: ComponentProps<typeof Points> = {
|
|
186
194
|
data: s.data,
|
|
187
|
-
|
|
195
|
+
x: isVertical ? (s.value ?? (s.data ? undefined : s.key)) : undefined,
|
|
196
|
+
y: !isVertical ? (s.value ?? (s.data ? undefined : s.key)) : undefined,
|
|
188
197
|
fill: s.color,
|
|
198
|
+
stroke: 'var(--color-surface-100, light-dark(white, black))',
|
|
199
|
+
opacity: seriesState.isHighlighted(s.key, true) ? 1 : 0.1,
|
|
189
200
|
...props.points,
|
|
190
201
|
...(typeof points === 'object' ? points : null),
|
|
191
|
-
class: cls(
|
|
192
|
-
'stroke-surface-200 transition-opacity',
|
|
193
|
-
seriesState.highlightKey.current &&
|
|
194
|
-
seriesState.highlightKey.current !== s.key &&
|
|
195
|
-
'opacity-10',
|
|
196
|
-
props.points?.class,
|
|
197
|
-
typeof points === 'object' && points.class
|
|
198
|
-
),
|
|
202
|
+
class: cls(props.points?.class, typeof points === 'object' && points.class),
|
|
199
203
|
};
|
|
200
204
|
|
|
201
205
|
return pointsProps;
|
|
@@ -204,26 +208,17 @@
|
|
|
204
208
|
function getLabelsProps(s: SeriesData<TData, typeof Spline>, i: number) {
|
|
205
209
|
const labelsProps: ComponentProps<typeof Labels<TData>> = {
|
|
206
210
|
data: s.data,
|
|
207
|
-
|
|
211
|
+
x: isVertical ? (s.value ?? (s.data ? undefined : s.key)) : undefined,
|
|
212
|
+
y: !isVertical ? (s.value ?? (s.data ? undefined : s.key)) : undefined,
|
|
213
|
+
opacity: seriesState.isHighlighted(s.key, true) ? 1 : 0.1,
|
|
208
214
|
...props.labels,
|
|
209
215
|
...(typeof labels === 'object' ? labels : null),
|
|
210
|
-
class: cls(
|
|
211
|
-
'stroke-surface-200 transition-opacity',
|
|
212
|
-
seriesState.highlightKey.current &&
|
|
213
|
-
seriesState.highlightKey.current !== s.key &&
|
|
214
|
-
'opacity-10',
|
|
215
|
-
props.labels?.class,
|
|
216
|
-
typeof labels === 'object' && labels.class
|
|
217
|
-
),
|
|
216
|
+
class: cls(props.labels?.class, typeof labels === 'object' && labels.class),
|
|
218
217
|
};
|
|
219
218
|
|
|
220
219
|
return labelsProps;
|
|
221
220
|
}
|
|
222
221
|
|
|
223
|
-
const highlightPointsProps = $derived(
|
|
224
|
-
typeof props.highlight?.points === 'object' ? props.highlight.points : null
|
|
225
|
-
);
|
|
226
|
-
|
|
227
222
|
function getHighlightProps(
|
|
228
223
|
s: SeriesData<TData, typeof Spline>,
|
|
229
224
|
i: number
|
|
@@ -231,18 +226,22 @@
|
|
|
231
226
|
if (!context || !context.tooltip.data) return {};
|
|
232
227
|
const seriesTooltipData =
|
|
233
228
|
s.data && context.tooltip.data
|
|
234
|
-
? findRelatedData(s.data, context.tooltip.data, context.x)
|
|
229
|
+
? (findRelatedData(s.data, context.tooltip.data, context.x) ?? {})
|
|
235
230
|
: null;
|
|
231
|
+
const highlightPointsProps =
|
|
232
|
+
typeof props.highlight?.points === 'object' ? props.highlight.points : null;
|
|
236
233
|
|
|
237
234
|
return {
|
|
238
235
|
data: seriesTooltipData,
|
|
239
|
-
|
|
236
|
+
x: isVertical ? (s.value ?? (s.data ? undefined : s.key)) : undefined,
|
|
237
|
+
y: !isVertical ? (s.value ?? (s.data ? undefined : s.key)) : undefined,
|
|
240
238
|
lines: i === 0,
|
|
241
239
|
onPointClick: onPointClick
|
|
242
240
|
? (e, detail) => onPointClick(e, { ...detail, series: s })
|
|
243
241
|
: undefined,
|
|
244
242
|
onPointEnter: () => (seriesState.highlightKey.current = s.key),
|
|
245
243
|
onPointLeave: () => (seriesState.highlightKey.current = null),
|
|
244
|
+
opacity: seriesState.isHighlighted(s.key, true) ? 1 : 0.1,
|
|
246
245
|
...props.highlight,
|
|
247
246
|
points:
|
|
248
247
|
props.highlight?.points == false
|
|
@@ -250,13 +249,6 @@
|
|
|
250
249
|
: {
|
|
251
250
|
...highlightPointsProps,
|
|
252
251
|
fill: s.color,
|
|
253
|
-
class: cls(
|
|
254
|
-
'transition-opacity',
|
|
255
|
-
seriesState.highlightKey.current &&
|
|
256
|
-
seriesState.highlightKey.current !== s.key &&
|
|
257
|
-
'opacity-10',
|
|
258
|
-
highlightPointsProps?.class
|
|
259
|
-
),
|
|
260
252
|
},
|
|
261
253
|
};
|
|
262
254
|
}
|
|
@@ -265,8 +257,8 @@
|
|
|
265
257
|
return createLegendProps({
|
|
266
258
|
seriesState,
|
|
267
259
|
props: {
|
|
268
|
-
...props.legend,
|
|
269
260
|
...(typeof legend === 'object' ? legend : null),
|
|
261
|
+
...props.legend,
|
|
270
262
|
},
|
|
271
263
|
});
|
|
272
264
|
}
|
|
@@ -284,14 +276,12 @@
|
|
|
284
276
|
if (axisDirection === 'y') {
|
|
285
277
|
return {
|
|
286
278
|
placement: radial ? 'radius' : 'left',
|
|
287
|
-
format: (value) => format(value, undefined, { variant: 'short' }),
|
|
288
279
|
...(typeof axis === 'object' ? axis : null),
|
|
289
280
|
...props.yAxis,
|
|
290
281
|
};
|
|
291
282
|
}
|
|
292
283
|
return {
|
|
293
284
|
placement: radial ? 'angle' : 'bottom',
|
|
294
|
-
format: (value) => format(value, undefined, { variant: 'short' }),
|
|
295
285
|
...(typeof axis === 'object' ? axis : null),
|
|
296
286
|
...props.xAxis,
|
|
297
287
|
};
|
|
@@ -327,22 +317,26 @@
|
|
|
327
317
|
<Chart
|
|
328
318
|
bind:context
|
|
329
319
|
data={chartData}
|
|
330
|
-
x={xProp}
|
|
331
|
-
{xDomain}
|
|
332
320
|
{xScale}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
321
|
+
x={xProp ?? (isVertical ? series.map((s) => s.value ?? s.key) : undefined)}
|
|
322
|
+
{xDomain}
|
|
323
|
+
xBaseline={!isVertical || (xScale && isScaleTime(xScale)) ? undefined : 0}
|
|
324
|
+
xNice={orientation === 'vertical'}
|
|
325
|
+
{yScale}
|
|
326
|
+
y={yProp ?? (isVertical ? undefined : series.map((s) => s.value ?? s.key))}
|
|
327
|
+
yBaseline={isVertical || (yScale && isScaleTime(yScale)) ? undefined : 0}
|
|
328
|
+
yNice={orientation === 'horizontal'}
|
|
336
329
|
{radial}
|
|
337
330
|
padding={radial ? undefined : defaultChartPadding(axis, legend)}
|
|
338
331
|
{...restProps}
|
|
339
332
|
tooltip={tooltip === false
|
|
340
333
|
? false
|
|
341
334
|
: {
|
|
342
|
-
mode: '
|
|
335
|
+
mode: isVertical ? 'quadtree-y' : 'quadtree-x',
|
|
343
336
|
onclick: onTooltipClick,
|
|
344
337
|
debug,
|
|
345
338
|
...props.tooltip?.context,
|
|
339
|
+
...(typeof tooltip === 'object' ? tooltip : null),
|
|
346
340
|
}}
|
|
347
341
|
brush={brush && (brush === true || brush.mode == undefined || brush.mode === 'integrated')
|
|
348
342
|
? {
|
|
@@ -10,12 +10,11 @@ export type LineChartExtraSnippetProps<TData> = {
|
|
|
10
10
|
export type LineChartPropsObjProp = Pick<SimplifiedChartPropsObject, 'brush' | 'canvas' | 'grid' | 'highlight' | 'labels' | 'legend' | 'points' | 'rule' | 'spline' | 'svg' | 'tooltip' | 'xAxis' | 'yAxis'>;
|
|
11
11
|
export type LineChartProps<TData> = SimplifiedChartProps<TData, typeof Spline, LineChartExtraSnippetProps<TData>> & {
|
|
12
12
|
/**
|
|
13
|
-
* The
|
|
13
|
+
* The orientation of the line chart.
|
|
14
|
+
*
|
|
15
|
+
* @default 'horizontal'
|
|
14
16
|
*/
|
|
15
|
-
|
|
16
|
-
data: HighlightPointData;
|
|
17
|
-
series: SeriesData<TData, typeof Spline>;
|
|
18
|
-
}) => void;
|
|
17
|
+
orientation?: 'vertical' | 'horizontal';
|
|
19
18
|
props?: LineChartPropsObjProp;
|
|
20
19
|
spline?: SimplifiedChartSnippet<TData, typeof Spline, LineChartExtraSnippetProps<TData> & {
|
|
21
20
|
props: ComponentProps<typeof Spline>;
|
|
@@ -24,6 +23,13 @@ export type LineChartProps<TData> = SimplifiedChartProps<TData, typeof Spline, L
|
|
|
24
23
|
*/
|
|
25
24
|
seriesIndex: number;
|
|
26
25
|
}>;
|
|
26
|
+
/**
|
|
27
|
+
* The event to be dispatched when the point is clicked.
|
|
28
|
+
*/
|
|
29
|
+
onPointClick?: (e: MouseEvent, details: {
|
|
30
|
+
data: HighlightPointData;
|
|
31
|
+
series: SeriesData<TData, typeof Spline>;
|
|
32
|
+
}) => void;
|
|
27
33
|
};
|
|
28
34
|
import { type ComponentProps } from 'svelte';
|
|
29
35
|
import Axis from '../Axis.svelte';
|
|
@@ -34,10 +40,17 @@ import Points from '../Points.svelte';
|
|
|
34
40
|
import Rule from '../Rule.svelte';
|
|
35
41
|
import Spline from '../Spline.svelte';
|
|
36
42
|
import type { SeriesData, SimplifiedChartProps, SimplifiedChartPropsObject, SimplifiedChartSnippet } from './types.js';
|
|
43
|
+
declare function $$render<TData>(): {
|
|
44
|
+
props: LineChartProps<TData>;
|
|
45
|
+
exports: {};
|
|
46
|
+
bindings: "context";
|
|
47
|
+
slots: {};
|
|
48
|
+
events: {};
|
|
49
|
+
};
|
|
37
50
|
declare class __sveltets_Render<TData> {
|
|
38
|
-
props():
|
|
39
|
-
events():
|
|
40
|
-
slots():
|
|
51
|
+
props(): ReturnType<typeof $$render<TData>>['props'];
|
|
52
|
+
events(): ReturnType<typeof $$render<TData>>['events'];
|
|
53
|
+
slots(): ReturnType<typeof $$render<TData>>['slots'];
|
|
41
54
|
bindings(): "context";
|
|
42
55
|
exports(): {};
|
|
43
56
|
}
|