UNPKG

1.56 kBJavaScriptView Raw
1const Adjust = require('./base');
2const each = require('@antv/util/lib/each');
3
4const MARGIN_RATIO = 1 / 2;
5const DODGE_RATIO = 1 / 2;
6
7class Dodge extends Adjust {
8
9 _initDefaultCfg() {
10 /**
11 * 调整过程中,2个数据的间距
12 * @type {Number}
13 */
14 this.marginRatio = MARGIN_RATIO;
15 /**
16 * 调整占单位宽度的比例,例如:占2个分类间距的 1/2
17 * @type {Number}
18 */
19 this.dodgeRatio = DODGE_RATIO;
20 this.adjustNames = [ 'x', 'y' ]; // 调整的维度,默认,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
55Adjust.Dodge = Dodge;
56module.exports = Dodge;