layerchart 0.79.2 → 0.79.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.
@@ -11,9 +11,17 @@
11
11
 
12
12
  const { xScale, yScale } = chartContext();
13
13
 
14
+ /** Override data instead of using context */
15
+ export let data: any = undefined;
16
+
14
17
  /** Override display value accessor. By default, uses `y` unless yScale is band scale */
15
18
  export let value: Accessor = undefined;
16
19
 
20
+ /** Override `x` accessor from Chart context */
21
+ export let x: Accessor = undefined;
22
+ /** Override `y` accessor from Chart context */
23
+ export let y: Accessor = undefined;
24
+
17
25
  export let placement: 'inside' | 'outside' | 'center' = 'outside';
18
26
  export let offset = placement === 'center' ? 0 : 4;
19
27
  export let format: FormatType | undefined = undefined;
@@ -90,19 +98,21 @@
90
98
  </script>
91
99
 
92
100
  <g class="Labels">
93
- <Points let:points>
101
+ <Points {data} {x} {y} let:points>
94
102
  {#each points as point, i (key(point.data, i))}
95
103
  {@const textProps = getTextProps(point)}
96
104
  <slot data={point} {textProps}>
97
105
  <Text
106
+ {...textProps}
107
+ {...$$restProps}
98
108
  class={cls(
99
109
  'text-xs',
100
110
  placement === 'inside'
101
111
  ? 'fill-surface-300 stroke-surface-content'
102
- : 'fill-surface-content stroke-surface-100'
112
+ : 'fill-surface-content stroke-surface-100',
113
+ textProps.class,
114
+ $$props.class
103
115
  )}
104
- {...textProps}
105
- {...$$restProps}
106
116
  />
107
117
  </slot>
108
118
  {/each}
@@ -5,7 +5,10 @@ import { type Accessor } from '../utils/common.js';
5
5
  declare const __propDef: {
6
6
  props: {
7
7
  [x: string]: any;
8
+ data?: any;
8
9
  value?: Accessor;
10
+ x?: Accessor;
11
+ y?: Accessor;
9
12
  placement?: "inside" | "outside" | "center" | undefined;
10
13
  offset?: number | undefined;
11
14
  format?: FormatType | undefined;
@@ -13,14 +13,15 @@
13
13
  import Link from './Link.svelte';
14
14
  import { isScaleBand, type AnyScale } from '../utils/scales.js';
15
15
  import { getCanvasContext } from './layout/Canvas.svelte';
16
+ import { accessor, type Accessor } from '../utils/common.js';
16
17
 
17
18
  const context = chartContext() as any;
18
19
  const {
19
20
  data: contextData,
20
- x,
21
+ x: contextX,
21
22
  xScale,
22
23
  xGet,
23
- y,
24
+ y: contextY,
24
25
  yScale,
25
26
  yGet,
26
27
  cGet,
@@ -34,6 +35,11 @@
34
35
  /** Override data instead of using context */
35
36
  export let data: any = undefined;
36
37
 
38
+ /** Override `x` accessor from Chart context */
39
+ export let x: Accessor = undefined;
40
+ /** Override `y` accessor from Chart context */
41
+ export let y: Accessor = undefined;
42
+
37
43
  export let r = 5;
38
44
  export let offsetX: Offset = undefined;
39
45
  export let offsetY: Offset = undefined;
@@ -65,12 +71,15 @@
65
71
  }
66
72
  }
67
73
 
74
+ $: xAccessor = x ? accessor(x) : $contextX;
75
+ $: yAccessor = y ? accessor(y) : $contextY;
76
+
68
77
  $: pointsData = data ?? $contextData;
69
78
 
70
79
  $: points = pointsData
71
80
  .flatMap((d: any) => {
72
- const xValue = $x(d);
73
- const yValue = $y(d);
81
+ const xValue = xAccessor(d);
82
+ const yValue = yAccessor(d);
74
83
 
75
84
  if (Array.isArray(xValue)) {
76
85
  /*
@@ -120,8 +129,8 @@
120
129
  .filter((p: Point) => p) as Point[];
121
130
 
122
131
  $: _links = pointsData.flatMap((d: any) => {
123
- const xValue = $x(d);
124
- const yValue = $y(d);
132
+ const xValue = xAccessor(d);
133
+ const yValue = yAccessor(d);
125
134
 
126
135
  if (Array.isArray(xValue)) {
127
136
  /*
@@ -9,10 +9,13 @@ export type Point = {
9
9
  };
10
10
  import { type ComponentProps } from 'svelte';
11
11
  import Link from './Link.svelte';
12
+ import { type Accessor } from '../utils/common.js';
12
13
  declare const __propDef: {
13
14
  props: {
14
15
  [x: string]: any;
15
16
  data?: any;
17
+ x?: Accessor;
18
+ y?: Accessor;
16
19
  r?: number | undefined;
17
20
  offsetX?: number | ((value: number, context: any) => number) | undefined;
18
21
  offsetY?: number | ((value: number, context: any) => number) | undefined;
@@ -187,6 +187,49 @@
187
187
  return areaProps;
188
188
  }
189
189
 
190
+ function getPointsProps(s: (typeof series)[number], i: number) {
191
+ const pointsProps: ComponentProps<Points> = {
192
+ data: s.data,
193
+ y: stackSeries
194
+ ? (d) => d.stackData[i][1]
195
+ : Array.isArray(s.value)
196
+ ? s.value[1]
197
+ : (s.value ?? (s.data ? undefined : s.key)),
198
+ fill: s.color,
199
+ ...props.points,
200
+ ...(typeof points === 'object' ? points : null),
201
+ class: cls(
202
+ 'stroke-surface-200 transition-opacity',
203
+ highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
204
+ props.points?.class,
205
+ typeof points === 'object' && points.class
206
+ ),
207
+ };
208
+
209
+ return pointsProps;
210
+ }
211
+
212
+ function getLabelsProps(s: (typeof series)[number], i: number) {
213
+ const labelsProps: ComponentProps<Labels> = {
214
+ data: s.data,
215
+ y: stackSeries
216
+ ? (d) => d.stackData[i][1]
217
+ : Array.isArray(s.value)
218
+ ? s.value[1]
219
+ : (s.value ?? (s.data ? undefined : s.key)),
220
+ ...props.labels,
221
+ ...(typeof labels === 'object' ? labels : null),
222
+ class: cls(
223
+ 'stroke-surface-200 transition-opacity',
224
+ highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
225
+ props.labels?.class,
226
+ typeof labels === 'object' && labels.class
227
+ ),
228
+ };
229
+
230
+ return labelsProps;
231
+ }
232
+
190
233
  const selectedSeries = selectionStore();
191
234
  $: visibleSeries = series.filter((s) => {
192
235
  return (
@@ -243,6 +286,8 @@
243
286
  series,
244
287
  visibleSeries,
245
288
  getAreaProps,
289
+ getLabelsProps,
290
+ getPointsProps,
246
291
  }}
247
292
 
248
293
  <slot {...slotProps}>
@@ -296,14 +341,8 @@
296
341
  </slot>
297
342
 
298
343
  {#if points}
299
- {#each visibleSeries as s}
300
- <Points
301
- data={s.data}
302
- fill={s.color}
303
- class="stroke-surface-200"
304
- {...props.points}
305
- {...typeof points === 'object' ? points : null}
306
- />
344
+ {#each visibleSeries as s, i (s.key)}
345
+ <Points {...getPointsProps(s, i)} />
307
346
  {/each}
308
347
  {/if}
309
348
 
@@ -326,7 +365,9 @@
326
365
  </slot>
327
366
 
328
367
  {#if labels}
329
- <Labels {...props.labels} {...typeof labels === 'object' ? labels : null} />
368
+ {#each visibleSeries as s, i (s.key)}
369
+ <Labels {...getLabelsProps(s, i)} />
370
+ {/each}
330
371
  {/if}
331
372
  </svelte:component>
332
373
 
@@ -163,7 +163,6 @@
163
163
 
164
164
  $: if (stackSeries) {
165
165
  const seriesKeys = visibleSeries.map((s) => s.key);
166
- // const stackData = stack().keys(seriesKeys)(chartDataArray(data)) as any[];
167
166
 
168
167
  const offset =
169
168
  seriesLayout === 'stackExpand'
@@ -240,6 +239,24 @@
240
239
  return barsProps;
241
240
  }
242
241
 
242
+ function getLabelsProps(s: (typeof series)[number], i: number) {
243
+ const labelsProps: ComponentProps<Labels> = {
244
+ // TODO: Improve placement when using `seriesLayout="group"`
245
+ // data: s.data,
246
+ // y: s.value ?? (s.data ? undefined : s.key),
247
+ ...props.labels,
248
+ ...(typeof labels === 'object' ? labels : null),
249
+ class: cls(
250
+ 'stroke-surface-200 transition-opacity',
251
+ highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
252
+ props.labels?.class,
253
+ typeof labels === 'object' && labels.class
254
+ ),
255
+ };
256
+
257
+ return labelsProps;
258
+ }
259
+
243
260
  const selectedSeries = selectionStore();
244
261
  $: visibleSeries = series.filter((s) => {
245
262
  return (
@@ -309,6 +326,7 @@
309
326
  series,
310
327
  visibleSeries,
311
328
  getBarsProps,
329
+ getLabelsProps,
312
330
  }}
313
331
  <slot {...slotProps}>
314
332
  <svelte:component this={renderContext === 'canvas' ? Canvas : Svg}>
@@ -381,7 +399,9 @@
381
399
  </slot>
382
400
 
383
401
  {#if labels}
384
- <Labels {...props.labels} {...typeof labels === 'object' ? labels : null} />
402
+ {#each visibleSeries as s, i (s.key)}
403
+ <Labels {...getLabelsProps(s, i)} />
404
+ {/each}
385
405
  {/if}
386
406
  </svelte:component>
387
407
 
@@ -132,6 +132,41 @@
132
132
  return splineProps;
133
133
  }
134
134
 
135
+ function getPointsProps(s: (typeof series)[number], i: number) {
136
+ const pointsProps: ComponentProps<Points> = {
137
+ data: s.data,
138
+ y: s.value ?? (s.data ? undefined : s.key),
139
+ fill: s.color,
140
+ ...props.points,
141
+ ...(typeof points === 'object' ? points : null),
142
+ class: cls(
143
+ 'stroke-surface-200 transition-opacity',
144
+ highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
145
+ props.points?.class,
146
+ typeof points === 'object' && points.class
147
+ ),
148
+ };
149
+
150
+ return pointsProps;
151
+ }
152
+
153
+ function getLabelsProps(s: (typeof series)[number], i: number) {
154
+ const labelsProps: ComponentProps<Labels> = {
155
+ data: s.data,
156
+ y: s.value ?? (s.data ? undefined : s.key),
157
+ ...props.labels,
158
+ ...(typeof labels === 'object' ? labels : null),
159
+ class: cls(
160
+ 'stroke-surface-200 transition-opacity',
161
+ highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
162
+ props.labels?.class,
163
+ typeof labels === 'object' && labels.class
164
+ ),
165
+ };
166
+
167
+ return labelsProps;
168
+ }
169
+
135
170
  const selectedSeries = selectionStore();
136
171
  $: visibleSeries = series.filter((s) => {
137
172
  return (
@@ -184,6 +219,8 @@
184
219
  tooltip,
185
220
  series,
186
221
  visibleSeries,
222
+ getLabelsProps,
223
+ getPointsProps,
187
224
  getSplineProps,
188
225
  }}
189
226
  <slot {...slotProps}>
@@ -231,19 +268,15 @@
231
268
  </slot>
232
269
 
233
270
  {#if points}
234
- {#each visibleSeries as s}
235
- <Points
236
- data={s.data}
237
- fill={s.color}
238
- class="stroke-surface-200"
239
- {...props.points}
240
- {...typeof points === 'object' ? points : null}
241
- />
271
+ {#each visibleSeries as s, i (s.key)}
272
+ <Points {...getPointsProps(s, i)} />
242
273
  {/each}
243
274
  {/if}
244
275
 
245
276
  {#if labels}
246
- <Labels {...props.labels} {...typeof labels === 'object' ? labels : null} />
277
+ {#each visibleSeries as s, i (s.key)}
278
+ <Labels {...getLabelsProps(s, i)} />
279
+ {/each}
247
280
  {/if}
248
281
 
249
282
  <slot name="highlight" {...slotProps}>
@@ -116,6 +116,22 @@
116
116
  return pointsProps;
117
117
  }
118
118
 
119
+ function getLabelsProps(s: (typeof series)[number], i: number) {
120
+ const labelsProps: ComponentProps<Labels> = {
121
+ data: s.data,
122
+ ...props.labels,
123
+ ...(typeof labels === 'object' ? labels : null),
124
+ class: cls(
125
+ 'stroke-surface-200 transition-opacity',
126
+ highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
127
+ props.labels?.class,
128
+ typeof labels === 'object' && labels.class
129
+ ),
130
+ };
131
+
132
+ return labelsProps;
133
+ }
134
+
119
135
  const selectedSeries = selectionStore();
120
136
  $: visibleSeries = series.filter((s) => {
121
137
  return (
@@ -169,6 +185,7 @@
169
185
  tooltip,
170
186
  series,
171
187
  visibleSeries,
188
+ getLabelsProps,
172
189
  getPointsProps,
173
190
  }}
174
191
  {@const activeSeries = tooltip.data
@@ -224,11 +241,9 @@
224
241
  </slot>
225
242
 
226
243
  {#if labels}
227
- <Labels
228
- format={(value) => format(value)}
229
- {...props.highlight}
230
- {...typeof labels === 'object' ? labels : null}
231
- />
244
+ {#each visibleSeries as s, i (s.key)}
245
+ <Labels {...getLabelsProps(s, i)} />
246
+ {/each}
232
247
  {/if}
233
248
  </svelte:component>
234
249
 
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "author": "Sean Lynch <techniq35@gmail.com>",
5
5
  "license": "MIT",
6
6
  "repository": "techniq/layerchart",
7
- "version": "0.79.2",
7
+ "version": "0.79.3",
8
8
  "devDependencies": {
9
9
  "@changesets/cli": "^2.27.12",
10
10
  "@mdi/js": "^7.4.47",