UNPKG

1.9 kBJavaScriptView Raw
1import { __extends } from "tslib";
2import { Linear as LinearScale } from '../deps/f2-scale/src';
3import { isArray, isNumber } from '@antv/util';
4import { interpolateNumber, interpolateRgb } from '../deps/d3-interpolate/src';
5import Base from './base';
6// 只处理 number 和 color
7var interpolate = function interpolate(a, b) {
8 if (isNumber(b)) {
9 return interpolateNumber(a, b);
10 }
11 return interpolateRgb(a, b);
12};
13var Linear = /** @class */function (_super) {
14 __extends(Linear, _super);
15 function Linear(options) {
16 var _this = _super.call(this, options) || this;
17 _this._updateInterpolate();
18 return _this;
19 }
20 Linear.prototype.createScale = function (scaleConfig) {
21 return new LinearScale(scaleConfig);
22 };
23 Linear.prototype._updateInterpolate = function () {
24 var _a = this.range,
25 min = _a[0],
26 max = _a[1];
27 this.interpolate = interpolate(min, max);
28 };
29 Linear.prototype.update = function (options) {
30 _super.prototype.update.call(this, options);
31 this._updateInterpolate();
32 };
33 Linear.prototype._mapping = function (value) {
34 var _a = this,
35 scale = _a.scale,
36 interpolate = _a.interpolate;
37 if (isArray(value)) {
38 return value.map(function (v) {
39 return interpolate(scale.scale(v));
40 });
41 }
42 return interpolate(scale.scale(value));
43 };
44 Linear.prototype.normalize = function (value) {
45 var scale = this.scale;
46 if (isArray(value)) {
47 return value.map(function (v) {
48 return scale.scale(v);
49 });
50 }
51 return scale.scale(value);
52 };
53 Linear.prototype.convert = function (value) {
54 var range = this.range;
55 var min = range[0],
56 max = range[1];
57 if (isArray(value)) {
58 return value.map(function (v) {
59 return min + (max - min) * v;
60 });
61 }
62 return min + (max - min) * value;
63 };
64 return Linear;
65}(Base);
66export default Linear;
\No newline at end of file