layerchart 0.58.4 → 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.
@@ -6,9 +6,11 @@
6
6
  import Spline from './Spline.svelte';
7
7
 
8
8
  import { createDimensionGetter } from '../utils/rect.js';
9
- import type { Accessor } from '../utils/common.js';
9
+ import { isScaleBand } from '../utils/scales.js';
10
+ import { accessor, type Accessor } from '../utils/common.js';
11
+ import { greatestAbs } from '@layerstack/utils/array';
10
12
 
11
- const { x: xContext, y: yContext } = chartContext();
13
+ const { x: xContext, y: yContext, xScale } = chartContext();
12
14
 
13
15
  export let bar: Object;
14
16
 
@@ -40,6 +42,8 @@
40
42
  /** Control which corners are rounded with radius. Uses <path> instead of <rect> when not set to `all` */
41
43
  export let rounded:
42
44
  | 'all'
45
+ | 'none'
46
+ | 'edge'
43
47
  | 'top'
44
48
  | 'bottom'
45
49
  | 'left'
@@ -65,10 +69,27 @@
65
69
  });
66
70
  $: dimensions = $getDimensions(bar) ?? { x: 0, y: 0, width: 0, height: 0 };
67
71
 
68
- $: topLeft = ['all', 'top', 'left', 'top-left'].includes(rounded);
69
- $: topRight = ['all', 'top', 'right', 'top-right'].includes(rounded);
70
- $: bottomLeft = ['all', 'bottom', 'left', 'bottom-left'].includes(rounded);
71
- $: bottomRight = ['all', 'bottom', 'right', 'bottom-right'].includes(rounded);
72
+ $: isVertical = isScaleBand($xScale);
73
+ $: valueAccessor = accessor(isVertical ? y : x);
74
+ $: value = valueAccessor(bar);
75
+ $: resolvedValue = Array.isArray(value) ? greatestAbs(value) : value;
76
+
77
+ // Resolved `rounded="edge"` based on orientation and value
78
+ $: _rounded =
79
+ rounded === 'edge'
80
+ ? isVertical
81
+ ? resolvedValue >= 0
82
+ ? 'top'
83
+ : 'bottom'
84
+ : resolvedValue >= 0
85
+ ? 'right'
86
+ : 'left'
87
+ : rounded;
88
+
89
+ $: topLeft = ['all', 'top', 'left', 'top-left'].includes(_rounded);
90
+ $: topRight = ['all', 'top', 'right', 'top-right'].includes(_rounded);
91
+ $: bottomLeft = ['all', 'bottom', 'left', 'bottom-left'].includes(_rounded);
92
+ $: bottomRight = ['all', 'bottom', 'right', 'bottom-right'].includes(_rounded);
72
93
 
73
94
  $: width = dimensions.width;
74
95
  $: height = dimensions.height;
@@ -87,7 +108,7 @@
87
108
  .join('');
88
109
  </script>
89
110
 
90
- {#if rounded === 'all'}
111
+ {#if _rounded === 'all' || radius === 0}
91
112
  <Rect
92
113
  {fill}
93
114
  {spring}
@@ -1,7 +1,7 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
2
  import { type ComponentProps } from 'svelte';
3
3
  import Rect from './Rect.svelte';
4
- import type { Accessor } from '../utils/common.js';
4
+ import { type Accessor } from '../utils/common.js';
5
5
  declare const __propDef: {
6
6
  props: {
7
7
  [x: string]: any;
@@ -14,7 +14,7 @@ declare const __propDef: {
14
14
  stroke?: string | undefined;
15
15
  strokeWidth?: number | undefined;
16
16
  radius?: number | undefined;
17
- rounded?: ("all" | "top" | "bottom" | "left" | "right" | "top-left" | "top-right" | "bottom-left" | "bottom-right") | undefined;
17
+ rounded?: ("all" | "none" | "edge" | "top" | "bottom" | "left" | "right" | "top-left" | "top-right" | "bottom-left" | "bottom-right") | undefined;
18
18
  inset?: number | undefined;
19
19
  spring?: ComponentProps<Rect>["spring"];
20
20
  tweened?: ComponentProps<Rect>["tweened"];
@@ -43,18 +43,20 @@
43
43
 
44
44
  export let spring: ComponentProps<Rect>['spring'] = undefined;
45
45
  export let tweened: ComponentProps<Rect>['tweened'] = undefined;
46
+
47
+ $: _data = chartDataArray(data ?? $contextData);
46
48
  </script>
47
49
 
48
50
  <g class="Bars">
49
51
  <slot>
50
- {#each chartDataArray(data ?? $contextData) as item}
52
+ {#each _data as d, i}
51
53
  <Bar
52
- bar={item}
54
+ bar={d}
53
55
  {x}
54
56
  {y}
55
57
  {x1}
56
58
  {y1}
57
- fill={fill ?? ($config.c ? $cGet(item) : null)}
59
+ fill={fill ?? ($config.c ? $cGet(d) : null)}
58
60
  {stroke}
59
61
  {strokeWidth}
60
62
  {radius}
@@ -47,7 +47,7 @@ declare const __propDef: {
47
47
  stroke?: string | undefined;
48
48
  strokeWidth?: number | undefined;
49
49
  radius?: number | undefined;
50
- rounded?: ("all" | "top" | "bottom" | "left" | "right" | "top-left" | "top-right" | "bottom-left" | "bottom-right") | undefined;
50
+ rounded?: ("all" | "none" | "edge" | "top" | "bottom" | "left" | "right" | "top-left" | "top-right" | "bottom-left" | "bottom-right") | undefined;
51
51
  inset?: number | undefined;
52
52
  spring?: ComponentProps<Rect>["spring"];
53
53
  tweened?: ComponentProps<Rect>["tweened"];
@@ -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 = '';
@@ -150,20 +150,16 @@
150
150
  }
151
151
 
152
152
  function getBarsProps(s: (typeof series)[number], i: number) {
153
+ const valueAccesor = stackSeries
154
+ ? (d: any) => d.stackData[i]
155
+ : (s.value ?? (s.data ? undefined : s.key));
153
156
  const barsProps: ComponentProps<Bars> = {
154
157
  data: s.data,
155
- x: !isVertical
156
- ? stackSeries
157
- ? (d) => d.stackData[i]
158
- : (s.value ?? (s.data ? undefined : s.key))
159
- : undefined,
160
- y: isVertical
161
- ? stackSeries
162
- ? (d) => d.stackData[i]
163
- : (s.value ?? (s.data ? undefined : s.key))
164
- : undefined,
158
+ x: !isVertical ? valueAccesor : undefined,
159
+ y: isVertical ? valueAccesor : undefined,
165
160
  x1: isVertical && groupSeries ? (d) => s.value ?? s.key : undefined,
166
161
  y1: !isVertical && groupSeries ? (d) => s.value ?? s.key : undefined,
162
+ rounded: seriesLayout.startsWith('stack') && i !== series.length - 1 ? 'none' : 'edge',
167
163
  radius: 4,
168
164
  strokeWidth: 1,
169
165
  fill: s.color,
@@ -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.58.4",
7
+ "version": "0.59.1",
8
8
  "devDependencies": {
9
9
  "@changesets/cli": "^2.27.9",
10
10
  "@mdi/js": "^7.4.47",