layerchart 0.43.2 → 0.43.4

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.
@@ -8,10 +8,19 @@ import Bar from './Bar.svelte';
8
8
  import Rect from './Rect.svelte';
9
9
  import { tooltipContext } from './TooltipContext.svelte';
10
10
  import { isScaleBand } from '../utils/scales.js';
11
- const { data: contextData, flatData, x, xDomain, xScale, xRange, xGet, y, yDomain, yScale, yRange, yGet, rGet, config, } = chartContext();
11
+ import { accessor } from '../utils/common.js';
12
+ const { data: contextData, flatData, x: xContext, xDomain, xScale, xRange, y: yContext, yDomain, yScale, yRange, rGet, config, } = chartContext();
12
13
  const tooltip = tooltipContext();
13
14
  /** Highlight specific data (annotate), espect uses tooltip data */
14
15
  export let data = undefined;
16
+ /**
17
+ * Override `x` from context
18
+ */
19
+ export let x = $xContext;
20
+ /**
21
+ * Override `y` from context
22
+ */
23
+ export let y = $yContext;
15
24
  export let axis = undefined;
16
25
  /** Show points and pass props to Circles */
17
26
  export let points = false;
@@ -24,6 +33,8 @@ export let bar = false;
24
33
  /** Set to false to disable spring transitions */
25
34
  export let motion = true;
26
35
  // TODO: Fix circle points being backwards for stack (see AreaStack)
36
+ const _x = accessor(x);
37
+ const _y = accessor(y);
27
38
  let _points = [];
28
39
  let _lines = [];
29
40
  let _area = {
@@ -34,10 +45,12 @@ let _area = {
34
45
  };
35
46
  $: highlightData = data ?? $tooltip.data;
36
47
  $: if (highlightData) {
37
- let xCoord = $xGet(highlightData);
38
- let xOffset = isScaleBand($xScale) ? $xScale.bandwidth() / 2 : 0;
39
- let yCoord = $yGet(highlightData);
40
- let yOffset = isScaleBand($yScale) ? $yScale.bandwidth() / 2 : 0;
48
+ const xValue = _x(highlightData);
49
+ const xCoord = Array.isArray(xValue) ? xValue.map((v) => $xScale(v)) : $xScale(xValue);
50
+ const xOffset = isScaleBand($xScale) ? $xScale.bandwidth() / 2 : 0;
51
+ const yValue = _y(highlightData);
52
+ const yCoord = Array.isArray(yValue) ? yValue.map((v) => $yScale(v)) : $yScale(yValue);
53
+ const yOffset = isScaleBand($yScale) ? $yScale.bandwidth() / 2 : 0;
41
54
  // Reset lines
42
55
  _lines = [];
43
56
  const defaultAxis = isScaleBand($yScale) ? 'y' : 'x';
@@ -79,9 +92,9 @@ $: if (highlightData) {
79
92
  }
80
93
  else {
81
94
  // Find width to next data point
82
- const index = $flatData.findIndex((d) => Number($x(d)) === Number($x(highlightData)));
95
+ const index = $flatData.findIndex((d) => Number(_x(d)) === Number(_x(highlightData)));
83
96
  const isLastPoint = index + 1 === $flatData.length;
84
- const nextDataPoint = isLastPoint ? max($xDomain) : $x($flatData[index + 1]);
97
+ const nextDataPoint = isLastPoint ? max($xDomain) : _x($flatData[index + 1]);
85
98
  _area.width = ($xScale(nextDataPoint) ?? 0) - (xCoord ?? 0);
86
99
  }
87
100
  // If array, use left-most value for top left of rect
@@ -127,9 +140,9 @@ $: if (highlightData) {
127
140
  }
128
141
  else {
129
142
  // Find width to next data point
130
- const index = $flatData.findIndex((d) => Number($x(d)) === Number($x(highlightData)));
143
+ const index = $flatData.findIndex((d) => Number(_x(d)) === Number(_x(highlightData)));
131
144
  const isLastPoint = index + 1 === $flatData.length;
132
- const nextDataPoint = isLastPoint ? max($yDomain) : $x($flatData[index + 1]);
145
+ const nextDataPoint = isLastPoint ? max($yDomain) : _x($flatData[index + 1]);
133
146
  _area.height = ($yScale(nextDataPoint) ?? 0) - (yCoord ?? 0);
134
147
  }
135
148
  // If array, use left-most value for top left of rect
@@ -142,38 +155,80 @@ $: if (highlightData) {
142
155
  }
143
156
  // points
144
157
  if (Array.isArray(xCoord)) {
145
- // `x` accessor with multiple properties (ex. `x={['start', 'end']})`)
146
- _points = xCoord.filter(notNull).map((xItem, i) => ({
147
- x: xItem + xOffset,
148
- y: $yGet(highlightData) + yOffset,
149
- fill: $config.r ? $rGet(highlightData) : null,
150
- }));
158
+ // `x` accessor with multiple properties (ex. `x={['start', 'end']}` or `x={[0, 1]}`)
159
+ if (Array.isArray(highlightData)) {
160
+ // Stack series (ex. `y={[['apples', 'bananas', 'oranges']]})`)
161
+ // `highlightData` is a single stack layer/point, which is an 2 element array with an extra `data` property `[number, number, data: any]`.
162
+ const highlightSeriesPoint = highlightData;
163
+ // Ignore non-array data such as hierarchy and graph (make Typescript happy)
164
+ if (Array.isArray($contextData)) {
165
+ // For each series, find the related data point
166
+ const seriesPointsData = $contextData.map((series) => {
167
+ return {
168
+ series,
169
+ point: series.find((d) => _y(d) === _y(highlightSeriesPoint)),
170
+ };
171
+ });
172
+ _points = seriesPointsData.map((seriesPoint, i) => ({
173
+ x: $xScale(seriesPoint.point[1]) + xOffset,
174
+ y: yCoord + yOffset,
175
+ fill: $config.r ? $rGet(seriesPoint.series) : null,
176
+ }));
177
+ }
178
+ }
179
+ else {
180
+ // Multi series / etc (ex. `y={['apples', 'bananas', 'oranges']}`)
181
+ _points = xCoord.filter(notNull).map((xItem, i) => {
182
+ const $key = $config.x[i];
183
+ return {
184
+ x: xItem + xOffset,
185
+ y: yCoord + yOffset,
186
+ // TODO: is there a better way to expose the series key/value?
187
+ fill: $config.r ? $rGet({ ...highlightData, $key }) : null,
188
+ };
189
+ });
190
+ }
151
191
  }
152
- else if (Array.isArray(highlightData)) {
153
- // Stack series
154
- // `highlightData` is a single stack layer/point, which is an 2 element array with an extra `data` property `[number, number, data: any]`.
155
- const highlightSeriesPoint = highlightData;
156
- // Ignore non-array data such as hierarchy and graph (make Typescript happy)
157
- if (Array.isArray($contextData)) {
158
- // For each series, find the related data point
159
- const seriesPointsData = $contextData.map((series) => {
192
+ else if (Array.isArray(yCoord)) {
193
+ // `y` accessor with multiple properties (ex. `y={['apples', 'bananas', 'oranges']}` or `y={[0, 1]})
194
+ if (Array.isArray(highlightData)) {
195
+ // Stack series (ex. `y={[['apples', 'bananas', 'oranges']]})`)
196
+ // `highlightData` is a single stack layer/point, which is an 2 element array with an extra `data` property `[number, number, data: any]`.
197
+ const highlightSeriesPoint = highlightData;
198
+ // Ignore non-array data such as hierarchy and graph (make Typescript happy)
199
+ if (Array.isArray($contextData)) {
200
+ // For each series, find the related data point
201
+ const seriesPointsData = $contextData.map((series) => {
202
+ return {
203
+ series,
204
+ point: series.find((d) => _x(d) === _x(highlightSeriesPoint)),
205
+ };
206
+ });
207
+ _points = seriesPointsData.map((seriesPoint, i) => ({
208
+ x: xCoord + xOffset,
209
+ y: $yScale(seriesPoint.point[1]) + yOffset,
210
+ fill: $config.r ? $rGet(seriesPoint.series) : null,
211
+ }));
212
+ }
213
+ }
214
+ else {
215
+ // Multi series / etc (ex. `y={['apples', 'bananas', 'oranges']}`)
216
+ _points = yCoord.filter(notNull).map((yItem, i) => {
217
+ const $key = $config.y[i];
160
218
  return {
161
- series,
162
- point: series.find((d) => $x(d) === $x(highlightSeriesPoint)),
219
+ x: xCoord + xOffset,
220
+ y: yItem + yOffset,
221
+ // TODO: is there a better way to expose the series key/value?
222
+ fill: $config.r ? $rGet({ ...highlightData, $key }) : null,
163
223
  };
164
224
  });
165
- _points = seriesPointsData.map((seriesPoint, i) => ({
166
- x: xCoord + xOffset,
167
- y: $yScale(seriesPoint.point[1]) + yOffset,
168
- fill: $config.r ? $rGet(seriesPoint.series) : null,
169
- }));
170
225
  }
171
226
  }
172
227
  else {
173
228
  _points = [
174
229
  {
175
230
  x: xCoord + xOffset,
176
- y: $yGet(highlightData) + yOffset,
231
+ y: yCoord + yOffset,
177
232
  fill: $config.r ? $rGet(highlightData) : null,
178
233
  },
179
234
  ];
@@ -250,7 +305,7 @@ $: if (highlightData) {
250
305
  {...typeof points === 'object' ? points : null}
251
306
  class={cls(
252
307
  'stroke-[6] stroke-white [paint-order:stroke] drop-shadow',
253
- !point.fill && 'fill-primary',
308
+ !point.fill && typeof points === 'object' && !points.fill && 'fill-primary',
254
309
  typeof points === 'object' ? points.class : null
255
310
  )}
256
311
  />
@@ -4,9 +4,16 @@ import Circle from './Circle.svelte';
4
4
  import Line from './Line.svelte';
5
5
  import Bar from './Bar.svelte';
6
6
  import Rect from './Rect.svelte';
7
+ import { type Accessor } from '../utils/common.js';
7
8
  declare const __propDef: {
8
9
  props: {
9
10
  /** Highlight specific data (annotate), espect uses tooltip data */ data?: any;
11
+ /**
12
+ * Override `x` from context
13
+ */ x?: Accessor;
14
+ /**
15
+ * Override `y` from context
16
+ */ y?: Accessor;
10
17
  axis?: "x" | "y" | "both" | "none" | undefined;
11
18
  /** Show points and pass props to Circles */ points?: boolean | Partial<ComponentProps<Circle>>;
12
19
  /** Show lines and pass props to Lines */ lines?: boolean | Partial<ComponentProps<Line>>;
@@ -32,8 +39,8 @@ declare const __propDef: {
32
39
  bar: boolean | Partial<{
33
40
  [x: string]: any;
34
41
  bar: Object;
35
- x?: import("..").Accessor;
36
- y?: import("..").Accessor;
42
+ x?: Accessor;
43
+ y?: Accessor;
37
44
  fill?: string | undefined;
38
45
  stroke?: string;
39
46
  strokeWidth?: number;
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.43.2",
7
+ "version": "0.43.4",
8
8
  "devDependencies": {
9
9
  "@changesets/cli": "^2.27.5",
10
10
  "@mdi/js": "^7.4.47",