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

Commit 0fd69d0

Browse files
author
Stefan Luger
authored
Merge pull request #18 from datavisyn/sluger/15_test
Sluger/15 test
2 parents 8cdaa43 + 3eea065 commit 0fd69d0

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"src/**/*.js"
2121
],
2222
"dependencies": {
23-
"chart.js": "^2.7.2"
23+
"chart.js": "^2.8.0"
2424
},
2525
"devDependencies": {
2626
"@babel/core": "^7.4.5",

src/scale/hierarchical.js

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

3-
import {scaleService, Scale} from 'chart.js';
4-
import {parentsOf} from '../utils';
3+
import {
4+
scaleService,
5+
Scale
6+
} from 'chart.js';
7+
import {
8+
parentsOf
9+
} from '../utils';
510

611
const defaultConfig = Object.assign({}, scaleService.getScaleDefaults('category'), {
712
/**
@@ -86,31 +91,30 @@ const HierarchicalScale = Scale.extend({
8691
const ratios = [1, Math.pow(ratio, 1), Math.pow(ratio, 2), Math.pow(ratio, 3), Math.pow(ratio, 4)];
8792

8893
const distances = [];
89-
{
90-
let prev = nodes[0];
91-
let prevParents = parentsOf(prev, flat);
92-
distances.push(0.5); // half top level distance before and after
93-
94-
for (let i = 1; i < nodes.length; ++i) {
95-
const n = nodes[i];
96-
const parents = parentsOf(n, flat);
97-
if (prev.parent === n.parent) {
98-
// same parent -> can use the level distance
99-
distances.push(ratios[n.level]);
100-
} else {
101-
// differnt level -> use the distance of the common parent
102-
// find level of common parent
103-
let common = 0;
104-
while (parents[common] === prevParents[common]) {
105-
common++;
106-
}
107-
distances.push(ratios[common]);
94+
95+
let prev = nodes[0];
96+
let prevParents = parentsOf(prev, flat);
97+
distances.push(0.5); // half top level distance before and after
98+
99+
for (let i = 1; i < nodes.length; ++i) {
100+
const n = nodes[i];
101+
const parents = parentsOf(n, flat);
102+
if (prev.parent === n.parent) {
103+
// same parent -> can use the level distance
104+
distances.push(ratios[n.level]);
105+
} else {
106+
// differnt level -> use the distance of the common parent
107+
// find level of common parent
108+
let common = 0;
109+
while (parents[common] === prevParents[common]) {
110+
common++;
108111
}
109-
prev = n;
110-
prevParents = parents;
112+
distances.push(ratios[common]);
111113
}
112-
distances.push(0.5);
114+
prev = n;
115+
prevParents = parents;
113116
}
117+
distances.push(0.5);
114118

115119
const distance = distances.reduce((acc, s) => acc + s, 0);
116120
const factor = total / distance;
@@ -159,22 +163,30 @@ const HierarchicalScale = Scale.extend({
159163
}
160164
}
161165

162-
const node = this._nodes[index];
163-
const centerTick = this.options.offset;
164-
const base = this.isHorizontal() ? this.left : this.top;
165-
166-
return base + node.center - (centerTick ? 0 : node.width / 2);
166+
return this._centerBase(index);
167167
},
168168

169169
getPixelForTick(index) {
170170
if (index === 1 && this._nodes.length === 1) {
171171
// cornercase in chartjs to determine tick with, hard coded 1
172172
return this._nodes[0].width;
173173
}
174+
175+
return this._centerBase(index + this.minIndex);
176+
},
177+
178+
_centerBase(index) {
174179
const centerTick = this.options.offset;
175-
const node = this._nodes[index + this.minIndex];
176180
const base = this.isHorizontal() ? this.left : this.top;
177-
return base + node.center - (centerTick ? 0 : node.width / 2);
181+
const node = this._nodes[index];
182+
183+
if (node == null) {
184+
return base;
185+
}
186+
187+
const nodeCenter = node.center != null ? node.center : 0;
188+
const nodeWidth = node.width != null ? node.width : 0;
189+
return base + nodeCenter - (centerTick ? 0 : nodeWidth / 2);
178190
},
179191

180192
getValueForPixel(pixel) {

0 commit comments

Comments
 (0)