|
1 | 1 | 'use strict';
|
2 | 2 |
|
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'; |
5 | 10 |
|
6 | 11 | const defaultConfig = Object.assign({}, scaleService.getScaleDefaults('category'), {
|
7 | 12 | /**
|
@@ -86,31 +91,30 @@ const HierarchicalScale = Scale.extend({
|
86 | 91 | const ratios = [1, Math.pow(ratio, 1), Math.pow(ratio, 2), Math.pow(ratio, 3), Math.pow(ratio, 4)];
|
87 | 92 |
|
88 | 93 | 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++; |
108 | 111 | }
|
109 |
| - prev = n; |
110 |
| - prevParents = parents; |
| 112 | + distances.push(ratios[common]); |
111 | 113 | }
|
112 |
| - distances.push(0.5); |
| 114 | + prev = n; |
| 115 | + prevParents = parents; |
113 | 116 | }
|
| 117 | + distances.push(0.5); |
114 | 118 |
|
115 | 119 | const distance = distances.reduce((acc, s) => acc + s, 0);
|
116 | 120 | const factor = total / distance;
|
@@ -159,22 +163,30 @@ const HierarchicalScale = Scale.extend({
|
159 | 163 | }
|
160 | 164 | }
|
161 | 165 |
|
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); |
167 | 167 | },
|
168 | 168 |
|
169 | 169 | getPixelForTick(index) {
|
170 | 170 | if (index === 1 && this._nodes.length === 1) {
|
171 | 171 | // cornercase in chartjs to determine tick with, hard coded 1
|
172 | 172 | return this._nodes[0].width;
|
173 | 173 | }
|
| 174 | + |
| 175 | + return this._centerBase(index + this.minIndex); |
| 176 | + }, |
| 177 | + |
| 178 | + _centerBase(index) { |
174 | 179 | const centerTick = this.options.offset;
|
175 |
| - const node = this._nodes[index + this.minIndex]; |
176 | 180 | 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); |
178 | 190 | },
|
179 | 191 |
|
180 | 192 | getValueForPixel(pixel) {
|
|
0 commit comments