1 | const Adjust = require('./base');
|
2 | const each = require('@antv/util/src/each');
|
3 |
|
4 | const MARGIN_RATIO = 1 / 2;
|
5 | const DODGE_RATIO = 1 / 2;
|
6 |
|
7 | class Dodge extends Adjust {
|
8 |
|
9 | _initDefaultCfg() {
|
10 | |
11 |
|
12 |
|
13 |
|
14 | this.marginRatio = MARGIN_RATIO;
|
15 | |
16 |
|
17 |
|
18 |
|
19 | this.dodgeRatio = DODGE_RATIO;
|
20 | this.adjustNames = [ 'x', 'y' ];
|
21 | }
|
22 |
|
23 | getDodgeOffset(range, index, count) {
|
24 | const self = this;
|
25 | const pre = range.pre;
|
26 | const next = range.next;
|
27 | const tickLength = next - pre;
|
28 | const width = (tickLength * self.dodgeRatio) / count;
|
29 | const margin = self.marginRatio * width;
|
30 | const offset = 1 / 2 * (tickLength - (count) * width - (count - 1) * margin) +
|
31 | ((index + 1) * width + index * margin) -
|
32 | 1 / 2 * width - 1 / 2 * tickLength;
|
33 | return (pre + next) / 2 + offset;
|
34 | }
|
35 |
|
36 | processAdjust(dataArray) {
|
37 | const self = this;
|
38 | const count = dataArray.length;
|
39 | const xField = self.xField;
|
40 | each(dataArray, function(data, index) {
|
41 | for (let i = 0, len = data.length; i < len; i++) {
|
42 | const obj = data[i];
|
43 | const value = obj[xField];
|
44 | const range = {
|
45 | pre: value - 0.5,
|
46 | next: value + 0.5
|
47 | };
|
48 | const dodgeValue = self.getDodgeOffset(range, index, count);
|
49 | obj[xField] = dodgeValue;
|
50 | }
|
51 | });
|
52 | }
|
53 | }
|
54 |
|
55 | Adjust.Dodge = Dodge;
|
56 | module.exports = Dodge;
|