Skip to content

Commit

Permalink
Merge pull request sgratzl#18 from datavisyn/sluger/15_test
Browse files Browse the repository at this point in the history
Sluger/15 test
  • Loading branch information
Stefan Luger authored Jul 2, 2019
2 parents 8cdaa43 + 3eea065 commit 0fd69d0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"src/**/*.js"
],
"dependencies": {
"chart.js": "^2.7.2"
"chart.js": "^2.8.0"
},
"devDependencies": {
"@babel/core": "^7.4.5",
Expand Down
74 changes: 43 additions & 31 deletions src/scale/hierarchical.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict';

import {scaleService, Scale} from 'chart.js';
import {parentsOf} from '../utils';
import {
scaleService,
Scale
} from 'chart.js';
import {
parentsOf
} from '../utils';

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

const distances = [];
{
let prev = nodes[0];
let prevParents = parentsOf(prev, flat);
distances.push(0.5); // half top level distance before and after

for (let i = 1; i < nodes.length; ++i) {
const n = nodes[i];
const parents = parentsOf(n, flat);
if (prev.parent === n.parent) {
// same parent -> can use the level distance
distances.push(ratios[n.level]);
} else {
// differnt level -> use the distance of the common parent
// find level of common parent
let common = 0;
while (parents[common] === prevParents[common]) {
common++;
}
distances.push(ratios[common]);

let prev = nodes[0];
let prevParents = parentsOf(prev, flat);
distances.push(0.5); // half top level distance before and after

for (let i = 1; i < nodes.length; ++i) {
const n = nodes[i];
const parents = parentsOf(n, flat);
if (prev.parent === n.parent) {
// same parent -> can use the level distance
distances.push(ratios[n.level]);
} else {
// differnt level -> use the distance of the common parent
// find level of common parent
let common = 0;
while (parents[common] === prevParents[common]) {
common++;
}
prev = n;
prevParents = parents;
distances.push(ratios[common]);
}
distances.push(0.5);
prev = n;
prevParents = parents;
}
distances.push(0.5);

const distance = distances.reduce((acc, s) => acc + s, 0);
const factor = total / distance;
Expand Down Expand Up @@ -159,22 +163,30 @@ const HierarchicalScale = Scale.extend({
}
}

const node = this._nodes[index];
const centerTick = this.options.offset;
const base = this.isHorizontal() ? this.left : this.top;

return base + node.center - (centerTick ? 0 : node.width / 2);
return this._centerBase(index);
},

getPixelForTick(index) {
if (index === 1 && this._nodes.length === 1) {
// cornercase in chartjs to determine tick with, hard coded 1
return this._nodes[0].width;
}

return this._centerBase(index + this.minIndex);
},

_centerBase(index) {
const centerTick = this.options.offset;
const node = this._nodes[index + this.minIndex];
const base = this.isHorizontal() ? this.left : this.top;
return base + node.center - (centerTick ? 0 : node.width / 2);
const node = this._nodes[index];

if (node == null) {
return base;
}

const nodeCenter = node.center != null ? node.center : 0;
const nodeWidth = node.width != null ? node.width : 0;
return base + nodeCenter - (centerTick ? 0 : nodeWidth / 2);
},

getValueForPixel(pixel) {
Expand Down

0 comments on commit 0fd69d0

Please sign in to comment.