layerchart 0.59.0 → 0.59.1

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.
@@ -20,6 +20,7 @@
20
20
  import { motionStore } from '../stores/motionStore.js';
21
21
  import { accessor, type Accessor } from '../utils/common.js';
22
22
  import { isScaleBand } from '../utils/scales.js';
23
+ import { flattenPathData } from '../utils/path.js';
23
24
 
24
25
  const {
25
26
  data: contextData,
@@ -29,6 +30,7 @@
29
30
  y: contextY,
30
31
  yRange,
31
32
  radial,
33
+ config,
32
34
  } = chartContext();
33
35
 
34
36
  /** Override data instead of using context */
@@ -100,19 +102,27 @@
100
102
 
101
103
  /** Provide initial `0` horizontal baseline and initially hide/untrack scale changes so not reactive (only set on initial mount) */
102
104
  function defaultPathData() {
103
- const path = $radial
104
- ? lineRadial()
105
- .angle((d) => $xScale(xAccessor(d)))
106
- .radius((d) => Math.min($yScale(0), $yRange[0]))
107
- : d3Line()
108
- .x((d) => $xScale(xAccessor(d)) + xOffset)
109
- .y((d) => Math.min($yScale(0), $yRange[0]));
110
-
111
- path.defined(defined ?? ((d) => xAccessor(d) != null && yAccessor(d) != null));
112
-
113
- if (curve) path.curve(curve);
114
-
115
- return path(data ?? $contextData);
105
+ if (pathData) {
106
+ // Flatten all `y` coordinates of pre-defined `pathData`
107
+ return flattenPathData(pathData, Math.min($yScale(0), $yRange[0]));
108
+ } else if ($config.x) {
109
+ // Only use default line if `x` accessor is defined (cartesian chart)
110
+ const path = $radial
111
+ ? lineRadial()
112
+ .angle((d) => $xScale(xAccessor(d)))
113
+ .radius((d) => Math.min($yScale(0), $yRange[0]))
114
+ : d3Line()
115
+ .x((d) => $xScale(xAccessor(d)) + xOffset)
116
+ .y((d) => Math.min($yScale(0), $yRange[0]));
117
+
118
+ path.defined(defined ?? ((d) => xAccessor(d) != null && yAccessor(d) != null));
119
+
120
+ if (curve) path.curve(curve);
121
+
122
+ return path(data ?? $contextData);
123
+ } else {
124
+ return '';
125
+ }
116
126
  }
117
127
 
118
128
  let d: string | null = '';
@@ -10,3 +10,5 @@ export declare function circlePath(dimensions: {
10
10
  r: number;
11
11
  sweep?: 'inside' | 'outside';
12
12
  }): string;
13
+ /** Flatten all `y` coordinates to `0` */
14
+ export declare function flattenPathData(pathData: string, yOverride?: number): string;
@@ -22,3 +22,23 @@ export function circlePath(dimensions) {
22
22
  a ${r},${r} 0 1,${sweep} -${r * 2},0
23
23
  `;
24
24
  }
25
+ /** Flatten all `y` coordinates to `0` */
26
+ export function flattenPathData(pathData, yOverride = 0) {
27
+ let result = pathData;
28
+ // Match commands with y-coordinates, and replace `y` coordinate with `0` (or override such as `yScale(0)`)
29
+ result = result.replace(/([MLTQCSAZ])(-?\d*\.?\d+),(-?\d*\.?\d+)/g, (match, command, x, y) => {
30
+ return `${command}${x},${yOverride}`;
31
+ });
32
+ // Replace all vertical line commands (ex. `v123`) with `0` height
33
+ result = result.replace(/([v])(-?\d*\.?\d+)/g, (match, command, l) => {
34
+ return `${command}${0}`;
35
+ });
36
+ // TODO: Flatten all elliptical arc commands (ex. `a4,4 0 0 1 4,4`) with `0` height
37
+ // result = result.replace(
38
+ // /a(\d+),(\d+) (\d+) (\d+) (\d+) (\d+),(\d+)/g,
39
+ // (match, rx, ry, rot, large, sweep, x, y) => {
40
+ // return `a${rx},0 ${rot} ${large} ${sweep} ${x},0`;
41
+ // }
42
+ // );
43
+ return result;
44
+ }
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.59.0",
7
+ "version": "0.59.1",
8
8
  "devDependencies": {
9
9
  "@changesets/cli": "^2.27.9",
10
10
  "@mdi/js": "^7.4.47",