Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit 41763d1

Browse files
committed
move files
1 parent 52664d8 commit 41763d1

File tree

3 files changed

+66
-33
lines changed

3 files changed

+66
-33
lines changed

samples/end_hierarchy.html

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,23 @@
2323
const data = {
2424
// define label tree
2525
labels: [
26-
'A',
26+
{
27+
label: 'A1',
28+
children: [
29+
{
30+
label: 'A1.1',
31+
children: ['A1.1.1', 'A1.1.2', 'A1.1.3']
32+
},
33+
{
34+
label: 'A1.2',
35+
children: ['A1.2.1', 'A1.2.2', 'A1.2.3']
36+
},
37+
'A1.3',
38+
{
39+
label: 'A1.4',
40+
children: ['A1.4.1', 'A1.4.2', 'A1.4.3']
41+
}]
42+
},
2743
{
2844
label: 'C1',
2945
expand: true,
@@ -39,7 +55,23 @@
3955
label: 'Test',
4056
// store as the tree attribute for reference, the data attribute will be automatically managed
4157
tree: [
42-
1,
58+
{
59+
value: 1,
60+
children: [
61+
{
62+
value: 2,
63+
children: [3, 4, 5]
64+
},
65+
{
66+
value: 6,
67+
children: [7, 8, 9]
68+
},
69+
10,
70+
{
71+
value: 11,
72+
children: [12, 13, 14]
73+
}]
74+
},
4375
{
4476
value: 6,
4577
children: [7, 8, 9, {

src/plugin/hierarchical.js

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import * as Chart from 'chart.js';
4-
import {toNodes, countExpanded, resolve, parentsOf, preOrderTraversal, flatChildren, lastOfLevel} from '../utils';
4+
import {toNodes, countExpanded, resolve, parentsOf, preOrderTraversal, lastOfLevel} from '../utils';
55

66

77
function parseFontOptions(options) {
@@ -145,34 +145,6 @@ const HierarchicalPlugin = {
145145
const ctx = chart.ctx;
146146
const hor = scale.isHorizontal();
147147

148-
const spanLogic = (node) => {
149-
const firstChild = node.children[0];
150-
const lastChild = node.children[node.children.length - 1];
151-
const flatSubTree = flatChildren(node, flat);
152-
153-
const leftVisible = flatSubTree.find((d) => visibles.has(d));
154-
const rightVisible = flatSubTree.slice().reverse().find((d) => visibles.has(d));
155-
156-
if (!leftVisible || !rightVisible) {
157-
return false;
158-
}
159-
160-
const leftParents = parentsOf(leftVisible, flat);
161-
const rightParents = parentsOf(rightVisible, flat);
162-
// is the left visible one also a child of my first child = whole starting range is visible?
163-
const leftFirstVisible = leftParents[node.level + 1] === firstChild;
164-
// is the right visible one also my last child = whole end range is visible?
165-
const rightLastVisible = rightParents[node.level + 1] === lastChild;
166-
167-
const hasCollapseBox = leftFirstVisible && node.expand !== 'focus';
168-
const hasFocusBox = leftFirstVisible && rightLastVisible && node.children.length > 1;
169-
// the next visible after the left one
170-
const nextVisible = flat.slice(leftVisible.index + 1, rightVisible.index + 1).find((d) => visibles.has(d));
171-
const groupLabelCenter = !nextVisible ? leftVisible.center : (leftVisible.center + nextVisible.center) / 2;
172-
173-
return {hasCollapseBox, hasFocusBox, leftVisible, rightVisible, groupLabelCenter, leftFirstVisible, rightLastVisible};
174-
};
175-
176148
const boxSize = scale.options.hierarchyBoxSize;
177149
const boxSize05 = boxSize * 0.5;
178150
const boxSize01 = boxSize * 0.1;
@@ -208,7 +180,7 @@ const HierarchicalPlugin = {
208180
}
209181
return false;
210182
}
211-
const r = spanLogic(node);
183+
const r = spanLogic(node, flat, visibles);
212184
if (!r) {
213185
return false;
214186
}
@@ -280,7 +252,7 @@ const HierarchicalPlugin = {
280252
}
281253
return false;
282254
}
283-
const r = spanLogic(node);
255+
const r = spanLogic(node, flat, visibles);
284256
if (!r) {
285257
return false;
286258
}

src/utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,32 @@ export function flatChildren(node, flat) {
151151
return flat.slice(firstChild.index); // till end
152152
}
153153
}
154+
155+
156+
export function spanLogic(node, flat, visibles) {
157+
const firstChild = node.children[0];
158+
const lastChild = node.children[node.children.length - 1];
159+
const flatSubTree = flatChildren(node, flat);
160+
161+
const leftVisible = flatSubTree.find((d) => visibles.has(d));
162+
const rightVisible = flatSubTree.slice().reverse().find((d) => visibles.has(d));
163+
164+
if (!leftVisible || !rightVisible) {
165+
return false;
166+
}
167+
168+
const leftParents = parentsOf(leftVisible, flat);
169+
const rightParents = parentsOf(rightVisible, flat);
170+
// is the left visible one also a child of my first child = whole starting range is visible?
171+
const leftFirstVisible = leftParents[node.level + 1] === firstChild;
172+
// is the right visible one also my last child = whole end range is visible?
173+
const rightLastVisible = rightParents[node.level + 1] === lastChild;
174+
175+
const hasCollapseBox = leftFirstVisible && node.expand !== 'focus';
176+
const hasFocusBox = leftFirstVisible && rightLastVisible && node.children.length > 1;
177+
// the next visible after the left one
178+
const nextVisible = flat.slice(leftVisible.index + 1, rightVisible.index + 1).find((d) => visibles.has(d));
179+
const groupLabelCenter = !nextVisible ? leftVisible.center : (leftVisible.center + nextVisible.center) / 2;
180+
181+
return {hasCollapseBox, hasFocusBox, leftVisible, rightVisible, groupLabelCenter, leftFirstVisible, rightLastVisible};
182+
}

0 commit comments

Comments
 (0)