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

Commit 419da24

Browse files
committed
fix ui cornercase and format code
1 parent 2a2d32c commit 419da24

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

src/plugin/hierarchical.js

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

3-
import {helpers, defaults, pluginService} from 'chart.js';
4-
import {toNodes, countExpanded, resolve, parentsOf, preOrderTraversal, lastOfLevel, spanLogic, determineVisible} from '../utils';
3+
import {
4+
helpers,
5+
defaults,
6+
pluginService
7+
} from 'chart.js';
8+
import {
9+
toNodes,
10+
countExpanded,
11+
resolve,
12+
parentsOf,
13+
preOrderTraversal,
14+
lastOfLevel,
15+
spanLogic,
16+
determineVisible,
17+
flatChildren
18+
} from '../utils';
519

620

721
function parseFontOptions(options) {
@@ -176,7 +190,15 @@ const HierarchicalPlugin = {
176190
if (!r) {
177191
return false;
178192
}
179-
const {hasFocusBox, hasCollapseBox, leftVisible, rightVisible, leftFirstVisible, rightLastVisible, groupLabelCenter} = r;
193+
const {
194+
hasFocusBox,
195+
hasCollapseBox,
196+
leftVisible,
197+
rightVisible,
198+
leftFirstVisible,
199+
rightLastVisible,
200+
groupLabelCenter
201+
} = r;
180202

181203
// render group label
182204
if (renderLabel === 'below') {
@@ -248,7 +270,15 @@ const HierarchicalPlugin = {
248270
if (!r) {
249271
return false;
250272
}
251-
const {hasFocusBox, hasCollapseBox, leftVisible, rightVisible, leftFirstVisible, rightLastVisible, groupLabelCenter} = r;
273+
const {
274+
hasFocusBox,
275+
hasCollapseBox,
276+
leftVisible,
277+
rightVisible,
278+
leftFirstVisible,
279+
rightLastVisible,
280+
groupLabelCenter
281+
} = r;
252282

253283
// render group label
254284
ctx.fillText(node.label, offset - boxSize, groupLabelCenter);
@@ -415,11 +445,16 @@ const HierarchicalPlugin = {
415445
return null;
416446
}
417447

418-
const elem = chart.getElementsAtEventForMode(event, 'index', {axis: hor ? 'x' : 'y'})[0];
448+
const elem = chart.getElementsAtEventForMode(event, 'index', {
449+
axis: hor ? 'x' : 'y'
450+
})[0];
419451
if (!elem) {
420452
return null;
421453
}
422-
return {offset, index: elem._index};
454+
return {
455+
offset,
456+
index: elem._index
457+
};
423458
},
424459

425460
beforeEvent(chart, event) {
@@ -466,7 +501,7 @@ const HierarchicalPlugin = {
466501
return;
467502
}
468503
// last index of expanded parent
469-
if (isLastChildOfParent && parent.expand === true) {
504+
if (isLastChildOfParent && parent.expand === true && flatChildren(parent, flat).every((d) => d.expand !== 'focus')) {
470505
this._zoomIn(chart, index, parent, flat);
471506
return;
472507
}

src/utils.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* builds up recursivly the label tree
43
* @param {string|ILabelNode} label
@@ -15,7 +14,9 @@ export function asNode(label, parent) {
1514
width: 0,
1615
hidden: false,
1716
major: !parent // for ticks
18-
}, typeof label === 'string' ? {label} : label);
17+
}, typeof label === 'string' ? {
18+
label
19+
} : label);
1920

2021
node.children = node.children.map((d) => asNode(d, node));
2122

@@ -119,7 +120,9 @@ export function preOrderTraversal(node, visit) {
119120
export function resolve(label, flat, dataTree) {
120121
const parents = parentsOf(label, flat);
121122

122-
let dataItem = {children: dataTree};
123+
let dataItem = {
124+
children: dataTree
125+
};
123126
const dataParents = parents.map((p) => {
124127
dataItem = dataItem && !(typeof dataItem === 'number' && isNaN(dataItem)) && dataItem.children ? dataItem.children[p.relIndex] : NaN;
125128
return dataItem;
@@ -155,8 +158,8 @@ export function flatChildren(node, flat) {
155158
const nextSibling = flat[node.parent].children[node.relIndex + 1];
156159
return flat.slice(firstChild.index, nextSibling.index);
157160
}
158-
// find sibling
159-
const nextSibling = flat.slice(firstChild.index + 1).find((d) => d.parent === node.parent && d.relIndex === node.relIndex + 1);
161+
// find sibling or next in the hierarchy up
162+
const nextSibling = flat.slice(firstChild.index + 1).find((d) => d.level < node.level || (d.parent === node.parent && d.relIndex === node.relIndex + 1));
160163
if (nextSibling) {
161164
return flat.slice(firstChild.index, nextSibling.index);
162165
}
@@ -208,5 +211,13 @@ export function spanLogic(node, flat, visibles) {
208211
const nextVisible = flat.slice(leftVisible.index + 1, rightVisible.index + 1).find((d) => visibles.has(d));
209212
const groupLabelCenter = !nextVisible ? leftVisible.center : (leftVisible.center + nextVisible.center) / 2;
210213

211-
return {hasCollapseBox, hasFocusBox, leftVisible, rightVisible, groupLabelCenter, leftFirstVisible, rightLastVisible};
214+
return {
215+
hasCollapseBox,
216+
hasFocusBox,
217+
leftVisible,
218+
rightVisible,
219+
groupLabelCenter,
220+
leftFirstVisible,
221+
rightLastVisible
222+
};
212223
}

tests/utils.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ describe('flatChildren', () => {
153153
const children = flatChildren(a, flat);
154154
expect(children).toEqual(flat.slice(1, 1 + 4));
155155
});
156+
157+
test('ab nested', () => {
158+
const {flat, root} = setupNodes([{ label: 'a', children: ['aa', { label: 'ab', children: ['aba', 'abb']}] }, 'b', 'c']);
159+
160+
const ab = root[0].children[1];
161+
const children = flatChildren(ab, flat);
162+
expect(children).toEqual(ab.children);
163+
});
156164
});
157165

158166
describe('countExpanded', () => {

0 commit comments

Comments
 (0)