layerchart 0.3.2 → 0.3.5

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,7 +8,6 @@ import { cubicOut } from 'svelte/easing';
8
8
  import * as d3 from 'd3-hierarchy';
9
9
  import { scaleLinear } from 'd3-scale';
10
10
  import { group } from 'd3-array';
11
- import Group from './Group.svelte';
12
11
  import RectClipPath from './RectClipPath.svelte';
13
12
  import { aspectTile } from '../utils/treemap';
14
13
  const { data, width, height } = getContext('LayerCake');
@@ -35,32 +34,35 @@ $: tileFunc =
35
34
  : tile === 'sliceDice'
36
35
  ? d3.treemapSliceDice
37
36
  : tile;
38
- $: treemap = d3.treemap().size([$width, $height]).tile(aspectTile(tileFunc, $width, $height));
39
- $: if (padding) {
40
- treemap.padding(padding);
41
- }
42
- $: if (paddingInner) {
43
- treemap.paddingInner(paddingInner);
44
- }
45
- $: if (paddingOuter) {
46
- treemap.paddingOuter(paddingOuter);
47
- }
48
- $: if (paddingTop) {
49
- treemap.paddingTop(paddingTop);
50
- }
51
- $: if (paddingBottom) {
52
- treemap.paddingBottom(paddingBottom);
53
- }
54
- $: if (paddingLeft) {
55
- treemap.paddingLeft(paddingLeft);
56
- }
57
- $: if (paddingLeft) {
58
- treemap.paddingRight(paddingRight);
37
+ let treemap;
38
+ $: {
39
+ treemap = d3.treemap().size([$width, $height]).tile(aspectTile(tileFunc, $width, $height));
40
+ if (padding) {
41
+ treemap.padding(padding);
42
+ }
43
+ if (paddingInner) {
44
+ treemap.paddingInner(paddingInner);
45
+ }
46
+ if (paddingOuter) {
47
+ treemap.paddingOuter(paddingOuter);
48
+ }
49
+ if (paddingTop) {
50
+ treemap.paddingTop(paddingTop);
51
+ }
52
+ if (paddingBottom) {
53
+ treemap.paddingBottom(paddingBottom);
54
+ }
55
+ if (paddingLeft) {
56
+ treemap.paddingLeft(paddingLeft);
57
+ }
58
+ if (paddingRight) {
59
+ treemap.paddingRight(paddingRight);
60
+ }
59
61
  }
60
62
  $: root = treemap($data);
61
63
  $: selected = root; // set initial selection
62
- // group nodes by height so can be rendered lowest to highest
63
- $: nodesByHeight = group(root, (d) => d.height);
64
+ // group nodes by depth so can be rendered lowest to highest
65
+ $: nodesByDepth = group(root, (d) => d.depth);
64
66
  const duration = 800;
65
67
  const extents = tweened(undefined, { easing: cubicOut, duration });
66
68
  $: $extents = selected
@@ -81,24 +83,19 @@ $: yScale = scaleLinear().domain([$extents.y0, $extents.y1]).rangeRound([0, $hei
81
83
  </script>
82
84
 
83
85
  <RectClipPath width={$width} height={$height}>
84
- {#each Array.from(nodesByHeight) as [height, nodes]}
86
+ {#each Array.from(nodesByDepth) as [depth, nodes]}
85
87
  <g>
86
88
  {#each nodes as node}
87
- {@const nodeWidth = xScale(node.x1) - xScale(node.x0)}
88
- {@const nodeHeight = yScale(node.y1) - yScale(node.y0)}
89
- <Group x={xScale(node.x0)} y={yScale(node.y0)}>
90
- <RectClipPath width={nodeWidth} height={nodeHeight}>
91
- <slot
92
- {node}
93
- rect={{
94
- x: 0, // applied by Group
95
- y: 0, // applied by Group
96
- width: nodeWidth,
97
- height: nodeHeight
98
- }}
99
- />
100
- </RectClipPath>
101
- </Group>
89
+ <slot
90
+ name="node"
91
+ {node}
92
+ rect={{
93
+ x: xScale(node.x0),
94
+ y: yScale(node.y0),
95
+ width: xScale(node.x1) - xScale(node.x0),
96
+ height: yScale(node.y1) - yScale(node.y0)
97
+ }}
98
+ />
102
99
  {/each}
103
100
  </g>
104
101
  {/each}
@@ -16,13 +16,13 @@ declare const __propDef: {
16
16
  [evt: string]: CustomEvent<any>;
17
17
  };
18
18
  slots: {
19
- default: {
20
- node: d3.HierarchyRectangularNode<unknown>;
19
+ node: {
20
+ node: unknown;
21
21
  rect: {
22
22
  x: number;
23
23
  y: number;
24
- width: any;
25
- height: any;
24
+ width: number;
25
+ height: number;
26
26
  };
27
27
  };
28
28
  };
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.3.2",
6
+ "version": "0.3.5",
7
7
  "devDependencies": {
8
8
  "@sveltejs/adapter-vercel": "^1.0.0-next.47",
9
9
  "@sveltejs/kit": "^1.0.0-next.303",
@@ -1,6 +1,10 @@
1
1
  /**
2
- * This custom tiling function adapts the built-in binary tiling function
2
+ * This custom tiling function adapts the tiling function
3
3
  * for the appropriate aspect ratio when the treemap is zoomed-in.
4
4
  * see: https://observablehq.com/@d3/zoomable-treemap#tile and https://observablehq.com/@d3/stretched-treemap
5
5
  */
6
6
  export declare function aspectTile(tile: any, width: any, height: any): (node: any, x0: any, y0: any, x1: any, y1: any) => void;
7
+ /**
8
+ * Show if the node (a) is a child of the selected (b), or any parent above selected
9
+ */
10
+ export declare function isNodeVisible(a: any, b: any): boolean;
package/utils/treemap.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This custom tiling function adapts the built-in binary tiling function
2
+ * This custom tiling function adapts the tiling function
3
3
  * for the appropriate aspect ratio when the treemap is zoomed-in.
4
4
  * see: https://observablehq.com/@d3/zoomable-treemap#tile and https://observablehq.com/@d3/stretched-treemap
5
5
  */
@@ -14,3 +14,14 @@ export function aspectTile(tile, width, height) {
14
14
  }
15
15
  };
16
16
  }
17
+ /**
18
+ * Show if the node (a) is a child of the selected (b), or any parent above selected
19
+ */
20
+ export function isNodeVisible(a, b) {
21
+ while (b) {
22
+ if (a.parent === b)
23
+ return true;
24
+ b = b.parent;
25
+ }
26
+ return false;
27
+ }