| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- /**
- * DevExtreme (viz/gauges/base_indicators.js)
- * Version: 19.1.16
- * Build date: Tue Oct 18 2022
- *
- * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
- * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
- */
- "use strict";
- var noop = require("../../core/utils/common").noop;
- var each = require("../../core/utils/iterator").each;
- var _isFinite = isFinite;
- var _Number = Number;
- var _round = Math.round;
- var baseGaugeModule = require("./base_gauge");
- var _formatValue = baseGaugeModule.formatValue;
- var _getSampleText = baseGaugeModule.getSampleText;
- var _patchFontOptions = require("../core/utils").patchFontOptions;
- var extend = require("../../core/utils/extend").extend;
- var Class = require("../../core/class");
- var BaseElement = Class.inherit({
- ctor: function(parameters) {
- var that = this;
- each(parameters, function(name, value) {
- that["_" + name] = value
- });
- that._init()
- },
- dispose: function() {
- var that = this;
- that._dispose();
- each(that, function(name) {
- that[name] = null
- });
- return that
- },
- getOffset: function() {
- return _Number(this._options.offset) || 0
- }
- });
- var BaseIndicator = BaseElement.inherit({
- _init: function() {
- var that = this;
- that._rootElement = that._createRoot().linkOn(that._owner, {
- name: "value-indicator",
- after: "core"
- });
- that._trackerElement = that._createTracker()
- },
- _dispose: function() {
- this._rootElement.linkOff()
- },
- _setupAnimation: function() {
- var that = this;
- if (that._options.animation) {
- that._animation = {
- step: function(pos) {
- that._actualValue = that._animation.start + that._animation.delta * pos;
- that._actualPosition = that._translator.translate(that._actualValue);
- that._move()
- },
- duration: that._options.animation.duration > 0 ? _Number(that._options.animation.duration) : 0,
- easing: that._options.animation.easing
- }
- }
- },
- _runAnimation: function(value) {
- var that = this;
- var animation = that._animation;
- animation.start = that._actualValue;
- animation.delta = value - that._actualValue;
- that._rootElement.animate({
- _: 0
- }, {
- step: animation.step,
- duration: animation.duration,
- easing: animation.easing
- })
- },
- _createRoot: function() {
- return this._renderer.g().attr({
- "class": this._className
- })
- },
- _createTracker: function() {
- return this._renderer.path([], "area")
- },
- _getTrackerSettings: noop,
- clean: function() {
- var that = this;
- that._animation && that._rootElement.stopAnimation();
- that._rootElement.linkRemove().clear();
- that._clear();
- that._tracker.detach(that._trackerElement);
- that._options = that.enabled = that._animation = null;
- return that
- },
- render: function(options) {
- var that = this;
- that.type = options.type;
- that._options = options;
- that._actualValue = that._currentValue = that._translator.adjust(that._options.currentValue);
- that.enabled = that._isEnabled();
- if (that.enabled) {
- that._setupAnimation();
- that._rootElement.attr({
- fill: that._options.color
- }).linkAppend();
- that._tracker.attach(that._trackerElement, that, that._trackerInfo)
- }
- return that
- },
- resize: function(layout) {
- var that = this;
- that._rootElement.clear();
- that._clear();
- that.visible = that._isVisible(layout);
- if (that.visible) {
- extend(that._options, layout);
- that._actualPosition = that._translator.translate(that._actualValue);
- that._render();
- that._trackerElement.attr(that._getTrackerSettings());
- that._move()
- }
- return that
- },
- value: function(arg, _noAnimation) {
- var that = this;
- var val;
- var rootElement = this._rootElement;
- var visibility = null;
- if (void 0 === arg) {
- return that._currentValue
- }
- if (null === arg) {
- visibility = "hidden";
- that._currentValue = arg
- } else {
- val = that._translator.adjust(arg);
- if (that._currentValue !== val && _isFinite(val)) {
- that._currentValue = val;
- if (that.visible) {
- if (that._animation && !_noAnimation) {
- that._runAnimation(val)
- } else {
- that._actualValue = val;
- that._actualPosition = that._translator.translate(val);
- that._move()
- }
- }
- }
- }
- rootElement.attr({
- visibility: visibility
- });
- return that
- },
- _isEnabled: null,
- _isVisible: null,
- _render: null,
- _clear: null,
- _move: null
- });
- var COEFFICIENTS_MAP = {};
- COEFFICIENTS_MAP["right-bottom"] = COEFFICIENTS_MAP.rb = [0, -1, -1, 0, 0, 1, 1, 0];
- COEFFICIENTS_MAP["bottom-right"] = COEFFICIENTS_MAP.br = [-1, 0, 0, -1, 1, 0, 0, 1];
- COEFFICIENTS_MAP["left-bottom"] = COEFFICIENTS_MAP.lb = [0, -1, 1, 0, 0, 1, -1, 0];
- COEFFICIENTS_MAP["bottom-left"] = COEFFICIENTS_MAP.bl = [1, 0, 0, -1, -1, 0, 0, 1];
- COEFFICIENTS_MAP["left-top"] = COEFFICIENTS_MAP.lt = [0, 1, 1, 0, 0, -1, -1, 0];
- COEFFICIENTS_MAP["top-left"] = COEFFICIENTS_MAP.tl = [1, 0, 0, 1, -1, 0, 0, -1];
- COEFFICIENTS_MAP["right-top"] = COEFFICIENTS_MAP.rt = [0, 1, -1, 0, 0, -1, 1, 0];
- COEFFICIENTS_MAP["top-right"] = COEFFICIENTS_MAP.tr = [-1, 0, 0, 1, 1, 0, 0, -1];
- function getTextCloudInfo(options) {
- var x = options.x;
- var y = options.y;
- var type = COEFFICIENTS_MAP[options.type];
- var cloudWidth = options.textWidth + 2 * options.horMargin;
- var cloudHeight = options.textHeight + 2 * options.verMargin;
- var tailWidth;
- var tailHeight;
- var cx = x;
- var cy = y;
- tailWidth = tailHeight = options.tailLength;
- if (1 & type[0]) {
- tailHeight = Math.min(tailHeight, cloudHeight / 3)
- } else {
- tailWidth = Math.min(tailWidth, cloudWidth / 3)
- }
- return {
- cx: _round(cx + type[0] * tailWidth + (type[0] + type[2]) * cloudWidth / 2),
- cy: _round(cy + type[1] * tailHeight + (type[1] + type[3]) * cloudHeight / 2),
- points: [_round(x), _round(y), _round(x += type[0] * (cloudWidth + tailWidth)), _round(y += type[1] * (cloudHeight + tailHeight)), _round(x += type[2] * cloudWidth), _round(y += type[3] * cloudHeight), _round(x += type[4] * cloudWidth), _round(y += type[5] * cloudHeight), _round(x += type[6] * (cloudWidth - tailWidth)), _round(y += type[7] * (cloudHeight - tailHeight))]
- }
- }
- var BaseTextCloudMarker = BaseIndicator.inherit({
- _move: function() {
- var that = this;
- var textCloudOptions = that._getTextCloudOptions();
- var text = _formatValue(that._actualValue, that._options.text);
- that._text.attr({
- text: text
- });
- var bBox = that._text.getBBox();
- var info = getTextCloudInfo({
- x: textCloudOptions.x,
- y: textCloudOptions.y,
- textWidth: bBox.width || text.length * that._textUnitWidth,
- textHeight: bBox.height || that._textHeight,
- horMargin: that._options.horizontalOffset,
- verMargin: that._options.verticalOffset,
- tailLength: that._options.arrowLength,
- type: textCloudOptions.type
- });
- that._text.attr({
- x: info.cx,
- y: info.cy + that._textVerticalOffset
- });
- that._cloud.attr({
- points: info.points
- });
- that._trackerElement && that._trackerElement.attr({
- points: info.points
- })
- },
- _measureText: function() {
- var that = this;
- var root;
- var text;
- var bBox;
- var sampleText;
- if (!that._textVerticalOffset) {
- root = that._createRoot().append(that._owner);
- sampleText = _getSampleText(that._translator, that._options.text);
- text = that._renderer.text(sampleText, 0, 0).attr({
- align: "center"
- }).css(_patchFontOptions(that._options.text.font)).append(root);
- bBox = text.getBBox();
- root.remove();
- that._textVerticalOffset = -bBox.y - bBox.height / 2;
- that._textWidth = bBox.width;
- that._textHeight = bBox.height;
- that._textUnitWidth = that._textWidth / sampleText.length;
- that._textFullWidth = that._textWidth + 2 * that._options.horizontalOffset;
- that._textFullHeight = that._textHeight + 2 * that._options.verticalOffset
- }
- },
- _render: function() {
- var that = this;
- that._measureText();
- that._cloud = that._cloud || that._renderer.path([], "area").append(that._rootElement);
- that._text = that._text || that._renderer.text().append(that._rootElement);
- that._text.attr({
- align: "center"
- }).css(_patchFontOptions(that._options.text.font))
- },
- _clear: function() {
- delete this._cloud;
- delete this._text
- },
- getTooltipParameters: function() {
- var position = this._getTextCloudOptions();
- return {
- x: position.x,
- y: position.y,
- value: this._currentValue,
- color: this._options.color
- }
- }
- });
- var BaseRangeBar = BaseIndicator.inherit({
- _measureText: function() {
- var that = this;
- var root;
- var text;
- var bBox;
- that._hasText = that._isTextVisible();
- if (that._hasText && !that._textVerticalOffset) {
- root = that._createRoot().append(that._owner);
- text = that._renderer.text(_getSampleText(that._translator, that._options.text), 0, 0).attr({
- "class": "dxg-text",
- align: "center"
- }).css(_patchFontOptions(that._options.text.font)).append(root);
- bBox = text.getBBox();
- root.remove();
- that._textVerticalOffset = -bBox.y - bBox.height / 2;
- that._textWidth = bBox.width;
- that._textHeight = bBox.height
- }
- },
- _move: function() {
- var that = this;
- that._updateBarItemsPositions();
- if (that._hasText) {
- that._text.attr({
- text: _formatValue(that._actualValue, that._options.text)
- });
- that._updateTextPosition();
- that._updateLinePosition()
- }
- },
- _updateBarItems: function() {
- var that = this;
- var options = that._options;
- var spaceColor;
- var translator = that._translator;
- that._setBarSides();
- that._startPosition = translator.translate(translator.getDomainStart());
- that._endPosition = translator.translate(translator.getDomainEnd());
- that._basePosition = translator.translate(options.baseValue);
- that._space = that._getSpace();
- var backgroundColor = options.backgroundColor || "none";
- if ("none" !== backgroundColor && that._space > 0) {
- spaceColor = options.containerBackgroundColor || "none"
- } else {
- that._space = 0;
- spaceColor = "none"
- }
- that._backItem1.attr({
- fill: backgroundColor
- });
- that._backItem2.attr({
- fill: backgroundColor
- });
- that._spaceItem1.attr({
- fill: spaceColor
- });
- that._spaceItem2.attr({
- fill: spaceColor
- })
- },
- _getSpace: function() {
- return 0
- },
- _updateTextItems: function() {
- var that = this;
- if (that._hasText) {
- that._line = that._line || that._renderer.path([], "line").attr({
- "class": "dxg-main-bar",
- "stroke-linecap": "square"
- }).append(that._rootElement);
- that._text = that._text || that._renderer.text("", 0, 0).attr({
- "class": "dxg-text"
- }).append(that._rootElement);
- that._text.attr({
- align: that._getTextAlign()
- }).css(that._getFontOptions());
- that._setTextItemsSides()
- } else {
- if (that._line) {
- that._line.remove();
- delete that._line
- }
- if (that._text) {
- that._text.remove();
- delete that._text
- }
- }
- },
- _isTextVisible: function() {
- return false
- },
- _getTextAlign: function() {
- return "center"
- },
- _getFontOptions: function() {
- var options = this._options;
- var font = options.text.font;
- if (!font || !font.color) {
- font = extend({}, font, {
- color: options.color
- })
- }
- return _patchFontOptions(font)
- },
- _updateBarItemsPositions: function() {
- var that = this;
- var positions = that._getPositions();
- that._backItem1.attr(that._buildItemSettings(positions.start, positions.back1));
- that._backItem2.attr(that._buildItemSettings(positions.back2, positions.end));
- that._spaceItem1.attr(that._buildItemSettings(positions.back1, positions.main1));
- that._spaceItem2.attr(that._buildItemSettings(positions.main2, positions.back2));
- that._mainItem.attr(that._buildItemSettings(positions.main1, positions.main2));
- that._trackerElement && that._trackerElement.attr(that._buildItemSettings(positions.main1, positions.main2))
- },
- _render: function() {
- var that = this;
- that._measureText();
- if (!that._backItem1) {
- that._backItem1 = that._createBarItem();
- that._backItem1.attr({
- "class": "dxg-back-bar"
- })
- }
- if (!that._backItem2) {
- that._backItem2 = that._createBarItem();
- that._backItem2.attr({
- "class": "dxg-back-bar"
- })
- }
- if (!that._spaceItem1) {
- that._spaceItem1 = that._createBarItem();
- that._spaceItem1.attr({
- "class": "dxg-space-bar"
- })
- }
- if (!that._spaceItem2) {
- that._spaceItem2 = that._createBarItem();
- that._spaceItem2.attr({
- "class": "dxg-space-bar"
- })
- }
- if (!that._mainItem) {
- that._mainItem = that._createBarItem();
- that._mainItem.attr({
- "class": "dxg-main-bar"
- })
- }
- that._updateBarItems();
- that._updateTextItems()
- },
- _clear: function() {
- var that = this;
- delete that._backItem1;
- delete that._backItem2;
- delete that._spaceItem1;
- delete that._spaceItem2;
- delete that._mainItem;
- delete that._hasText;
- delete that._line;
- delete that._text
- },
- getTooltipParameters: function() {
- var position = this._getTooltipPosition();
- return {
- x: position.x,
- y: position.y,
- value: this._currentValue,
- color: this._options.color,
- offset: 0
- }
- }
- });
- exports.BaseElement = BaseElement;
- exports.BaseIndicator = BaseIndicator;
- exports.BaseTextCloudMarker = BaseTextCloudMarker;
- exports.BaseRangeBar = BaseRangeBar;
|