devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
326 lines (318 loc) • 9.49 kB
JavaScript
/**
* DevExtreme (esm/__internal/viz/sparklines/base_sparkline.js)
* Version: 26.1.6
* Build date: Fri Oct 16 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import {
domAdapter
} from "../../core/dom_adapter";
import {
renderer as $
} from "../../core/renderer";
import {
noop
} from "../../core/utils/m_common";
import {
extend
} from "../../core/utils/m_extend";
import {
isFunction
} from "../../core/utils/m_type";
import eventsEngine from "../../events/core/events_engine";
import pointerEvents from "../../events/pointer";
import {
addNamespace
} from "../../events/utils/index";
import BaseWidget from "../../viz/core/base_widget";
import {
plugin
} from "../../viz/core/export";
import {
setupWidgetPrototype
} from "../../viz/core/helpers";
import {
plugin as tooltipPlugin
} from "../../viz/core/tooltip";
import {
pointInCanvas
} from "../../viz/core/utils";
import {
Translator2D
} from "../../viz/translators/translator2d";
const DEFAULT_LINE_SPACING = 2;
const TOOLTIP_TABLE_BORDER_SPACING = 0;
const TOOLTIP_TABLE_KEY_VALUE_SPACE = 15;
const EVENT_NS = "sparkline-tooltip";
const POINTER_ACTION = addNamespace([pointerEvents.down, pointerEvents.move], EVENT_NS);
const {
_initTooltip: initTooltip
} = tooltipPlugin.members;
function inCanvas(_ref, x, y) {
let {
width: width,
height: height
} = _ref;
return pointInCanvas({
left: 0,
top: 0,
right: width,
bottom: height,
width: width,
height: height
}, x, y)
}
function pointerHandler(_ref2) {
let {
data: data
} = _ref2;
const {
widget: widget
} = data;
widget._enableOutHandler();
widget._showTooltip()
}
function getDefaultTemplate(_ref3, textAlign) {
let {
lineSpacing: lineSpacing,
size: size
} = _ref3;
const lineHeight = `${(lineSpacing??2)+size}px`;
return function(_ref4, container) {
let {
valueText: valueText
} = _ref4;
const table = $("<table>").css({
borderSpacing: 0,
lineHeight: lineHeight
});
for (let i = 0; i < valueText.length; i += 2) {
const tr = $("<tr>");
$("<td>").text(valueText[i]).appendTo(tr);
$("<td>").css({
width: 15
}).appendTo(tr);
$("<td>").css({
textAlign: textAlign
}).text(valueText[i + 1]).appendTo(tr);
table.append(tr)
}
container.append(table)
}
}
function createAxis(isHorizontal) {
const translator = new Translator2D({}, {}, {
shiftZeroValue: !isHorizontal,
isHorizontal: !!isHorizontal
});
return {
getTranslator: () => translator,
update(range, canvas, options) {
translator.update(range, canvas, options)
},
getVisibleArea() {
const visibleArea = translator.getCanvasVisibleArea();
return [visibleArea.min, visibleArea.max]
},
visualRange: noop,
calculateInterval: noop,
getMarginOptions: () => ({}),
aggregatedPointBetweenTicks: () => false
}
}
class BaseSparkline extends BaseWidget {
_initCore() {
this._tooltipTracker = this._renderer.root;
this._tooltipTracker.attr({
"pointer-events": "visible"
});
this._createHtmlElements();
this._initTooltipEvents();
this._argumentAxis = createAxis(true);
this._valueAxis = createAxis()
}
_getDefaultSize() {
return this._defaultSize
}
_disposeCore() {
this._disposeWidgetElements();
this._disposeTooltipEvents();
this._ranges = null
}
_change_OPTIONS() {
this._prepareOptions();
this._change(["UPDATE"])
}
_change_UPDATE() {
this._update()
}
_update() {
if (this._tooltipShown) {
this._tooltipShown = false;
this._tooltip.hide()
}
this._cleanWidgetElements();
this._updateWidgetElements();
this._drawWidgetElements()
}
_updateWidgetElements() {
const canvas = this._getCorrectCanvas();
this._updateRange();
this._argumentAxis.update(this._ranges.arg, canvas, this._getStick());
this._valueAxis.update(this._ranges.val, canvas)
}
_getStick() {
return
}
_applySize(rect) {
this._allOptions.size = {
width: rect[2] - rect[0],
height: rect[3] - rect[1]
};
this._change(["UPDATE"])
}
_prepareOptions() {
return extend(true, {}, this._themeManager.theme(), this.option())
}
_getTooltipCoords() {
const canvas = this._canvas;
const rootOffset = this._renderer.getRootOffset();
return {
x: canvas.width / 2 + rootOffset.left,
y: canvas.height / 2 + rootOffset.top
}
}
_initTooltipEvents() {
const data = {
widget: this
};
this._renderer.root.off(`.${EVENT_NS}`).on(POINTER_ACTION, data, pointerHandler)
}
_showTooltip() {
if (!this._tooltipShown) {
this._tooltipShown = true;
const tooltip = this._getTooltip();
if (tooltip.isEnabled()) {
this._tooltip.show(this._getTooltipData(), this._getTooltipCoords(), {})
}
}
}
_hideTooltip() {
if (this._tooltipShown) {
this._tooltipShown = false;
this._tooltip.hide()
}
}
_stopCurrentHandling() {
this._hideTooltip()
}
_enableOutHandler() {
if (this._outHandler) {
return
}
const handler = _ref5 => {
let {
pageX: pageX,
pageY: pageY
} = _ref5;
const {
left: left,
top: top
} = this._renderer.getRootOffset();
const x = Math.floor(pageX - left);
const y = Math.floor(pageY - top);
if (!inCanvas(this._canvas, x, y)) {
this._hideTooltip();
this._disableOutHandler()
}
};
eventsEngine.on(domAdapter.getDocument(), POINTER_ACTION, handler);
this._outHandler = handler
}
_disableOutHandler() {
if (this._outHandler) {
eventsEngine.off(domAdapter.getDocument(), POINTER_ACTION, this._outHandler)
}
this._outHandler = null
}
_disposeTooltipEvents() {
this._tooltipTracker.off();
this._disableOutHandler();
this._renderer.root.off(`.${EVENT_NS}`)
}
_getTooltip() {
if (!this._tooltip) {
initTooltip.call(this);
this._setTooltipRendererOptions();
this._tooltipRendererOptions = null;
this._setTooltipOptions()
}
return this._tooltip
}
_getDefaultTooltipTemplate(options) {
let defaultTemplateNeeded = true;
const textAlign = this.option("rtlEnabled") ? "left" : "right";
if (isFunction(options.customizeTooltip)) {
this._tooltip.update(options);
const formatObject = this._getTooltipData();
const customizeResult = options.customizeTooltip.call(formatObject, formatObject) ?? {};
defaultTemplateNeeded = !("html" in customizeResult) && !("text" in customizeResult)
}
return defaultTemplateNeeded && getDefaultTemplate(options.font, textAlign)
}
}
setupWidgetPrototype(BaseSparkline, {
_getLayoutItems: noop,
_useLinks: false,
_themeDependentChanges: ["OPTIONS"],
_optionChangesOrder: ["OPTIONS"],
_customChangesOrder: ["UPDATE"],
_setupResizeHandler: noop
});
BaseSparkline.addPlugin(tooltipPlugin);
const {
_disposeTooltip: disposeTooltip
} = BaseSparkline.prototype;
function disposeLazyTooltip() {
if (this._tooltip) {
disposeTooltip.call(this);
this._tooltipShown = false
}
}
function setLazyTooltipRendererOptions() {
const options = this._getRendererOptions();
if (this._tooltip) {
this._tooltip.setRendererOptions(options)
} else {
this._tooltipRendererOptions = options
}
}
function setLazyTooltipOptions() {
if (this._tooltip) {
const options = this._getOption("tooltip");
const defaultContentTemplate = this._getDefaultTooltipTemplate(options);
const contentTemplateOptions = defaultContentTemplate ? {
contentTemplate: defaultContentTemplate
} : {};
const optionsToUpdate = extend(contentTemplateOptions, options, {
enabled: options.enabled && this._isTooltipEnabled()
});
this._tooltip.update(optionsToUpdate)
}
}
BaseSparkline.prototype._initTooltip = noop;
BaseSparkline.prototype._disposeTooltip = disposeLazyTooltip;
BaseSparkline.prototype._setTooltipRendererOptions = setLazyTooltipRendererOptions;
BaseSparkline.prototype._setTooltipOptions = setLazyTooltipOptions;
const exportPlugin = extend(true, {}, plugin, {
init: noop,
dispose: noop,
customize: null,
members: {
_getExportMenuOptions: null
}
});
BaseSparkline.addPlugin(exportPlugin);
export default BaseSparkline;