UNPKG

2.72 kBJavaScriptView Raw
1function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
3function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
4
5function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
6
7var Adjust = require('./base');
8var each = require('@antv/util/lib/each');
9
10var MARGIN_RATIO = 1 / 2;
11var DODGE_RATIO = 1 / 2;
12
13var Dodge = function (_Adjust) {
14 _inherits(Dodge, _Adjust);
15
16 function Dodge() {
17 _classCallCheck(this, Dodge);
18
19 return _possibleConstructorReturn(this, _Adjust.apply(this, arguments));
20 }
21
22 Dodge.prototype._initDefaultCfg = function _initDefaultCfg() {
23 /**
24 * 调整过程中,2个数据的间距
25 * @type {Number}
26 */
27 this.marginRatio = MARGIN_RATIO;
28 /**
29 * 调整占单位宽度的比例,例如:占2个分类间距的 1/2
30 * @type {Number}
31 */
32 this.dodgeRatio = DODGE_RATIO;
33 this.adjustNames = ['x', 'y']; // 调整的维度,默认,x,y都做调整
34 };
35
36 Dodge.prototype.getDodgeOffset = function getDodgeOffset(range, index, count) {
37 var self = this;
38 var pre = range.pre;
39 var next = range.next;
40 var tickLength = next - pre;
41 var width = tickLength * self.dodgeRatio / count;
42 var margin = self.marginRatio * width;
43 var offset = 1 / 2 * (tickLength - count * width - (count - 1) * margin) + ((index + 1) * width + index * margin) - 1 / 2 * width - 1 / 2 * tickLength;
44 return (pre + next) / 2 + offset;
45 };
46
47 Dodge.prototype.processAdjust = function processAdjust(dataArray) {
48 var self = this;
49 var count = dataArray.length;
50 var xField = self.xField;
51 each(dataArray, function (data, index) {
52 for (var i = 0, len = data.length; i < len; i++) {
53 var obj = data[i];
54 var value = obj[xField];
55 var range = {
56 pre: value - 0.5,
57 next: value + 0.5
58 };
59 var dodgeValue = self.getDodgeOffset(range, index, count);
60 obj[xField] = dodgeValue;
61 }
62 });
63 };
64
65 return Dodge;
66}(Adjust);
67
68Adjust.Dodge = Dodge;
69module.exports = Dodge;
\No newline at end of file