| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Continuous = void 0;
- var tslib_1 = require("tslib");
- var g_1 = require("@antv/g");
- var scale_1 = require("@antv/scale");
- var util_1 = require("@antv/util");
- var core_1 = require("../../core");
- var util_2 = require("../../util");
- var axis_1 = require("../axis");
- var constant_1 = require("../axis/constant");
- var indicator_1 = require("../indicator");
- var title_1 = require("../title");
- var constant_2 = require("./constant");
- var handle_1 = require("./continuous/handle");
- var ribbon_1 = require("./continuous/ribbon");
- var utils_1 = require("./continuous/utils");
- var utils_2 = require("./utils");
- var handle_2 = require("../slider/handle");
- var getMinMax = (0, util_1.memoize)(function (data) {
- return {
- min: Math.min.apply(Math, tslib_1.__spreadArray([], tslib_1.__read(data.map(function (d) { return d.value; })), false)),
- max: Math.max.apply(Math, tslib_1.__spreadArray([], tslib_1.__read(data.map(function (d) { return d.value; })), false)),
- };
- }, function (data) { return data.map(function (d) { return d.id; }); });
- var Continuous = /** @class */ (function (_super) {
- tslib_1.__extends(Continuous, _super);
- function Continuous(options) {
- var _this = _super.call(this, options, constant_2.CONTINUOUS_DEFAULT_OPTIONS) || this;
- _this.eventToOffsetScale = new scale_1.Linear({});
- _this.innerRibbonScale = new scale_1.Linear({});
- _this.cacheLabelBBox = null;
- _this.cacheHandleBBox = null;
- _this.onHovering = function (e) {
- var _a = _this.attributes, data = _a.data, block = _a.block;
- e.stopPropagation();
- var value = _this.getValueByCanvasPoint(e);
- if (block) {
- var range = (0, utils_1.getNextTickValue)(data.map(function (_a) {
- var value = _a.value;
- return value;
- }), value).range;
- _this.showIndicator((range[0] + range[1]) / 2, "".concat(range[0], "-").concat(range[1]));
- _this.dispatchIndicated(value, range);
- }
- else {
- var safetyValue = _this.getTickValue(value);
- _this.showIndicator(safetyValue);
- _this.dispatchIndicated(safetyValue);
- }
- };
- _this.onDragStart = function (target) { return function (e) {
- e.stopPropagation();
- // 关闭滑动
- if (!_this.attributes.slidable)
- return;
- _this.target = target;
- _this.prevValue = _this.getTickValue(_this.getValueByCanvasPoint(e));
- document.addEventListener('mousemove', _this.onDragging);
- document.addEventListener('touchmove', _this.onDragging);
- document.addEventListener('mouseleave', _this.onDragEnd);
- document.addEventListener('mouseup', _this.onDragEnd);
- document.addEventListener('mouseup', _this.onDragEnd);
- document.addEventListener('touchend', _this.onDragEnd);
- }; };
- _this.onDragging = function (e) {
- var target = _this.target;
- _this.updateMouse();
- var _a = tslib_1.__read(_this.selection, 2), start = _a[0], end = _a[1];
- var currValue = _this.getTickValue(_this.getValueByCanvasPoint(e));
- var diffValue = currValue - _this.prevValue;
- if (target === 'start')
- start !== currValue && _this.updateSelection(currValue, end);
- else if (target === 'end')
- end !== currValue && _this.updateSelection(start, currValue);
- else if (target === 'ribbon' && diffValue !== 0) {
- _this.prevValue = currValue;
- _this.updateSelection(diffValue, diffValue, true);
- }
- };
- _this.onDragEnd = function () {
- _this.style.cursor = 'default';
- document.removeEventListener('mousemove', _this.onDragging);
- document.removeEventListener('touchmove', _this.onDragging);
- document.removeEventListener('mouseup', _this.onDragEnd);
- document.removeEventListener('touchend', _this.onDragEnd);
- };
- return _this;
- }
- Object.defineProperty(Continuous.prototype, "handleOffsetRatio", {
- get: function () {
- return this.ifHorizontal(0.5, 0.5);
- },
- enumerable: false,
- configurable: true
- });
- Continuous.prototype.getBBox = function () {
- var _a = this.attributes, width = _a.width, height = _a.height;
- return new util_2.BBox(0, 0, width, height);
- };
- Continuous.prototype.render = function (attributes, container) {
- var _this = this;
- // 渲染顺序
- // 1. 绘制 title, 获得可用空间
- // 2. 绘制 label, handle
- // 3. 基于可用空间、label高度、handle 宽高,计算 ribbon 宽高
- // 4. 绘制 ribbon
- // 5. 调整 label、handle 位置
- var showLabel = attributes.showLabel;
- /** title */
- this.renderTitle((0, util_2.select)(container));
- var _a = this.availableSpace, x = _a.x, y = _a.y;
- /** label */
- /** content */
- var contentGroup = (0, util_2.select)(container).maybeAppendByClassName(constant_2.CLASS_NAMES.contentGroup, 'g').styles({ x: x, y: y });
- var labelGroup = contentGroup.maybeAppendByClassName(constant_2.CLASS_NAMES.labelGroup, 'g').styles({ zIndex: 1 });
- (0, util_2.ifShow)(!!showLabel, labelGroup, function (group) {
- _this.renderLabel(group);
- });
- var ribbonGroup = contentGroup.maybeAppendByClassName(constant_2.CLASS_NAMES.ribbonGroup, 'g').styles({ zIndex: 0 });
- /** handle */
- this.handlesGroup = contentGroup.maybeAppendByClassName(constant_2.CLASS_NAMES.handlesGroup, 'g').styles({ zIndex: 2 });
- this.renderHandles();
- /** ribbon */
- this.renderRibbon(ribbonGroup);
- this.renderIndicator(contentGroup);
- /** adjust */
- this.adjustLabel();
- this.adjustHandles();
- };
- Object.defineProperty(Continuous.prototype, "range", {
- get: function () {
- var data = this.attributes.data;
- return getMinMax(data);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "ribbonScale", {
- get: function () {
- var _a = this.range, min = _a.min, max = _a.max;
- this.innerRibbonScale.update({
- domain: [min, max],
- range: [0, 1],
- });
- return this.innerRibbonScale;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "ribbonRange", {
- get: function () {
- var _a = tslib_1.__read(this.selection, 2), min = _a[0], max = _a[1];
- var scale = this.ribbonScale;
- return [scale.map(min), scale.map(max)];
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "selection", {
- get: function () {
- var _a = this.range, min = _a.min, max = _a.max;
- var _b = this.attributes.defaultValue, _c = _b === void 0 ? [min, max] : _b, _d = tslib_1.__read(_c, 2), start = _d[0], end = _d[1];
- return [start, end];
- },
- enumerable: false,
- configurable: true
- });
- Continuous.prototype.ifHorizontal = function (a, b) {
- return (0, utils_2.ifHorizontal)(this.attributes.orientation, typeof a === 'function' ? a() : a, typeof b === 'function' ? b() : b);
- };
- Continuous.prototype.renderTitle = function (container) {
- var _a = this.attributes, showTitle = _a.showTitle, titleText = _a.titleText, width = _a.width, height = _a.height;
- var style = (0, util_2.subStyleProps)(this.attributes, 'title');
- var finalTitleStyle = tslib_1.__assign(tslib_1.__assign({}, style), { width: width, height: height, text: titleText });
- var that = this;
- container
- .selectAll(constant_2.CLASS_NAMES.title.class)
- .data(showTitle ? [titleText] : [])
- .join(function (enter) {
- return enter
- .append(function () { return new title_1.Title({ style: finalTitleStyle }); })
- .attr('className', constant_2.CLASS_NAMES.title.name)
- .each(function () {
- that.title = this;
- });
- }, function (update) { return update.update(finalTitleStyle); }, function (exit) {
- return exit
- .each(function () {
- that.title = undefined;
- })
- .remove();
- });
- };
- Object.defineProperty(Continuous.prototype, "availableSpace", {
- get: function () {
- if (this.title)
- return this.title.getAvailableSpace();
- var _a = this.attributes, width = _a.width, height = _a.height;
- return new util_2.BBox(0, 0, width, height);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "labelFixedSpacing", {
- get: function () {
- var showTick = this.attributes.showTick;
- return showTick ? 5 : 0;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "labelPosition", {
- get: function () {
- var _a = this.attributes, orientation = _a.orientation, labelDirection = _a.labelDirection;
- var positions = {
- vertical: { positive: 'right', negative: 'left' },
- horizontal: { positive: 'bottom', negative: 'top' },
- };
- return positions[orientation][labelDirection];
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "labelBBox", {
- get: function () {
- var _a;
- var showLabel = this.attributes.showLabel;
- if (!showLabel)
- return new util_2.BBox(0, 0, 0, 0);
- if (this.cacheLabelBBox)
- return this.cacheLabelBBox;
- 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;
- this.cacheLabelBBox = new util_2.BBox(0, 0, width, height);
- return this.cacheLabelBBox;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "labelShape", {
- get: function () {
- var _a = this.attributes, showLabel = _a.showLabel, _b = _a.labelSpacing, labelSpacing = _b === void 0 ? 0 : _b;
- if (!showLabel)
- return { width: 0, height: 0, size: 0, length: 0 };
- var _c = this.labelBBox, width = _c.width, height = _c.height;
- var size = this.ifHorizontal(height, width) + labelSpacing + this.labelFixedSpacing;
- var length = this.ifHorizontal(width, height);
- return { width: width, height: height, size: size, length: length };
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "ribbonBBox", {
- get: function () {
- var _a = this.attributes, showHandle = _a.showHandle, userDefinedRibbonSize = _a.ribbonSize;
- var _b = this.availableSpace, availableWidth = _b.width, availableHeight = _b.height;
- var _c = this.labelShape, labelSize = _c.size, labelLength = _c.length;
- var _d = tslib_1.__read(this.ifHorizontal([availableHeight, availableWidth], [availableWidth, availableHeight]), 2), availableSize = _d[0], availableLength = _d[1];
- var _e = showHandle ? this.handleShape : { size: 0, length: 0 }, handleSize = _e.size, handleLength = _e.length;
- var handleRatio = this.handleOffsetRatio;
- var ribbonSize = 0;
- var labelPosition = this.labelPosition;
- if (userDefinedRibbonSize) {
- ribbonSize = userDefinedRibbonSize;
- }
- else if (['bottom', 'right'].includes(labelPosition)) {
- ribbonSize = Math.min(availableSize - labelSize, (availableSize - handleSize) / handleRatio);
- }
- else if (availableSize * (1 - handleRatio) > handleSize) {
- ribbonSize = Math.max(availableSize - labelSize, 0);
- }
- else
- ribbonSize = Math.max((availableSize - labelSize - handleSize) / handleRatio, 0);
- var edgeLength = Math.max(handleLength, labelLength);
- var ribbonLength = availableLength - edgeLength;
- var _f = tslib_1.__read(this.ifHorizontal([ribbonLength, ribbonSize], [ribbonSize, ribbonLength]), 2), width = _f[0], height = _f[1];
- // 需要考虑 handle 的占用空间
- // todo 为了防止因为 handle 文本变化导致的 ribbon 位置变化,handle size 取最大值
- var finalLabelOccupy = ['top', 'left'].includes(labelPosition) ? labelSize : 0;
- var _g = tslib_1.__read(this.ifHorizontal([edgeLength / 2, finalLabelOccupy], [finalLabelOccupy, edgeLength / 2]), 2), x = _g[0], y = _g[1];
- return new util_2.BBox(x, y, width, height);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "ribbonShape", {
- get: function () {
- var _a = this.ribbonBBox, width = _a.width, height = _a.height;
- return this.ifHorizontal({ size: height, length: width }, { size: width, length: height });
- },
- enumerable: false,
- configurable: true
- });
- Continuous.prototype.renderRibbon = function (container) {
- var _a = this.attributes, data = _a.data, type = _a.type, orientation = _a.orientation, color = _a.color, block = _a.block;
- var ribbonStyle = (0, util_2.subStyleProps)(this.attributes, 'ribbon');
- var _b = this.range, min = _b.min, max = _b.max;
- var _c = this.ribbonBBox, x = _c.x, y = _c.y;
- var _d = this.ribbonShape, length = _d.length, size = _d.size;
- var style = (0, util_2.deepAssign)({
- x: x,
- y: y,
- length: length,
- size: size,
- type: type,
- orientation: orientation,
- color: color,
- block: block,
- partition: data.map(function (d) { return (d.value - min) / (max - min); }),
- range: this.ribbonRange,
- }, ribbonStyle);
- this.ribbon = container.maybeAppendByClassName(constant_2.CLASS_NAMES.ribbon, function () { return new ribbon_1.Ribbon({ style: style }); }).update(style);
- };
- Continuous.prototype.getHandleClassName = function (type) {
- // @ts-ignore
- return "".concat(constant_2.CLASS_NAMES.prefix("".concat(type, "-handle")));
- };
- Continuous.prototype.renderHandles = function () {
- var _a = this.attributes, showHandle = _a.showHandle, orientation = _a.orientation;
- var handleStyle = (0, util_2.subStyleProps)(this.attributes, 'handle');
- var _b = tslib_1.__read(this.selection, 2), min = _b[0], max = _b[1];
- var style = tslib_1.__assign(tslib_1.__assign({}, handleStyle), { orientation: orientation });
- var _c = handleStyle.shape, shape = _c === void 0 ? 'slider' : _c;
- var HandleCtor = shape === 'basic' ? handle_1.Handle : handle_2.Handle;
- var that = this;
- this.handlesGroup
- .selectAll(constant_2.CLASS_NAMES.handle.class)
- .data(showHandle
- ? [
- { value: min, type: 'start' },
- { value: max, type: 'end' },
- ]
- : [], function (d) { return d.type; })
- .join(function (enter) {
- return enter
- .append(function () { return new HandleCtor({ style: style }); })
- .attr('className', function (_a) {
- var type = _a.type;
- return "".concat(constant_2.CLASS_NAMES.handle, " ").concat(that.getHandleClassName(type));
- })
- .each(function (_a) {
- var type = _a.type, labelText = _a.value;
- this.update({ labelText: labelText });
- var name = "".concat(type, "Handle");
- that[name] = this;
- this.addEventListener('pointerdown', that.onDragStart(type));
- });
- }, function (update) {
- return update.update(style).each(function (_a) {
- var labelText = _a.value;
- this.update({ labelText: labelText });
- });
- }, function (exit) {
- return exit
- .each(function (_a) {
- var type = _a.type;
- var name = "".concat(type, "Handle");
- that[name] = undefined;
- })
- .remove();
- });
- };
- Continuous.prototype.adjustHandles = function () {
- var _a = tslib_1.__read(this.selection, 2), min = _a[0], max = _a[1];
- this.setHandlePosition('start', min);
- this.setHandlePosition('end', max);
- };
- Object.defineProperty(Continuous.prototype, "handleBBox", {
- get: function () {
- if (this.cacheHandleBBox)
- return this.cacheHandleBBox;
- if (!this.attributes.showHandle)
- return new util_2.BBox(0, 0, 0, 0);
- var _a = this.startHandle.getBBox(), startHandleWidth = _a.width, startHandleHeight = _a.height;
- var _b = this.endHandle.getBBox(), endHandleWidth = _b.width, endHandleHeight = _b.height;
- var _c = tslib_1.__read([Math.max(startHandleWidth, endHandleWidth), Math.max(startHandleHeight, endHandleHeight)], 2), width = _c[0], height = _c[1];
- this.cacheHandleBBox = new util_2.BBox(0, 0, width, height);
- return this.cacheHandleBBox;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "handleShape", {
- /**
- * 因为 handle label 的宽高是动态的,所以 handle bbox 是第一次渲染时的 bbox
- */
- get: function () {
- var _a = this.handleBBox, width = _a.width, height = _a.height;
- var _b = tslib_1.__read(this.ifHorizontal([height, width], [width, height]), 2), size = _b[0], length = _b[1];
- return { width: width, height: height, size: size, length: length };
- },
- enumerable: false,
- configurable: true
- });
- Continuous.prototype.setHandlePosition = function (type, value) {
- var handleFormatter = this.attributes.handleFormatter;
- var _a = this.ribbonBBox, ribbonX = _a.x, ribbonY = _a.y;
- var ribbonSize = this.ribbonShape.size;
- var offset = this.getOffset(value);
- 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];
- var handle = this.handlesGroup.select(".".concat(this.getHandleClassName(type))).node();
- handle === null || handle === void 0 ? void 0 : handle.update({ x: x, y: y, formatter: handleFormatter });
- };
- Continuous.prototype.renderIndicator = function (container) {
- var style = (0, util_2.subStyleProps)(this.attributes, 'indicator');
- this.indicator = container.maybeAppendByClassName(constant_2.CLASS_NAMES.indicator, function () { return new indicator_1.Indicator({}); }).update(style);
- // this.hideIndicator();
- };
- Object.defineProperty(Continuous.prototype, "labelData", {
- get: function () {
- var _this = this;
- var data = this.attributes.data;
- return data.reduce(function (acc, curr, index, arr) {
- var _a, _b;
- var id = (_a = curr === null || curr === void 0 ? void 0 : curr.id) !== null && _a !== void 0 ? _a : index.toString();
- 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) }));
- if (index < arr.length - 1) {
- var next = arr[index + 1];
- var _c = tslib_1.__read([curr.value, next.value], 2), cr = _c[0], nx = _c[1];
- var midVal = (cr + nx) / 2;
- 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) }));
- }
- return acc;
- }, []);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Continuous.prototype, "labelStyle", {
- get: function () {
- var _a = tslib_1.__read(['center', 'middle'], 2), labelTextAlign = _a[0], labelTextBaseline = _a[1];
- var labelPosition = this.labelPosition;
- if (labelPosition === 'top')
- labelTextBaseline = 'bottom';
- else if (labelPosition === 'bottom')
- labelTextBaseline = 'top';
- else if (labelPosition === 'left')
- labelTextAlign = 'end';
- else if (labelPosition === 'right')
- labelTextAlign = 'start';
- return {
- labelTextAlign: labelTextAlign,
- labelTextBaseline: labelTextBaseline,
- };
- },
- enumerable: false,
- configurable: true
- });
- Continuous.prototype.renderLabel = function (container) {
- var _a = this.attributes, _b = _a.showTick, showTick = _b === void 0 ? false : _b, labelFilter = _a.labelFilter, labelFormatter = _a.labelFormatter;
- var tickStyle = (0, util_2.subStyleProps)(this.attributes, 'tick');
- var labelStyle = (0, util_2.subStyleProps)(this.attributes, 'label');
- var align = labelStyle.align;
- 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 });
- var functionStyle = {
- tickFilter: function (datum, index, data) {
- if ((datum === null || datum === void 0 ? void 0 : datum.type) !== 'value')
- return false;
- if (labelFilter)
- return labelFilter(datum, datum.index, data.filter(function (d) { return d.type !== 'value'; }));
- return true;
- },
- labelFilter: function (datum, index, data) {
- if ((datum === null || datum === void 0 ? void 0 : datum.type) !== align)
- return false;
- if (labelFilter)
- return labelFilter(datum, datum.index, data.filter(function (d) { return d.type === align; }));
- return true;
- },
- labelFormatter: labelFormatter,
- };
- var finalLabelStyle = tslib_1.__assign(tslib_1.__assign({}, style), functionStyle);
- this.label = container.maybeAppendByClassName(constant_2.CLASS_NAMES.label, function () { return new axis_1.Axis({ style: finalLabelStyle }); }).node();
- this.label.update(finalLabelStyle, false);
- };
- Object.defineProperty(Continuous.prototype, "labelAxisStyle", {
- get: function () {
- // @ts-ignore
- var _a = this.attributes, showTick = _a.showTick, labelDirection = _a.labelDirection, labelSpacing = _a.labelSpacing, definedTickLength = _a.tickLength;
- var ribbonSize = this.ribbonShape.size;
- var labelPosition = this.labelPosition;
- var labelFixedSpacing = this.labelFixedSpacing;
- var _b = tslib_1.__read([0, 0, 0], 3), offset = _b[0], spacing = _b[1], tickLength = _b[2];
- var internalVal = definedTickLength !== null && definedTickLength !== void 0 ? definedTickLength : ribbonSize;
- if (showTick) {
- tickLength = internalVal;
- spacing = labelFixedSpacing;
- if (labelDirection === 'positive') {
- if (labelPosition === 'right') {
- offset = internalVal;
- tickLength = internalVal;
- }
- else if (labelPosition === 'bottom')
- offset = tickLength;
- }
- else if (labelDirection === 'negative') {
- if (labelPosition === 'top')
- offset = ribbonSize;
- else if (labelPosition === 'left')
- offset = ribbonSize;
- }
- }
- else if (labelDirection === 'positive') {
- if (labelPosition === 'right')
- spacing = internalVal;
- else if (labelPosition === 'bottom') {
- offset = ribbonSize + labelFixedSpacing;
- spacing = labelSpacing;
- }
- }
- else if (labelDirection === 'negative') {
- if (labelPosition === 'left')
- spacing = labelSpacing;
- else if (labelPosition === 'top')
- spacing = labelSpacing;
- }
- return { offset: offset, spacing: labelSpacing, tickLength: tickLength };
- },
- enumerable: false,
- configurable: true
- });
- Continuous.prototype.adjustLabel = function () {
- var showLabel = this.attributes.showLabel;
- if (!showLabel)
- return;
- var _a = this.ribbonBBox, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
- var _b = this.labelAxisStyle, axisOffset = _b.offset, axisSpacing = _b.spacing, axisTickLength = _b.tickLength;
- var _c = tslib_1.__read(this.ifHorizontal([
- [x, y + axisOffset],
- [x + width, y + axisOffset],
- ], [
- [x + axisOffset, y + height],
- [x + axisOffset, y],
- ]), 2), startPos = _c[0], endPos = _c[1];
- this.label.update({
- startPos: startPos,
- endPos: endPos,
- tickLength: axisTickLength,
- labelSpacing: axisSpacing,
- }, false);
- };
- Continuous.prototype.bindEvents = function () {
- // 绑定 drag 开始事件
- this.ribbon.on('pointerdown', this.onDragStart('ribbon'));
- this.ribbon.on('pointermove', this.onHovering);
- this.addEventListener('pointerout', this.hideIndicator);
- };
- Continuous.prototype.showIndicator = function (value, text) {
- if (text === void 0) { text = "".concat(value); }
- var showIndicator = this.attributes.showIndicator;
- if (!showIndicator || typeof value !== 'number') {
- this.hideIndicator();
- return;
- }
- var _a = this.range, min = _a.min, max = _a.max;
- var _b = this.ribbonBBox, x = _b.x, y = _b.y;
- var safeValue = (0, util_1.clamp)(value, min, max);
- var offset = this.getOffset(safeValue);
- var pos = this.ifHorizontal([offset + x, y], [x, offset + y]);
- this.indicator.update({
- x: pos[0],
- y: pos[1],
- position: this.ifHorizontal('top', 'left'),
- labelText: text,
- });
- (0, util_2.show)(this.indicator.node());
- };
- Continuous.prototype.hideIndicator = function () {
- (0, util_2.hide)(this.indicator.node());
- };
- Continuous.prototype.updateMouse = function () {
- if (this.attributes.slidable)
- this.style.cursor = 'grabbing';
- };
- Continuous.prototype.setSelection = function (start, end) {
- this.updateSelection(start, end);
- };
- Continuous.prototype.updateSelection = function (stVal, endVal, isOffset) {
- var _a;
- if (isOffset === void 0) { isOffset = false; }
- var _b = tslib_1.__read(this.selection, 2), currSt = _b[0], currEnd = _b[1];
- var _c = tslib_1.__read([stVal, endVal], 2), start = _c[0], end = _c[1];
- if (isOffset) {
- // 获取当前值
- start += currSt;
- end += currEnd;
- }
- // 值校验
- var _d = this.range, min = _d.min, max = _d.max;
- _a = tslib_1.__read((0, utils_2.getSafetySelections)([min, max], [start, end], this.selection), 2), start = _a[0], end = _a[1];
- this.update({ defaultValue: [start, end] });
- this.dispatchSelection();
- };
- Object.defineProperty(Continuous.prototype, "step", {
- get: function () {
- var _a = this.attributes.step, step = _a === void 0 ? 1 : _a;
- var _b = this.range, min = _b.min, max = _b.max;
- if ((0, util_1.isUndefined)(step))
- return (0, util_2.toPrecision)((max - min) * constant_2.STEP_RATIO, 0);
- return step;
- },
- enumerable: false,
- configurable: true
- });
- Continuous.prototype.getTickValue = function (value) {
- var _a = this.attributes, data = _a.data, block = _a.block;
- var min = this.range.min;
- if (block)
- return (0, utils_1.getNextTickValue)(data.map(function (_a) {
- var value = _a.value;
- return value;
- }), value).tick;
- return (0, utils_2.getStepValueByValue)(value, this.step, min);
- };
- /**
- * 事件触发的位置对应的value值
- * @param limit {boolean} 我也忘了要干啥了
- */
- Continuous.prototype.getValueByCanvasPoint = function (e, limit) {
- if (limit === void 0) { limit = false; }
- var _a = this.range, min = _a.min, max = _a.max;
- var _b = tslib_1.__read(this.ribbon.node().getPosition(), 2), x = _b[0], y = _b[1];
- var startPos = this.ifHorizontal(x, y);
- var currValue = this.ifHorizontal.apply(this, tslib_1.__spreadArray([], tslib_1.__read((0, util_2.getEventPos)(e)), false));
- var offset = currValue - startPos;
- var value = (0, util_1.clamp)(this.getOffset(offset, true), min, max);
- return value;
- };
- /** reverse: 屏幕偏移量 -> 值 */
- Continuous.prototype.getOffset = function (value, reverse) {
- if (reverse === void 0) { reverse = false; }
- var _a = this.range, min = _a.min, max = _a.max;
- var ribbonLen = this.ribbonShape.length;
- var scale = this.eventToOffsetScale;
- scale.update({ domain: [min, max], range: [0, ribbonLen] });
- if (reverse)
- return scale.invert(value);
- return scale.map(value);
- };
- Continuous.prototype.dispatchSelection = function () {
- var evt = new g_1.CustomEvent('valuechange', {
- detail: {
- value: this.selection,
- },
- });
- this.dispatchEvent(evt);
- };
- Continuous.prototype.dispatchIndicated = function (value, range) {
- var evt = new g_1.CustomEvent('indicate', {
- detail: { value: value, range: range },
- });
- this.dispatchEvent(evt);
- };
- return Continuous;
- }(core_1.GUI));
- exports.Continuous = Continuous;
- //# sourceMappingURL=continuous.js.map
|