layerchart 1.0.2 → 1.0.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.
@@ -17,6 +17,7 @@
17
17
  import Spline from './Spline.svelte';
18
18
  import { accessor, type Accessor } from '../utils/common.js';
19
19
  import { isScaleBand } from '../utils/scales.js';
20
+ import { flattenPathData } from '../utils/path.js';
20
21
  import { getCanvasContext } from './layout/Canvas.svelte';
21
22
  import { renderPathData, type ComputedStylesOptions } from '../utils/canvas.js';
22
23
  const {
@@ -78,24 +79,33 @@
78
79
 
79
80
  /** Provide initial `0` horizontal baseline and initially hide/untrack scale changes so not reactive (only set on initial mount) */
80
81
  function defaultPathData() {
81
- const path = $radial
82
- ? areaRadial()
83
- .angle((d) => $xScale(xAccessor(d)))
84
- .innerRadius((d) => Math.min($yScale(0), $yRange[0]))
85
- .outerRadius((d) => Math.min($yScale(0), $yRange[0]))
86
- : d3Area()
87
- .x((d) => $xScale(xAccessor(d)) + xOffset)
88
- .y0((d) => Math.min($yScale(0), $yRange[0]))
89
- .y1((d) => Math.min($yScale(0), $yRange[0]));
90
-
91
- path.defined(defined ?? ((d) => xAccessor(d) != null && y1Accessor(d) != null));
92
-
93
- if (curve) path.curve(curve);
94
-
95
- return path(data ?? $contextData);
82
+ if (!tweenedOptions) {
83
+ // If not tweened, return empty string (faster initial render)
84
+ return '';
85
+ } else if (pathData) {
86
+ // Flatten all `y` coordinates of pre-defined `pathData`
87
+ return flattenPathData(pathData, Math.min($yScale(0), $yRange[0]));
88
+ } else if ($config.x) {
89
+ // Only use default line if `x` accessor is defined (cartesian chart)
90
+ const path = $radial
91
+ ? areaRadial()
92
+ .angle((d) => $xScale(xAccessor(d)))
93
+ .innerRadius((d) => Math.min($yScale(0), $yRange[0]))
94
+ .outerRadius((d) => Math.min($yScale(0), $yRange[0]))
95
+ : d3Area()
96
+ .x((d) => $xScale(xAccessor(d)) + xOffset)
97
+ .y0((d) => Math.min($yScale(0), $yRange[0]))
98
+ .y1((d) => Math.min($yScale(0), $yRange[0]));
99
+
100
+ path.defined(defined ?? ((d) => xAccessor(d) != null && y1Accessor(d) != null));
101
+
102
+ if (curve) path.curve(curve);
103
+
104
+ return path(data ?? $contextData);
105
+ }
96
106
  }
97
107
 
98
- $: tweenedOptions = tweened
108
+ const tweenedOptions = tweened
99
109
  ? { interpolate: interpolatePath, ...(typeof tweened === 'object' ? tweened : null) }
100
110
  : false;
101
111
  $: tweened_d = motionStore(defaultPathData(), { tweened: tweenedOptions });
@@ -65,8 +65,9 @@
65
65
  $: markerEndId = markerEnd || $$slots['markerEnd'] ? uniqueId('marker-') : '';
66
66
 
67
67
  export let tweened: boolean | Parameters<typeof tweenedStore>[1] = undefined;
68
- // @ts-expect-error
69
- $: tweenedOptions = tweened ? { interpolate: interpolatePath, ...tweened } : false;
68
+ const tweenedOptions = tweened
69
+ ? { interpolate: interpolatePath, ...(typeof tweened === 'object' ? tweened : null) }
70
+ : false;
70
71
  $: tweened_d = motionStore('', { tweened: tweenedOptions });
71
72
 
72
73
  $: {
@@ -143,6 +143,7 @@
143
143
  x: xMax + getOffset(xMax, offsetX, $xScale) - ($config.r ? $rGet(d) : r),
144
144
  y: y,
145
145
  },
146
+ data: d,
146
147
  };
147
148
  } else if (Array.isArray(yValue)) {
148
149
  /*
@@ -160,6 +161,7 @@
160
161
  x: x,
161
162
  y: yMax + getOffset(yMax, offsetY, $yScale),
162
163
  },
164
+ data: d,
163
165
  };
164
166
  }
165
167
  });
@@ -170,7 +172,7 @@
170
172
  {#each _links as link}
171
173
  <Link
172
174
  data={link}
173
- class="stroke-surface-content/50"
175
+ stroke={fill ?? ($config.c ? $cGet(link.data) : null)}
174
176
  {...typeof links === 'object' ? links : null}
175
177
  />
176
178
  {/each}
@@ -149,8 +149,9 @@
149
149
  }
150
150
 
151
151
  let d: string | null = '';
152
- // @ts-expect-error
153
- $: tweenedOptions = tweened ? { interpolate: interpolatePath, ...tweened } : false;
152
+ const tweenedOptions = tweened
153
+ ? { interpolate: interpolatePath, ...(typeof tweened === 'object' ? tweened : null) }
154
+ : false;
154
155
  $: tweened_d = motionStore(defaultPathData(), { tweened: tweenedOptions });
155
156
  $: {
156
157
  const path = $radial
@@ -130,15 +130,15 @@
130
130
  center: { x: number; y: number },
131
131
  rect?: { width: number; height: number }
132
132
  ) {
133
- // TODO: Improve with geo/projection
133
+ const newScale = rect ? ($width < $height ? $width / rect.width : $height / rect.height) : 1;
134
134
 
135
135
  $translate = {
136
- x: $width / 2 - center.x,
137
- y: $height / 2 - center.y,
136
+ x: $width / 2 - center.x * newScale,
137
+ y: $height / 2 - center.y * newScale,
138
138
  };
139
139
 
140
140
  if (rect) {
141
- $scale = $width < $height ? $width / rect.width : $height / rect.height;
141
+ $scale = newScale;
142
142
  }
143
143
  }
144
144
 
@@ -433,15 +433,17 @@
433
433
  onpointenter={() => (highlightSeriesKey = s.key)}
434
434
  onpointleave={() => (highlightSeriesKey = null)}
435
435
  {...props.highlight}
436
- points={{
437
- ...highlightPointsProps,
438
- fill: s.color,
439
- class: cls(
440
- 'transition-opacity',
441
- highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
442
- highlightPointsProps?.class
443
- ),
444
- }}
436
+ points={props.highlight?.points == false
437
+ ? false
438
+ : {
439
+ ...highlightPointsProps,
440
+ fill: s.color,
441
+ class: cls(
442
+ 'transition-opacity',
443
+ highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
444
+ highlightPointsProps?.class
445
+ ),
446
+ }}
445
447
  />
446
448
  {/each}
447
449
  </slot>
@@ -363,15 +363,17 @@
363
363
  onpointenter={() => (highlightSeriesKey = s.key)}
364
364
  onpointleave={() => (highlightSeriesKey = null)}
365
365
  {...props.highlight}
366
- points={{
367
- ...highlightPointsProps,
368
- fill: s.color,
369
- class: cls(
370
- 'transition-opacity',
371
- highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
372
- highlightPointsProps?.class
373
- ),
374
- }}
366
+ points={props.highlight?.points == false
367
+ ? false
368
+ : {
369
+ ...highlightPointsProps,
370
+ fill: s.color,
371
+ class: cls(
372
+ 'transition-opacity',
373
+ highlightSeriesKey && highlightSeriesKey !== s.key && 'opacity-10',
374
+ highlightPointsProps?.class
375
+ ),
376
+ }}
375
377
  />
376
378
  {/each}
377
379
  </slot>
@@ -192,7 +192,7 @@
192
192
  const point = localPoint(e, containerNode);
193
193
 
194
194
  if (
195
- tooltipData === null && // mode !== 'manual' but support annotations
195
+ tooltipData == null && // mode !== 'manual' but support annotations
196
196
  (point.x < tooltipContextNode.offsetLeft ||
197
197
  point.x > tooltipContextNode.offsetLeft + tooltipContextNode.offsetWidth ||
198
198
  point.y < tooltipContextNode.offsetTop ||
@@ -0,0 +1,2 @@
1
+ import { memoize } from 'lodash-es';
2
+ export const memoizeObject = memoize((obj) => obj, (obj) => JSON.stringify(obj));
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": "1.0.2",
7
+ "version": "1.0.4",
8
8
  "devDependencies": {
9
9
  "@changesets/cli": "^2.28.1",
10
10
  "@mdi/js": "^7.4.47",