continuous.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Continuous = void 0;
  4. var tslib_1 = require("tslib");
  5. var g_1 = require("@antv/g");
  6. var scale_1 = require("@antv/scale");
  7. var util_1 = require("@antv/util");
  8. var core_1 = require("../../core");
  9. var util_2 = require("../../util");
  10. var axis_1 = require("../axis");
  11. var constant_1 = require("../axis/constant");
  12. var indicator_1 = require("../indicator");
  13. var title_1 = require("../title");
  14. var constant_2 = require("./constant");
  15. var handle_1 = require("./continuous/handle");
  16. var ribbon_1 = require("./continuous/ribbon");
  17. var utils_1 = require("./continuous/utils");
  18. var utils_2 = require("./utils");
  19. var handle_2 = require("../slider/handle");
  20. var getMinMax = (0, util_1.memoize)(function (data) {
  21. return {
  22. min: Math.min.apply(Math, tslib_1.__spreadArray([], tslib_1.__read(data.map(function (d) { return d.value; })), false)),
  23. max: Math.max.apply(Math, tslib_1.__spreadArray([], tslib_1.__read(data.map(function (d) { return d.value; })), false)),
  24. };
  25. }, function (data) { return data.map(function (d) { return d.id; }); });
  26. var Continuous = /** @class */ (function (_super) {
  27. tslib_1.__extends(Continuous, _super);
  28. function Continuous(options) {
  29. var _this = _super.call(this, options, constant_2.CONTINUOUS_DEFAULT_OPTIONS) || this;
  30. _this.eventToOffsetScale = new scale_1.Linear({});
  31. _this.innerRibbonScale = new scale_1.Linear({});
  32. _this.cacheLabelBBox = null;
  33. _this.cacheHandleBBox = null;
  34. _this.onHovering = function (e) {
  35. var _a = _this.attributes, data = _a.data, block = _a.block;
  36. e.stopPropagation();
  37. var value = _this.getValueByCanvasPoint(e);
  38. if (block) {
  39. var range = (0, utils_1.getNextTickValue)(data.map(function (_a) {
  40. var value = _a.value;
  41. return value;
  42. }), value).range;
  43. _this.showIndicator((range[0] + range[1]) / 2, "".concat(range[0], "-").concat(range[1]));
  44. _this.dispatchIndicated(value, range);
  45. }
  46. else {
  47. var safetyValue = _this.getTickValue(value);
  48. _this.showIndicator(safetyValue);
  49. _this.dispatchIndicated(safetyValue);
  50. }
  51. };
  52. _this.onDragStart = function (target) { return function (e) {
  53. e.stopPropagation();
  54. // 关闭滑动
  55. if (!_this.attributes.slidable)
  56. return;
  57. _this.target = target;
  58. _this.prevValue = _this.getTickValue(_this.getValueByCanvasPoint(e));
  59. document.addEventListener('mousemove', _this.onDragging);
  60. document.addEventListener('touchmove', _this.onDragging);
  61. document.addEventListener('mouseleave', _this.onDragEnd);
  62. document.addEventListener('mouseup', _this.onDragEnd);
  63. document.addEventListener('mouseup', _this.onDragEnd);
  64. document.addEventListener('touchend', _this.onDragEnd);
  65. }; };
  66. _this.onDragging = function (e) {
  67. var target = _this.target;
  68. _this.updateMouse();
  69. var _a = tslib_1.__read(_this.selection, 2), start = _a[0], end = _a[1];
  70. var currValue = _this.getTickValue(_this.getValueByCanvasPoint(e));
  71. var diffValue = currValue - _this.prevValue;
  72. if (target === 'start')
  73. start !== currValue && _this.updateSelection(currValue, end);
  74. else if (target === 'end')
  75. end !== currValue && _this.updateSelection(start, currValue);
  76. else if (target === 'ribbon' && diffValue !== 0) {
  77. _this.prevValue = currValue;
  78. _this.updateSelection(diffValue, diffValue, true);
  79. }
  80. };
  81. _this.onDragEnd = function () {
  82. _this.style.cursor = 'default';
  83. document.removeEventListener('mousemove', _this.onDragging);
  84. document.removeEventListener('touchmove', _this.onDragging);
  85. document.removeEventListener('mouseup', _this.onDragEnd);
  86. document.removeEventListener('touchend', _this.onDragEnd);
  87. };
  88. return _this;
  89. }
  90. Object.defineProperty(Continuous.prototype, "handleOffsetRatio", {
  91. get: function () {
  92. return this.ifHorizontal(0.5, 0.5);
  93. },
  94. enumerable: false,
  95. configurable: true
  96. });
  97. Continuous.prototype.getBBox = function () {
  98. var _a = this.attributes, width = _a.width, height = _a.height;
  99. return new util_2.BBox(0, 0, width, height);
  100. };
  101. Continuous.prototype.render = function (attributes, container) {
  102. var _this = this;
  103. // 渲染顺序
  104. // 1. 绘制 title, 获得可用空间
  105. // 2. 绘制 label, handle
  106. // 3. 基于可用空间、label高度、handle 宽高,计算 ribbon 宽高
  107. // 4. 绘制 ribbon
  108. // 5. 调整 label、handle 位置
  109. var showLabel = attributes.showLabel;
  110. /** title */
  111. this.renderTitle((0, util_2.select)(container));
  112. var _a = this.availableSpace, x = _a.x, y = _a.y;
  113. /** label */
  114. /** content */
  115. var contentGroup = (0, util_2.select)(container).maybeAppendByClassName(constant_2.CLASS_NAMES.contentGroup, 'g').styles({ x: x, y: y });
  116. var labelGroup = contentGroup.maybeAppendByClassName(constant_2.CLASS_NAMES.labelGroup, 'g').styles({ zIndex: 1 });
  117. (0, util_2.ifShow)(!!showLabel, labelGroup, function (group) {
  118. _this.renderLabel(group);
  119. });
  120. var ribbonGroup = contentGroup.maybeAppendByClassName(constant_2.CLASS_NAMES.ribbonGroup, 'g').styles({ zIndex: 0 });
  121. /** handle */
  122. this.handlesGroup = contentGroup.maybeAppendByClassName(constant_2.CLASS_NAMES.handlesGroup, 'g').styles({ zIndex: 2 });
  123. this.renderHandles();
  124. /** ribbon */
  125. this.renderRibbon(ribbonGroup);
  126. this.renderIndicator(contentGroup);
  127. /** adjust */
  128. this.adjustLabel();
  129. this.adjustHandles();
  130. };
  131. Object.defineProperty(Continuous.prototype, "range", {
  132. get: function () {
  133. var data = this.attributes.data;
  134. return getMinMax(data);
  135. },
  136. enumerable: false,
  137. configurable: true
  138. });
  139. Object.defineProperty(Continuous.prototype, "ribbonScale", {
  140. get: function () {
  141. var _a = this.range, min = _a.min, max = _a.max;
  142. this.innerRibbonScale.update({
  143. domain: [min, max],
  144. range: [0, 1],
  145. });
  146. return this.innerRibbonScale;
  147. },
  148. enumerable: false,
  149. configurable: true
  150. });
  151. Object.defineProperty(Continuous.prototype, "ribbonRange", {
  152. get: function () {
  153. var _a = tslib_1.__read(this.selection, 2), min = _a[0], max = _a[1];
  154. var scale = this.ribbonScale;
  155. return [scale.map(min), scale.map(max)];
  156. },
  157. enumerable: false,
  158. configurable: true
  159. });
  160. Object.defineProperty(Continuous.prototype, "selection", {
  161. get: function () {
  162. var _a = this.range, min = _a.min, max = _a.max;
  163. var _b = this.attributes.defaultValue, _c = _b === void 0 ? [min, max] : _b, _d = tslib_1.__read(_c, 2), start = _d[0], end = _d[1];
  164. return [start, end];
  165. },
  166. enumerable: false,
  167. configurable: true
  168. });
  169. Continuous.prototype.ifHorizontal = function (a, b) {
  170. return (0, utils_2.ifHorizontal)(this.attributes.orientation, typeof a === 'function' ? a() : a, typeof b === 'function' ? b() : b);
  171. };
  172. Continuous.prototype.renderTitle = function (container) {
  173. var _a = this.attributes, showTitle = _a.showTitle, titleText = _a.titleText, width = _a.width, height = _a.height;
  174. var style = (0, util_2.subStyleProps)(this.attributes, 'title');
  175. var finalTitleStyle = tslib_1.__assign(tslib_1.__assign({}, style), { width: width, height: height, text: titleText });
  176. var that = this;
  177. container
  178. .selectAll(constant_2.CLASS_NAMES.title.class)
  179. .data(showTitle ? [titleText] : [])
  180. .join(function (enter) {
  181. return enter
  182. .append(function () { return new title_1.Title({ style: finalTitleStyle }); })
  183. .attr('className', constant_2.CLASS_NAMES.title.name)
  184. .each(function () {
  185. that.title = this;
  186. });
  187. }, function (update) { return update.update(finalTitleStyle); }, function (exit) {
  188. return exit
  189. .each(function () {
  190. that.title = undefined;
  191. })
  192. .remove();
  193. });
  194. };
  195. Object.defineProperty(Continuous.prototype, "availableSpace", {
  196. get: function () {
  197. if (this.title)
  198. return this.title.getAvailableSpace();
  199. var _a = this.attributes, width = _a.width, height = _a.height;
  200. return new util_2.BBox(0, 0, width, height);
  201. },
  202. enumerable: false,
  203. configurable: true
  204. });
  205. Object.defineProperty(Continuous.prototype, "labelFixedSpacing", {
  206. get: function () {
  207. var showTick = this.attributes.showTick;
  208. return showTick ? 5 : 0;
  209. },
  210. enumerable: false,
  211. configurable: true
  212. });
  213. Object.defineProperty(Continuous.prototype, "labelPosition", {
  214. get: function () {
  215. var _a = this.attributes, orientation = _a.orientation, labelDirection = _a.labelDirection;
  216. var positions = {
  217. vertical: { positive: 'right', negative: 'left' },
  218. horizontal: { positive: 'bottom', negative: 'top' },
  219. };
  220. return positions[orientation][labelDirection];
  221. },
  222. enumerable: false,
  223. configurable: true
  224. });
  225. Object.defineProperty(Continuous.prototype, "labelBBox", {
  226. get: function () {
  227. var _a;
  228. var showLabel = this.attributes.showLabel;
  229. if (!showLabel)
  230. return new util_2.BBox(0, 0, 0, 0);
  231. if (this.cacheLabelBBox)
  232. return this.cacheLabelBBox;
  233. var _b = ((_a = this.label.querySelector(constant_1.CLASS_NAMES.labelGroup.class)) === null || _a === void 0 ? void 0 : _a.children.slice(-1)[0]).getBBox(), width = _b.width, height = _b.height;
  234. this.cacheLabelBBox = new util_2.BBox(0, 0, width, height);
  235. return this.cacheLabelBBox;
  236. },
  237. enumerable: false,
  238. configurable: true
  239. });
  240. Object.defineProperty(Continuous.prototype, "labelShape", {
  241. get: function () {
  242. var _a = this.attributes, showLabel = _a.showLabel, _b = _a.labelSpacing, labelSpacing = _b === void 0 ? 0 : _b;
  243. if (!showLabel)
  244. return { width: 0, height: 0, size: 0, length: 0 };
  245. var _c = this.labelBBox, width = _c.width, height = _c.height;
  246. var size = this.ifHorizontal(height, width) + labelSpacing + this.labelFixedSpacing;
  247. var length = this.ifHorizontal(width, height);
  248. return { width: width, height: height, size: size, length: length };
  249. },
  250. enumerable: false,
  251. configurable: true
  252. });
  253. Object.defineProperty(Continuous.prototype, "ribbonBBox", {
  254. get: function () {
  255. var _a = this.attributes, showHandle = _a.showHandle, userDefinedRibbonSize = _a.ribbonSize;
  256. var _b = this.availableSpace, availableWidth = _b.width, availableHeight = _b.height;
  257. var _c = this.labelShape, labelSize = _c.size, labelLength = _c.length;
  258. var _d = tslib_1.__read(this.ifHorizontal([availableHeight, availableWidth], [availableWidth, availableHeight]), 2), availableSize = _d[0], availableLength = _d[1];
  259. var _e = showHandle ? this.handleShape : { size: 0, length: 0 }, handleSize = _e.size, handleLength = _e.length;
  260. var handleRatio = this.handleOffsetRatio;
  261. var ribbonSize = 0;
  262. var labelPosition = this.labelPosition;
  263. if (userDefinedRibbonSize) {
  264. ribbonSize = userDefinedRibbonSize;
  265. }
  266. else if (['bottom', 'right'].includes(labelPosition)) {
  267. ribbonSize = Math.min(availableSize - labelSize, (availableSize - handleSize) / handleRatio);
  268. }
  269. else if (availableSize * (1 - handleRatio) > handleSize) {
  270. ribbonSize = Math.max(availableSize - labelSize, 0);
  271. }
  272. else
  273. ribbonSize = Math.max((availableSize - labelSize - handleSize) / handleRatio, 0);
  274. var edgeLength = Math.max(handleLength, labelLength);
  275. var ribbonLength = availableLength - edgeLength;
  276. var _f = tslib_1.__read(this.ifHorizontal([ribbonLength, ribbonSize], [ribbonSize, ribbonLength]), 2), width = _f[0], height = _f[1];
  277. // 需要考虑 handle 的占用空间
  278. // todo 为了防止因为 handle 文本变化导致的 ribbon 位置变化,handle size 取最大值
  279. var finalLabelOccupy = ['top', 'left'].includes(labelPosition) ? labelSize : 0;
  280. var _g = tslib_1.__read(this.ifHorizontal([edgeLength / 2, finalLabelOccupy], [finalLabelOccupy, edgeLength / 2]), 2), x = _g[0], y = _g[1];
  281. return new util_2.BBox(x, y, width, height);
  282. },
  283. enumerable: false,
  284. configurable: true
  285. });
  286. Object.defineProperty(Continuous.prototype, "ribbonShape", {
  287. get: function () {
  288. var _a = this.ribbonBBox, width = _a.width, height = _a.height;
  289. return this.ifHorizontal({ size: height, length: width }, { size: width, length: height });
  290. },
  291. enumerable: false,
  292. configurable: true
  293. });
  294. Continuous.prototype.renderRibbon = function (container) {
  295. var _a = this.attributes, data = _a.data, type = _a.type, orientation = _a.orientation, color = _a.color, block = _a.block;
  296. var ribbonStyle = (0, util_2.subStyleProps)(this.attributes, 'ribbon');
  297. var _b = this.range, min = _b.min, max = _b.max;
  298. var _c = this.ribbonBBox, x = _c.x, y = _c.y;
  299. var _d = this.ribbonShape, length = _d.length, size = _d.size;
  300. var style = (0, util_2.deepAssign)({
  301. x: x,
  302. y: y,
  303. length: length,
  304. size: size,
  305. type: type,
  306. orientation: orientation,
  307. color: color,
  308. block: block,
  309. partition: data.map(function (d) { return (d.value - min) / (max - min); }),
  310. range: this.ribbonRange,
  311. }, ribbonStyle);
  312. this.ribbon = container.maybeAppendByClassName(constant_2.CLASS_NAMES.ribbon, function () { return new ribbon_1.Ribbon({ style: style }); }).update(style);
  313. };
  314. Continuous.prototype.getHandleClassName = function (type) {
  315. // @ts-ignore
  316. return "".concat(constant_2.CLASS_NAMES.prefix("".concat(type, "-handle")));
  317. };
  318. Continuous.prototype.renderHandles = function () {
  319. var _a = this.attributes, showHandle = _a.showHandle, orientation = _a.orientation;
  320. var handleStyle = (0, util_2.subStyleProps)(this.attributes, 'handle');
  321. var _b = tslib_1.__read(this.selection, 2), min = _b[0], max = _b[1];
  322. var style = tslib_1.__assign(tslib_1.__assign({}, handleStyle), { orientation: orientation });
  323. var _c = handleStyle.shape, shape = _c === void 0 ? 'slider' : _c;
  324. var HandleCtor = shape === 'basic' ? handle_1.Handle : handle_2.Handle;
  325. var that = this;
  326. this.handlesGroup
  327. .selectAll(constant_2.CLASS_NAMES.handle.class)
  328. .data(showHandle
  329. ? [
  330. { value: min, type: 'start' },
  331. { value: max, type: 'end' },
  332. ]
  333. : [], function (d) { return d.type; })
  334. .join(function (enter) {
  335. return enter
  336. .append(function () { return new HandleCtor({ style: style }); })
  337. .attr('className', function (_a) {
  338. var type = _a.type;
  339. return "".concat(constant_2.CLASS_NAMES.handle, " ").concat(that.getHandleClassName(type));
  340. })
  341. .each(function (_a) {
  342. var type = _a.type, labelText = _a.value;
  343. this.update({ labelText: labelText });
  344. var name = "".concat(type, "Handle");
  345. that[name] = this;
  346. this.addEventListener('pointerdown', that.onDragStart(type));
  347. });
  348. }, function (update) {
  349. return update.update(style).each(function (_a) {
  350. var labelText = _a.value;
  351. this.update({ labelText: labelText });
  352. });
  353. }, function (exit) {
  354. return exit
  355. .each(function (_a) {
  356. var type = _a.type;
  357. var name = "".concat(type, "Handle");
  358. that[name] = undefined;
  359. })
  360. .remove();
  361. });
  362. };
  363. Continuous.prototype.adjustHandles = function () {
  364. var _a = tslib_1.__read(this.selection, 2), min = _a[0], max = _a[1];
  365. this.setHandlePosition('start', min);
  366. this.setHandlePosition('end', max);
  367. };
  368. Object.defineProperty(Continuous.prototype, "handleBBox", {
  369. get: function () {
  370. if (this.cacheHandleBBox)
  371. return this.cacheHandleBBox;
  372. if (!this.attributes.showHandle)
  373. return new util_2.BBox(0, 0, 0, 0);
  374. var _a = this.startHandle.getBBox(), startHandleWidth = _a.width, startHandleHeight = _a.height;
  375. var _b = this.endHandle.getBBox(), endHandleWidth = _b.width, endHandleHeight = _b.height;
  376. var _c = tslib_1.__read([Math.max(startHandleWidth, endHandleWidth), Math.max(startHandleHeight, endHandleHeight)], 2), width = _c[0], height = _c[1];
  377. this.cacheHandleBBox = new util_2.BBox(0, 0, width, height);
  378. return this.cacheHandleBBox;
  379. },
  380. enumerable: false,
  381. configurable: true
  382. });
  383. Object.defineProperty(Continuous.prototype, "handleShape", {
  384. /**
  385. * 因为 handle label 的宽高是动态的,所以 handle bbox 是第一次渲染时的 bbox
  386. */
  387. get: function () {
  388. var _a = this.handleBBox, width = _a.width, height = _a.height;
  389. var _b = tslib_1.__read(this.ifHorizontal([height, width], [width, height]), 2), size = _b[0], length = _b[1];
  390. return { width: width, height: height, size: size, length: length };
  391. },
  392. enumerable: false,
  393. configurable: true
  394. });
  395. Continuous.prototype.setHandlePosition = function (type, value) {
  396. var handleFormatter = this.attributes.handleFormatter;
  397. var _a = this.ribbonBBox, ribbonX = _a.x, ribbonY = _a.y;
  398. var ribbonSize = this.ribbonShape.size;
  399. var offset = this.getOffset(value);
  400. var _b = tslib_1.__read(this.ifHorizontal([ribbonX + offset, ribbonY + ribbonSize * this.handleOffsetRatio], [ribbonX + ribbonSize * this.handleOffsetRatio, ribbonY + offset]), 2), x = _b[0], y = _b[1];
  401. var handle = this.handlesGroup.select(".".concat(this.getHandleClassName(type))).node();
  402. handle === null || handle === void 0 ? void 0 : handle.update({ x: x, y: y, formatter: handleFormatter });
  403. };
  404. Continuous.prototype.renderIndicator = function (container) {
  405. var style = (0, util_2.subStyleProps)(this.attributes, 'indicator');
  406. this.indicator = container.maybeAppendByClassName(constant_2.CLASS_NAMES.indicator, function () { return new indicator_1.Indicator({}); }).update(style);
  407. // this.hideIndicator();
  408. };
  409. Object.defineProperty(Continuous.prototype, "labelData", {
  410. get: function () {
  411. var _this = this;
  412. var data = this.attributes.data;
  413. return data.reduce(function (acc, curr, index, arr) {
  414. var _a, _b;
  415. var id = (_a = curr === null || curr === void 0 ? void 0 : curr.id) !== null && _a !== void 0 ? _a : index.toString();
  416. acc.push(tslib_1.__assign(tslib_1.__assign({}, curr), { id: id, index: index, type: 'value', label: (_b = curr === null || curr === void 0 ? void 0 : curr.label) !== null && _b !== void 0 ? _b : curr.value.toString(), value: _this.ribbonScale.map(curr.value) }));
  417. if (index < arr.length - 1) {
  418. var next = arr[index + 1];
  419. var _c = tslib_1.__read([curr.value, next.value], 2), cr = _c[0], nx = _c[1];
  420. var midVal = (cr + nx) / 2;
  421. acc.push(tslib_1.__assign(tslib_1.__assign({}, curr), { id: id, index: index, type: 'range', range: [cr, nx], label: [cr, nx].join('~'), value: _this.ribbonScale.map(midVal) }));
  422. }
  423. return acc;
  424. }, []);
  425. },
  426. enumerable: false,
  427. configurable: true
  428. });
  429. Object.defineProperty(Continuous.prototype, "labelStyle", {
  430. get: function () {
  431. var _a = tslib_1.__read(['center', 'middle'], 2), labelTextAlign = _a[0], labelTextBaseline = _a[1];
  432. var labelPosition = this.labelPosition;
  433. if (labelPosition === 'top')
  434. labelTextBaseline = 'bottom';
  435. else if (labelPosition === 'bottom')
  436. labelTextBaseline = 'top';
  437. else if (labelPosition === 'left')
  438. labelTextAlign = 'end';
  439. else if (labelPosition === 'right')
  440. labelTextAlign = 'start';
  441. return {
  442. labelTextAlign: labelTextAlign,
  443. labelTextBaseline: labelTextBaseline,
  444. };
  445. },
  446. enumerable: false,
  447. configurable: true
  448. });
  449. Continuous.prototype.renderLabel = function (container) {
  450. var _a = this.attributes, _b = _a.showTick, showTick = _b === void 0 ? false : _b, labelFilter = _a.labelFilter, labelFormatter = _a.labelFormatter;
  451. var tickStyle = (0, util_2.subStyleProps)(this.attributes, 'tick');
  452. var labelStyle = (0, util_2.subStyleProps)(this.attributes, 'label');
  453. var align = labelStyle.align;
  454. var style = (0, util_2.deepAssign)(tslib_1.__assign({ showLine: false, showGrid: false, showTick: showTick, type: 'linear', startPos: [0, 0], endPos: [0, 0], tickDirection: 'negative', labelTransform: 'rotate(0)' }, this.labelStyle), (0, util_2.superStyleProps)(tickStyle, 'tick'), (0, util_2.superStyleProps)(labelStyle, 'label'), { data: this.labelData });
  455. var functionStyle = {
  456. tickFilter: function (datum, index, data) {
  457. if ((datum === null || datum === void 0 ? void 0 : datum.type) !== 'value')
  458. return false;
  459. if (labelFilter)
  460. return labelFilter(datum, datum.index, data.filter(function (d) { return d.type !== 'value'; }));
  461. return true;
  462. },
  463. labelFilter: function (datum, index, data) {
  464. if ((datum === null || datum === void 0 ? void 0 : datum.type) !== align)
  465. return false;
  466. if (labelFilter)
  467. return labelFilter(datum, datum.index, data.filter(function (d) { return d.type === align; }));
  468. return true;
  469. },
  470. labelFormatter: labelFormatter,
  471. };
  472. var finalLabelStyle = tslib_1.__assign(tslib_1.__assign({}, style), functionStyle);
  473. this.label = container.maybeAppendByClassName(constant_2.CLASS_NAMES.label, function () { return new axis_1.Axis({ style: finalLabelStyle }); }).node();
  474. this.label.update(finalLabelStyle, false);
  475. };
  476. Object.defineProperty(Continuous.prototype, "labelAxisStyle", {
  477. get: function () {
  478. // @ts-ignore
  479. var _a = this.attributes, showTick = _a.showTick, labelDirection = _a.labelDirection, labelSpacing = _a.labelSpacing, definedTickLength = _a.tickLength;
  480. var ribbonSize = this.ribbonShape.size;
  481. var labelPosition = this.labelPosition;
  482. var labelFixedSpacing = this.labelFixedSpacing;
  483. var _b = tslib_1.__read([0, 0, 0], 3), offset = _b[0], spacing = _b[1], tickLength = _b[2];
  484. var internalVal = definedTickLength !== null && definedTickLength !== void 0 ? definedTickLength : ribbonSize;
  485. if (showTick) {
  486. tickLength = internalVal;
  487. spacing = labelFixedSpacing;
  488. if (labelDirection === 'positive') {
  489. if (labelPosition === 'right') {
  490. offset = internalVal;
  491. tickLength = internalVal;
  492. }
  493. else if (labelPosition === 'bottom')
  494. offset = tickLength;
  495. }
  496. else if (labelDirection === 'negative') {
  497. if (labelPosition === 'top')
  498. offset = ribbonSize;
  499. else if (labelPosition === 'left')
  500. offset = ribbonSize;
  501. }
  502. }
  503. else if (labelDirection === 'positive') {
  504. if (labelPosition === 'right')
  505. spacing = internalVal;
  506. else if (labelPosition === 'bottom') {
  507. offset = ribbonSize + labelFixedSpacing;
  508. spacing = labelSpacing;
  509. }
  510. }
  511. else if (labelDirection === 'negative') {
  512. if (labelPosition === 'left')
  513. spacing = labelSpacing;
  514. else if (labelPosition === 'top')
  515. spacing = labelSpacing;
  516. }
  517. return { offset: offset, spacing: labelSpacing, tickLength: tickLength };
  518. },
  519. enumerable: false,
  520. configurable: true
  521. });
  522. Continuous.prototype.adjustLabel = function () {
  523. var showLabel = this.attributes.showLabel;
  524. if (!showLabel)
  525. return;
  526. var _a = this.ribbonBBox, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
  527. var _b = this.labelAxisStyle, axisOffset = _b.offset, axisSpacing = _b.spacing, axisTickLength = _b.tickLength;
  528. var _c = tslib_1.__read(this.ifHorizontal([
  529. [x, y + axisOffset],
  530. [x + width, y + axisOffset],
  531. ], [
  532. [x + axisOffset, y + height],
  533. [x + axisOffset, y],
  534. ]), 2), startPos = _c[0], endPos = _c[1];
  535. this.label.update({
  536. startPos: startPos,
  537. endPos: endPos,
  538. tickLength: axisTickLength,
  539. labelSpacing: axisSpacing,
  540. }, false);
  541. };
  542. Continuous.prototype.bindEvents = function () {
  543. // 绑定 drag 开始事件
  544. this.ribbon.on('pointerdown', this.onDragStart('ribbon'));
  545. this.ribbon.on('pointermove', this.onHovering);
  546. this.addEventListener('pointerout', this.hideIndicator);
  547. };
  548. Continuous.prototype.showIndicator = function (value, text) {
  549. if (text === void 0) { text = "".concat(value); }
  550. var showIndicator = this.attributes.showIndicator;
  551. if (!showIndicator || typeof value !== 'number') {
  552. this.hideIndicator();
  553. return;
  554. }
  555. var _a = this.range, min = _a.min, max = _a.max;
  556. var _b = this.ribbonBBox, x = _b.x, y = _b.y;
  557. var safeValue = (0, util_1.clamp)(value, min, max);
  558. var offset = this.getOffset(safeValue);
  559. var pos = this.ifHorizontal([offset + x, y], [x, offset + y]);
  560. this.indicator.update({
  561. x: pos[0],
  562. y: pos[1],
  563. position: this.ifHorizontal('top', 'left'),
  564. labelText: text,
  565. });
  566. (0, util_2.show)(this.indicator.node());
  567. };
  568. Continuous.prototype.hideIndicator = function () {
  569. (0, util_2.hide)(this.indicator.node());
  570. };
  571. Continuous.prototype.updateMouse = function () {
  572. if (this.attributes.slidable)
  573. this.style.cursor = 'grabbing';
  574. };
  575. Continuous.prototype.setSelection = function (start, end) {
  576. this.updateSelection(start, end);
  577. };
  578. Continuous.prototype.updateSelection = function (stVal, endVal, isOffset) {
  579. var _a;
  580. if (isOffset === void 0) { isOffset = false; }
  581. var _b = tslib_1.__read(this.selection, 2), currSt = _b[0], currEnd = _b[1];
  582. var _c = tslib_1.__read([stVal, endVal], 2), start = _c[0], end = _c[1];
  583. if (isOffset) {
  584. // 获取当前值
  585. start += currSt;
  586. end += currEnd;
  587. }
  588. // 值校验
  589. var _d = this.range, min = _d.min, max = _d.max;
  590. _a = tslib_1.__read((0, utils_2.getSafetySelections)([min, max], [start, end], this.selection), 2), start = _a[0], end = _a[1];
  591. this.update({ defaultValue: [start, end] });
  592. this.dispatchSelection();
  593. };
  594. Object.defineProperty(Continuous.prototype, "step", {
  595. get: function () {
  596. var _a = this.attributes.step, step = _a === void 0 ? 1 : _a;
  597. var _b = this.range, min = _b.min, max = _b.max;
  598. if ((0, util_1.isUndefined)(step))
  599. return (0, util_2.toPrecision)((max - min) * constant_2.STEP_RATIO, 0);
  600. return step;
  601. },
  602. enumerable: false,
  603. configurable: true
  604. });
  605. Continuous.prototype.getTickValue = function (value) {
  606. var _a = this.attributes, data = _a.data, block = _a.block;
  607. var min = this.range.min;
  608. if (block)
  609. return (0, utils_1.getNextTickValue)(data.map(function (_a) {
  610. var value = _a.value;
  611. return value;
  612. }), value).tick;
  613. return (0, utils_2.getStepValueByValue)(value, this.step, min);
  614. };
  615. /**
  616. * 事件触发的位置对应的value值
  617. * @param limit {boolean} 我也忘了要干啥了
  618. */
  619. Continuous.prototype.getValueByCanvasPoint = function (e, limit) {
  620. if (limit === void 0) { limit = false; }
  621. var _a = this.range, min = _a.min, max = _a.max;
  622. var _b = tslib_1.__read(this.ribbon.node().getPosition(), 2), x = _b[0], y = _b[1];
  623. var startPos = this.ifHorizontal(x, y);
  624. var currValue = this.ifHorizontal.apply(this, tslib_1.__spreadArray([], tslib_1.__read((0, util_2.getEventPos)(e)), false));
  625. var offset = currValue - startPos;
  626. var value = (0, util_1.clamp)(this.getOffset(offset, true), min, max);
  627. return value;
  628. };
  629. /** reverse: 屏幕偏移量 -> 值 */
  630. Continuous.prototype.getOffset = function (value, reverse) {
  631. if (reverse === void 0) { reverse = false; }
  632. var _a = this.range, min = _a.min, max = _a.max;
  633. var ribbonLen = this.ribbonShape.length;
  634. var scale = this.eventToOffsetScale;
  635. scale.update({ domain: [min, max], range: [0, ribbonLen] });
  636. if (reverse)
  637. return scale.invert(value);
  638. return scale.map(value);
  639. };
  640. Continuous.prototype.dispatchSelection = function () {
  641. var evt = new g_1.CustomEvent('valuechange', {
  642. detail: {
  643. value: this.selection,
  644. },
  645. });
  646. this.dispatchEvent(evt);
  647. };
  648. Continuous.prototype.dispatchIndicated = function (value, range) {
  649. var evt = new g_1.CustomEvent('indicate', {
  650. detail: { value: value, range: range },
  651. });
  652. this.dispatchEvent(evt);
  653. };
  654. return Continuous;
  655. }(core_1.GUI));
  656. exports.Continuous = Continuous;
  657. //# sourceMappingURL=continuous.js.map