layerchart 0.17.2 → 0.18.0

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.
@@ -1,18 +1,24 @@
1
1
  <script>import { getContext } from 'svelte';
2
2
  import { format as formatValue, cls } from 'svelte-ux';
3
- import { max, min } from 'd3-array';
3
+ import { extent } from 'd3-array';
4
4
  import Text from './Text.svelte';
5
5
  import { isScaleBand } from '../utils/scales';
6
6
  const { xScale, yScale, xRange, yRange, width } = getContext('LayerCake');
7
+ /** Location of axis */
7
8
  export let placement;
9
+ /** Draw a rule line. Use Rule component for greater rendering order control */
10
+ export let rule = false;
11
+ /** Draw a grid lines */
12
+ export let grid = false;
8
13
  /** Control the number of ticks*/
9
14
  export let ticks = placement === 'left' || placement === 'right' ? 4 : undefined;
10
- export let gridlines = false;
11
15
  export let tickSize = 4;
12
16
  export let format = undefined;
13
17
  export let labelProps = undefined;
14
18
  $: orientation = ['top', 'bottom'].includes(placement) ? 'horizontal' : 'vertical';
15
19
  $: scale = orientation === 'horizontal' ? $xScale : $yScale;
20
+ $: [xRangeMin, xRangeMax] = extent($xRange);
21
+ $: [yRangeMin, yRangeMax] = extent($yRange);
16
22
  $: tickVals = Array.isArray(ticks)
17
23
  ? ticks
18
24
  : isScaleBand(scale)
@@ -23,21 +29,21 @@ function getCoords(tick) {
23
29
  case 'top':
24
30
  return {
25
31
  x: $xScale(tick) + (isScaleBand($xScale) ? $xScale.bandwidth() / 2 : 0),
26
- y: min($yRange)
32
+ y: xRangeMin
27
33
  };
28
34
  case 'bottom':
29
35
  return {
30
36
  x: $xScale(tick) + (isScaleBand($xScale) ? $xScale.bandwidth() / 2 : 0),
31
- y: max($yRange)
37
+ y: yRangeMax
32
38
  };
33
39
  case 'left':
34
40
  return {
35
- x: min($xRange),
41
+ x: xRangeMin,
36
42
  y: $yScale(tick) + (isScaleBand($yScale) ? $yScale.bandwidth() / 2 : 0)
37
43
  };
38
44
  case 'right':
39
45
  return {
40
- x: max($xRange),
46
+ x: xRangeMax,
41
47
  y: $yScale(tick) + (isScaleBand($yScale) ? $yScale.bandwidth() / 2 : 0)
42
48
  };
43
49
  }
@@ -75,19 +81,44 @@ function getDefaultLabelProps() {
75
81
  </script>
76
82
 
77
83
  <g class="Axis placement-{placement}">
84
+ {#if rule !== false}
85
+ {@const lineProps = typeof rule === 'object' ? rule : null}
86
+ {#if orientation === 'vertical'}
87
+ <line
88
+ x1={placement === 'right' ? xRangeMax : xRangeMin}
89
+ x2={placement === 'right' ? xRangeMax : xRangeMin}
90
+ y1={$yRange[0] || 0}
91
+ y2={$yRange[1] || 0}
92
+ {...lineProps}
93
+ class={cls('rule stroke-gray-400', lineProps?.class)}
94
+ />
95
+ {/if}
96
+
97
+ {#if orientation === 'horizontal'}
98
+ <line
99
+ x1={$xRange[0] || 0}
100
+ x2={$xRange[1] || 0}
101
+ y1={placement === 'top' ? yRangeMin : yRangeMax}
102
+ y2={placement === 'top' ? yRangeMin : yRangeMax}
103
+ {...lineProps}
104
+ class={cls('rule stroke-gray-400', lineProps?.class)}
105
+ />
106
+ {/if}
107
+ {/if}
108
+
78
109
  {#each tickVals as tick, i}
79
110
  {@const tickCoords = getCoords(tick)}
80
111
  <g>
81
- {#if gridlines !== false}
82
- {@const lineProps = typeof gridlines === 'object' ? gridlines : null}
112
+ {#if grid !== false}
113
+ {@const lineProps = typeof grid === 'object' ? grid : null}
83
114
  {#if orientation === 'horizontal'}
84
115
  <line
85
116
  x1={tickCoords.x}
86
- y1={min($yRange)}
117
+ y1={yRangeMin}
87
118
  x2={tickCoords.x}
88
- y2={max($yRange)}
119
+ y2={yRangeMax}
89
120
  {...lineProps}
90
- class={cls('gridline stroke-gray-200', lineProps?.class)}
121
+ class={cls('grid stroke-gray-200', lineProps?.class)}
91
122
  />
92
123
  {:else if orientation === 'vertical'}
93
124
  <line
@@ -96,11 +127,12 @@ function getDefaultLabelProps() {
96
127
  x2={$width}
97
128
  y2={tickCoords.y}
98
129
  {...lineProps}
99
- class={cls('gridline stroke-gray-200', lineProps?.class)}
130
+ class={cls('grid stroke-gray-200', lineProps?.class)}
100
131
  />
101
132
  {/if}
102
133
  {/if}
103
134
 
135
+ <!-- Tick marks -->
104
136
  {#if orientation === 'horizontal'}
105
137
  <line
106
138
  x1={tickCoords.x}
@@ -5,9 +5,10 @@ import { type FormatType } from 'svelte-ux';
5
5
  import Text from './Text.svelte';
6
6
  declare const __propDef: {
7
7
  props: {
8
- placement: 'top' | 'bottom' | 'left' | 'right';
8
+ /** Location of axis */ placement: 'top' | 'bottom' | 'left' | 'right';
9
+ /** Draw a rule line. Use Rule component for greater rendering order control */ rule?: boolean | SVGAttributes<SVGLineElement> | undefined;
10
+ /** Draw a grid lines */ grid?: boolean | SVGAttributes<SVGLineElement> | undefined;
9
11
  /** Control the number of ticks*/ ticks?: number | Function | undefined;
10
- gridlines?: boolean | SVGAttributes<SVGLineElement> | undefined;
11
12
  tickSize?: number | undefined;
12
13
  format?: FormatType;
13
14
  labelProps?: ComponentProps<Text> | undefined;
@@ -0,0 +1,14 @@
1
+ <script>import { getContext } from 'svelte';
2
+ const { width, height, padding } = getContext('LayerCake');
3
+ /** Include padding area */
4
+ export let full = false;
5
+ </script>
6
+
7
+ <rect
8
+ x={full ? -$padding.left : 0}
9
+ y={full ? -$padding.top : 0}
10
+ width={$width + (full ? $padding.left + $padding.right : 0)}
11
+ height={$height + (full ? $padding.top + $padding.bottom : 0)}
12
+ on:click
13
+ {...$$restProps}
14
+ />
@@ -0,0 +1,19 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ declare const __propDef: {
3
+ props: {
4
+ [x: string]: any;
5
+ full?: boolean | undefined;
6
+ };
7
+ events: {
8
+ click: MouseEvent;
9
+ } & {
10
+ [evt: string]: CustomEvent<any>;
11
+ };
12
+ slots: {};
13
+ };
14
+ export type FrameProps = typeof __propDef.props;
15
+ export type FrameEvents = typeof __propDef.events;
16
+ export type FrameSlots = typeof __propDef.slots;
17
+ export default class Frame extends SvelteComponentTyped<FrameProps, FrameEvents, FrameSlots> {
18
+ }
19
+ export {};
@@ -103,17 +103,20 @@ else {
103
103
  <div
104
104
  class={cls(
105
105
  'inline-block',
106
- {
107
- 'top-left': 'absolute top-0 left-0',
108
- top: 'absolute top-0 left-1/2 -translate-x-1/2',
109
- 'top-right': 'absolute top-0 right-0',
110
- left: 'absolute top-1/2 left-0 -translate-y-1/2',
111
- center: 'absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2',
112
- right: 'absolute top-1/2 right-0 -translate-y-1/2',
113
- 'bottom-left': 'absolute bottom-0 left-0',
114
- bottom: 'absolute bottom-0 left-1/2 -translate-x-1/2',
115
- 'bottom-right': 'absolute bottom-0 right-0'
116
- }[placement],
106
+ placement && [
107
+ 'absolute',
108
+ {
109
+ 'top-left': 'top-0 left-0',
110
+ top: 'top-0 left-1/2 -translate-x-1/2',
111
+ 'top-right': 'top-0 right-0',
112
+ left: 'top-1/2 left-0 -translate-y-1/2',
113
+ center: 'top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2',
114
+ right: 'top-1/2 right-0 -translate-y-1/2',
115
+ 'bottom-left': 'bottom-0 left-0',
116
+ bottom: 'bottom-0 left-1/2 -translate-x-1/2',
117
+ 'bottom-right': 'bottom-0 right-0'
118
+ }[placement]
119
+ ],
117
120
  classes.root
118
121
  )}
119
122
  {...$$restProps}
@@ -0,0 +1,48 @@
1
+ <script>import { getContext } from 'svelte';
2
+ import { cls } from 'svelte-ux';
3
+ import { extent } from 'd3-array';
4
+ import Line from './Line.svelte';
5
+ const { xScale, yScale, xRange, yRange } = getContext('LayerCake');
6
+ $: [xRangeMin, xRangeMax] = extent($xRange);
7
+ $: [yRangeMin, yRangeMax] = extent($yRange);
8
+ /**
9
+ * Create a vertical `x` line
10
+ * - If true or 'left', will draw at chart left (xRange[0])
11
+ * - If 'right', will draw at chart right (xRange[1])
12
+ * - Use `0` for baseline (yScale(0))
13
+ * - Use number | Date value for annotation (yScale(value))
14
+ */
15
+ export let x = false;
16
+ /**
17
+ * Create a horizontal `y` line
18
+ * - If true or 'bottom', will draw at chart bottom (yRange[0])
19
+ * - If 'top', will draw at chart top (yRange[1])
20
+ * - Use `0` for baseline (xScale(0))
21
+ * - Use number | Date value for annotation (xScale(value))
22
+ */
23
+ export let y = false;
24
+ </script>
25
+
26
+ <g class="rule">
27
+ {#if x !== false}
28
+ <Line
29
+ x1={x === true || x === 'left' ? xRangeMin : x === 'right' ? xRangeMax : $xScale(x)}
30
+ x2={x === true || x === 'left' ? xRangeMin : x === 'right' ? xRangeMax : $xScale(x)}
31
+ y1={$yRange[0] || 0}
32
+ y2={$yRange[1] || 0}
33
+ {...$$restProps}
34
+ class={cls('stroke-gray-400', $$props.class)}
35
+ />
36
+ {/if}
37
+
38
+ {#if y !== false}
39
+ <Line
40
+ x1={$xRange[0] || 0}
41
+ x2={$xRange[1] || 0}
42
+ y1={y === true || y === 'bottom' ? yRangeMax : y === 'top' ? yRangeMin : $yScale(y)}
43
+ y2={y === true || y === 'bottom' ? yRangeMax : y === 'top' ? yRangeMin : $yScale(y)}
44
+ {...$$restProps}
45
+ class={cls('stroke-gray-400', $$props.class)}
46
+ />
47
+ {/if}
48
+ </g>
@@ -0,0 +1,18 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ declare const __propDef: {
3
+ props: {
4
+ [x: string]: any;
5
+ x?: number | boolean | "left" | "right" | Date | undefined;
6
+ y?: number | boolean | "top" | "bottom" | Date | undefined;
7
+ };
8
+ events: {
9
+ [evt: string]: CustomEvent<any>;
10
+ };
11
+ slots: {};
12
+ };
13
+ export type RuleProps = typeof __propDef.props;
14
+ export type RuleEvents = typeof __propDef.events;
15
+ export type RuleSlots = typeof __propDef.slots;
16
+ export default class Rule extends SvelteComponentTyped<RuleProps, RuleEvents, RuleSlots> {
17
+ }
18
+ export {};
@@ -3,7 +3,6 @@ export { default as Area } from './Area.svelte';
3
3
  export { default as AreaStack } from './AreaStack.svelte';
4
4
  export { default as Axis } from './Axis.svelte';
5
5
  export { default as Bars } from './Bars.svelte';
6
- export { default as Baseline } from './Baseline.svelte';
7
6
  export { default as Blur } from './Blur.svelte';
8
7
  export { default as Bounds } from './Bounds.svelte';
9
8
  export { default as Chart } from './Chart.svelte';
@@ -14,6 +13,7 @@ export { default as ClipPath } from './ClipPath.svelte';
14
13
  export { default as ClipPathUse } from './ClipPathUse.svelte';
15
14
  export { default as ColorRamp } from './ColorRamp.svelte';
16
15
  export { default as ConnectedPoints } from './ConnectedPoints.svelte';
16
+ export { default as Frame } from './Frame.svelte';
17
17
  export { default as GeoContext } from './GeoContext.svelte';
18
18
  export { default as GeoPath } from './GeoPath.svelte';
19
19
  export { default as GeoPoint } from './GeoPoint.svelte';
@@ -35,6 +35,7 @@ export { default as Pie } from './Pie.svelte';
35
35
  export { default as Points } from './Points.svelte';
36
36
  export { default as Rect } from './Rect.svelte';
37
37
  export { default as RectClipPath } from './RectClipPath.svelte';
38
+ export { default as Rule } from './Rule.svelte';
38
39
  export { default as Sankey } from './Sankey.svelte';
39
40
  export { default as Text } from './Text.svelte';
40
41
  export { default as Threshold } from './Threshold.svelte';
@@ -3,7 +3,6 @@ export { default as Area } from './Area.svelte';
3
3
  export { default as AreaStack } from './AreaStack.svelte';
4
4
  export { default as Axis } from './Axis.svelte';
5
5
  export { default as Bars } from './Bars.svelte';
6
- export { default as Baseline } from './Baseline.svelte';
7
6
  export { default as Blur } from './Blur.svelte';
8
7
  export { default as Bounds } from './Bounds.svelte';
9
8
  export { default as Chart } from './Chart.svelte';
@@ -14,6 +13,7 @@ export { default as ClipPath } from './ClipPath.svelte';
14
13
  export { default as ClipPathUse } from './ClipPathUse.svelte';
15
14
  export { default as ColorRamp } from './ColorRamp.svelte';
16
15
  export { default as ConnectedPoints } from './ConnectedPoints.svelte';
16
+ export { default as Frame } from './Frame.svelte';
17
17
  export { default as GeoContext } from './GeoContext.svelte';
18
18
  export { default as GeoPath } from './GeoPath.svelte';
19
19
  export { default as GeoPoint } from './GeoPoint.svelte';
@@ -35,6 +35,7 @@ export { default as Pie } from './Pie.svelte';
35
35
  export { default as Points } from './Points.svelte';
36
36
  export { default as Rect } from './Rect.svelte';
37
37
  export { default as RectClipPath } from './RectClipPath.svelte';
38
+ export { default as Rule } from './Rule.svelte';
38
39
  export { default as Sankey } from './Sankey.svelte';
39
40
  export { default as Text } from './Text.svelte';
40
41
  export { default as Threshold } from './Threshold.svelte';
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { Svg, Html } from 'layercake';
2
- export * from './components/index.js';
3
- export * from './utils/index.js';
2
+ export * from './components';
3
+ export * from './utils';
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { Svg, Html } from 'layercake';
2
- export * from './components/index.js';
3
- export * from './utils/index.js';
2
+ export * from './components';
3
+ export * from './utils';
@@ -8,6 +8,15 @@ export declare function getRandomNumber(min: number, max: number): number;
8
8
  * see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_integer_between_two_values_inclusive
9
9
  */
10
10
  export declare function getRandomInteger(min: number, max: number, includeMax?: boolean): number;
11
+ export declare function createSeries(options: {
12
+ count?: number;
13
+ min: number;
14
+ max: number;
15
+ keys?: Array<string>;
16
+ value?: 'number' | 'integer';
17
+ }): {
18
+ x: number;
19
+ }[];
11
20
  export declare function createDateSeries(options: {
12
21
  count?: number;
13
22
  min: number;
@@ -16,6 +16,23 @@ export function getRandomInteger(min, max, includeMax = true) {
16
16
  max = Math.floor(max);
17
17
  return Math.floor(Math.random() * (max - min + (includeMax ? 1 : 0)) + min);
18
18
  }
19
+ export function createSeries(options) {
20
+ const count = options.count ?? 10;
21
+ const min = options.min;
22
+ const max = options.max;
23
+ const keys = options.keys ?? ['y'];
24
+ return Array.from({ length: count }).map((_, i) => {
25
+ return {
26
+ x: options.value === 'integer' ? getRandomInteger(min, max) : getRandomNumber(min, max),
27
+ ...Object.fromEntries(keys.map((key) => {
28
+ return [
29
+ key,
30
+ options.value === 'integer' ? getRandomInteger(min, max) : getRandomNumber(min, max)
31
+ ];
32
+ }))
33
+ };
34
+ });
35
+ }
19
36
  export function createDateSeries(options) {
20
37
  const now = startOfToday();
21
38
  const count = options.count ?? 10;
@@ -1,4 +1,4 @@
1
- import type { SankeyGraph, SankeyNodeMinimal } from 'd3-sankey';
1
+ import type { SankeyExtraProperties, SankeyGraph, SankeyLink, SankeyNodeMinimal } from 'd3-sankey';
2
2
  import type { hierarchy as d3Hierarchy } from 'd3-hierarchy';
3
3
  /**
4
4
  * Convert CSV rows in format: 'source,target,value' to SankeyGraph
@@ -22,3 +22,7 @@ export declare function graphFromNode(node: SankeyNodeMinimal<any, any>): {
22
22
  nodes: SankeyNodeMinimal<any, any>[];
23
23
  links: any[];
24
24
  };
25
+ /**
26
+ * Get distinct nodes from link.source and link.target
27
+ */
28
+ export declare function nodesFromLinks<N extends SankeyExtraProperties, L extends SankeyExtraProperties>(links: Array<SankeyLink<N, L>>): any[];
@@ -12,16 +12,7 @@ export function graphFromCsv(csv) {
12
12
  // color: linkColor,
13
13
  }
14
14
  : null);
15
- const nodeByName = new Map();
16
- for (const link of links) {
17
- if (!nodeByName.has(link.source)) {
18
- nodeByName.set(link.source, { name: link.source });
19
- }
20
- if (!nodeByName.has(link.target)) {
21
- nodeByName.set(link.target, { name: link.target });
22
- }
23
- }
24
- return { nodes: Array.from(nodeByName.values()), links };
15
+ return { nodes: nodesFromLinks(links), links };
25
16
  }
26
17
  /**
27
18
  * Convert d3-hierarchy to graph (nodes/links)
@@ -58,3 +49,18 @@ export function graphFromNode(node) {
58
49
  });
59
50
  return { nodes, links };
60
51
  }
52
+ /**
53
+ * Get distinct nodes from link.source and link.target
54
+ */
55
+ export function nodesFromLinks(links) {
56
+ const nodesByName = new Map();
57
+ for (const link of links) {
58
+ if (!nodesByName.has(link.source)) {
59
+ nodesByName.set(link.source, { name: link.source });
60
+ }
61
+ if (!nodesByName.has(link.target)) {
62
+ nodesByName.set(link.target, { name: link.target });
63
+ }
64
+ }
65
+ return Array.from(nodesByName.values());
66
+ }
@@ -1,5 +1,5 @@
1
- export { graphFromCsv, graphFromHierarchy, graphFromNode } from './graph';
2
- export { findAncestor } from './hierarchy';
3
- export { degreesToRadians, radiansToDegrees } from './math';
4
- export { createStackData, stackOffsetSeparated } from './stack';
5
- export { getMajorTicks, getMinorTicks } from './ticks';
1
+ export * from './graph';
2
+ export * from './hierarchy';
3
+ export * from './math';
4
+ export * from './stack';
5
+ export * from './ticks';
@@ -1,5 +1,5 @@
1
- export { graphFromCsv, graphFromHierarchy, graphFromNode } from './graph';
2
- export { findAncestor } from './hierarchy';
3
- export { degreesToRadians, radiansToDegrees } from './math';
4
- export { createStackData, stackOffsetSeparated } from './stack';
5
- export { getMajorTicks, getMinorTicks } from './ticks';
1
+ export * from './graph';
2
+ export * from './hierarchy';
3
+ export * from './math';
4
+ export * from './stack';
5
+ export * from './ticks';
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "Sean Lynch <techniq35@gmail.com>",
4
4
  "license": "MIT",
5
5
  "repository": "techniq/layerchart",
6
- "version": "0.17.2",
6
+ "version": "0.18.0",
7
7
  "scripts": {
8
8
  "dev": "vite dev",
9
9
  "build": "vite build",
@@ -13,13 +13,16 @@
13
13
  "check": "svelte-check --tsconfig ./tsconfig.json",
14
14
  "check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
15
15
  "lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. .",
16
- "format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
16
+ "format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. .",
17
+ "changeset": "changeset",
18
+ "version-package": "changeset version"
17
19
  },
18
20
  "devDependencies": {
19
21
  "@rollup/plugin-dsv": "^3.0.2",
20
22
  "@sveltejs/adapter-vercel": "^3.0.1",
21
23
  "@sveltejs/kit": "^1.20.5",
22
24
  "@sveltejs/package": "^2.1.0",
25
+ "@svitejs/changesets-changelog-github-compact": "^1.1.0",
23
26
  "@tailwindcss/typography": "^0.5.9",
24
27
  "@types/d3-array": "^3.0.5",
25
28
  "@types/d3-delaunay": "^6.0.1",
@@ -54,6 +57,7 @@
54
57
  },
55
58
  "type": "module",
56
59
  "dependencies": {
60
+ "@changesets/cli": "^2.26.2",
57
61
  "@mdi/js": "^7.2.96",
58
62
  "@vercel/analytics": "^1.0.1",
59
63
  "d3-array": "^3.2.4",
@@ -1,30 +0,0 @@
1
- <script>import { min, max } from 'd3-array';
2
- import { getContext } from 'svelte';
3
- import { cls } from 'svelte-ux';
4
- import { isScaleBand } from '../utils/scales';
5
- const { xScale, yScale, xRange, yRange } = getContext('LayerCake');
6
- export let x = false;
7
- export let y = false;
8
- </script>
9
-
10
- <g class="baseline">
11
- {#if x}
12
- <line
13
- x1={0}
14
- x2={max($xRange) || 0}
15
- y1={isScaleBand($yScale) ? max($yRange) : $yScale(0) || 0}
16
- y2={isScaleBand($yScale) ? max($yRange) : $yScale(0) || 0}
17
- class={cls('stroke-gray-400', $$props.class)}
18
- />
19
- {/if}
20
-
21
- {#if y}
22
- <line
23
- x1={isScaleBand($xScale) ? min($xRange) : $xScale(0) || 0}
24
- x2={isScaleBand($xScale) ? min($xRange) : $xScale(0) || 0}
25
- y1={min($yRange) || 0}
26
- y2={max($yRange) || 0}
27
- class={cls('stroke-gray-400', $$props.class)}
28
- />
29
- {/if}
30
- </g>
@@ -1,18 +0,0 @@
1
- import { SvelteComponentTyped } from "svelte";
2
- declare const __propDef: {
3
- props: {
4
- [x: string]: any;
5
- x?: boolean | undefined;
6
- y?: boolean | undefined;
7
- };
8
- events: {
9
- [evt: string]: CustomEvent<any>;
10
- };
11
- slots: {};
12
- };
13
- export type BaselineProps = typeof __propDef.props;
14
- export type BaselineEvents = typeof __propDef.events;
15
- export type BaselineSlots = typeof __propDef.slots;
16
- export default class Baseline extends SvelteComponentTyped<BaselineProps, BaselineEvents, BaselineSlots> {
17
- }
18
- export {};