devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
486 lines (477 loc) • 17.2 kB
JavaScript
/**
* DevExtreme (cjs/__internal/viz/sparklines/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/
*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _component_registrator = require("../../core/component_registrator");
var _m_type = require("../../core/utils/m_type");
var _data_validator = require("../../viz/components/data_validator");
var _data_source = require("../../viz/core/data_source");
var _helpers = require("../../viz/core/helpers");
var _utils = require("../../viz/core/utils");
var _base_series = require("../../viz/series/base_series");
var _base_sparkline = _interopRequireDefault(require("../../viz/sparklines/base_sparkline"));
function _interopRequireDefault(e) {
return e && e.__esModule ? e : {
default: e
}
}
const MIN_BAR_WIDTH = 1;
const MAX_BAR_WIDTH = 50;
const DEFAULT_BAR_INTERVAL = 4;
const DEFAULT_CANVAS_WIDTH = 250;
const DEFAULT_CANVAS_HEIGHT = 30;
const DEFAULT_POINT_BORDER = 2;
const ALLOWED_TYPES = {
line: true,
spline: true,
stepline: true,
area: true,
steparea: true,
splinearea: true,
bar: true,
winloss: true
};
function findMinMax(data, valField) {
const firstItem = data[0] || {};
const firstValue = firstItem[valField] || 0;
let min = firstValue;
let max = firstValue;
let minIndexes = [0];
let maxIndexes = [0];
for (let i = 1; i < data.length; i += 1) {
const value = data[i][valField];
if (value < min) {
min = value;
minIndexes = [i]
} else if (value === min) {
minIndexes.push(i)
}
if (value > max) {
max = value;
maxIndexes = [i]
} else if (value === max) {
maxIndexes.push(i)
}
}
if (max === min) {
minIndexes = [];
maxIndexes = []
}
return {
minIndexes: minIndexes,
maxIndexes: maxIndexes
}
}
function parseNumericValue(value, ignoreEmptyPoints) {
if (null === value) {
return ignoreEmptyPoints ? void 0 : value
}
return Number(value)
}
function parseNumericDataSource(data, argField, valField, ignoreEmptyPoints) {
const result = (0, _utils.map)(data, (dataItem, index) => {
if (void 0 === dataItem) {
return null
}
const isDataNumber = isFinite(dataItem);
const item = {};
item[argField] = isDataNumber ? String(index) : dataItem[argField];
const value = isDataNumber ? dataItem : dataItem[valField];
item[valField] = parseNumericValue(value, ignoreEmptyPoints);
return void 0 !== item[argField] && void 0 !== item[valField] ? item : null
});
return result
}
function parseWinlossDataSource(data, argField, valField, target) {
const result = (0, _utils.map)(data, dataItem => {
const item = {};
item[argField] = dataItem[argField];
if (Math.abs(dataItem[valField] - target) < 1e-4) {
item[valField] = 0
} else if (dataItem[valField] > target) {
item[valField] = 1
} else {
item[valField] = -1
}
return item
});
return result
}
function selectPointColor(color, options, index, pointIndexes) {
let result = color;
if (index === pointIndexes.first || index === pointIndexes.last) {
result = options.firstLastColor
}
if ((pointIndexes.min || []).includes(index)) {
result = options.minColor
}
if ((pointIndexes.max || []).includes(index)) {
result = options.maxColor
}
return result
}
function createLineCustomizeFunction(pointIndexes, options) {
return function() {
const color = selectPointColor(void 0, options, this.index, pointIndexes);
return color ? {
visible: true,
border: {
color: color
}
} : {}
}
}
function createBarCustomizeFunction(pointIndexes, options, winlossData) {
return function() {
const {
index: index
} = this;
const isWinloss = "winloss" === options.type;
const target = isWinloss ? options.winlossThreshold : 0;
const value = isWinloss ? winlossData[index][options.valueField] : this.value;
const positiveColor = isWinloss ? options.winColor : options.barPositiveColor;
const negativeColor = isWinloss ? options.lossColor : options.barNegativeColor;
return {
color: selectPointColor(value >= target ? positiveColor : negativeColor, options, index, pointIndexes)
}
}
}
class Sparkline extends _base_sparkline.default {
_initCore() {
super._initCore();
this._createSeries()
}
_dataSourceChangedHandler() {
this._requestChange(["UPDATE"])
}
_updateWidgetElements() {
this._updateSeries();
super._updateWidgetElements()
}
_disposeWidgetElements() {
if (this._series) {
this._series.dispose()
}
this._series = null;
this._seriesGroup = null;
this._seriesLabelGroup = null
}
_cleanWidgetElements() {
this._seriesGroup.remove();
this._seriesLabelGroup.remove();
this._seriesGroup.clear();
this._seriesLabelGroup.clear();
this._series.removeGraphicElements();
this._series.removePointElements();
this._series.removeBordersGroup()
}
_drawWidgetElements() {
if (this._dataIsLoaded()) {
this._drawSeries();
this._drawn()
}
}
_getCorrectCanvas() {
const options = this._allOptions;
const canvas = this._canvas;
const halfPointSize = options.pointSize && Math.ceil(options.pointSize / 2) + 2;
const {
type: type
} = options;
if ("bar" !== type && "winloss" !== type && (options.showFirstLast || options.showMinMax)) {
return {
width: canvas.width,
height: canvas.height,
left: canvas.left + halfPointSize,
right: canvas.right + halfPointSize,
top: canvas.top + halfPointSize,
bottom: canvas.bottom + halfPointSize
}
}
return canvas
}
_prepareOptions() {
this._allOptions = super._prepareOptions();
this._allOptions.type = (0, _utils.normalizeEnum)(this._allOptions.type);
if (!ALLOWED_TYPES[this._allOptions.type]) {
this._allOptions.type = "line"
}
}
_createHtmlElements() {
this._seriesGroup = this._renderer.g().attr({
class: "dxsl-series"
});
this._seriesLabelGroup = this._renderer.g().attr({
class: "dxsl-series-labels"
})
}
_createSeries() {
this._series = new _base_series.Series({
renderer: this._renderer,
seriesGroup: this._seriesGroup,
labelsGroup: this._seriesLabelGroup,
argumentAxis: this._argumentAxis,
valueAxis: this._valueAxis,
incidentOccurred: this._incidentOccurred
}, {
widgetType: "chart",
type: "line"
})
}
_updateSeries() {
const singleSeries = this._series;
this._prepareDataSource();
const seriesOptions = this._prepareSeriesOptions();
singleSeries.updateOptions(seriesOptions);
const groupsData = {
groups: [{
series: [singleSeries]
}]
};
groupsData.argumentOptions = {
type: "bar" === seriesOptions.type ? "discrete" : void 0
};
this._simpleDataSource = (0, _data_validator.validateData)(this._simpleDataSource, groupsData, this._incidentOccurred, {
checkTypeForAllData: false,
convertToAxisDataType: true,
sortingMethod: true
})[singleSeries.getArgumentField()];
seriesOptions.customizePoint = this._getCustomizeFunction();
singleSeries.updateData(this._simpleDataSource);
singleSeries.createPoints();
this._groupsDataCategories = groupsData.categories
}
_change_DATA_SOURCE() {
this._updateDataSource()
}
_prepareDataSource() {
const options = this._allOptions;
const argField = options.argumentField;
const valField = options.valueField;
const dataSource = this._dataSourceItems() || [];
const data = parseNumericDataSource(dataSource, argField, valField, this.option("ignoreEmptyPoints"));
if ("winloss" === options.type) {
this._winlossDataSource = data;
this._simpleDataSource = parseWinlossDataSource(data, argField, valField, options.winlossThreshold)
} else {
this._simpleDataSource = data
}
}
_prepareSeriesOptions() {
const options = this._allOptions;
const type = "winloss" === options.type ? "bar" : options.type;
return {
visible: true,
argumentField: options.argumentField,
valueField: options.valueField,
color: options.lineColor,
width: options.lineWidth,
widgetType: "chart",
name: "",
type: type,
opacity: -1 !== type.indexOf("area") ? this._allOptions.areaOpacity : void 0,
point: {
size: options.pointSize,
symbol: options.pointSymbol,
border: {
visible: true,
width: 2
},
color: options.pointColor,
visible: false,
hoverStyle: {
border: {}
},
selectionStyle: {
border: {}
}
},
border: {
color: options.lineColor,
width: options.lineWidth,
visible: "bar" !== type
}
}
}
_getCustomizeFunction() {
const options = this._allOptions;
const dataSource = this._winlossDataSource || this._simpleDataSource;
const drawnPointIndexes = this._getExtremumPointsIndexes(dataSource);
if ("winloss" === options.type || "bar" === options.type) {
return createBarCustomizeFunction(drawnPointIndexes, options, this._winlossDataSource)
}
return createLineCustomizeFunction(drawnPointIndexes, options)
}
_getExtremumPointsIndexes(data) {
const options = this._allOptions;
const lastIndex = data.length - 1;
const indexes = {};
this._minMaxIndexes = findMinMax(data, options.valueField);
if (options.showFirstLast) {
indexes.first = 0;
indexes.last = lastIndex
}
if (options.showMinMax) {
indexes.min = this._minMaxIndexes.minIndexes;
indexes.max = this._minMaxIndexes.maxIndexes
}
return indexes
}
_getStick() {
return {
stick: "bar" !== this._series.type
}
}
_updateRange() {
const series = this._series;
const {
type: type
} = series;
const isBarType = "bar" === type;
const isWinlossType = "winloss" === type;
const rangeData = series.getRangeData();
const {
minValue: minValue
} = this._allOptions;
const hasMinY = (0, _m_type.isDefined)(minValue) && isFinite(minValue);
const {
maxValue: maxValue
} = this._allOptions;
const hasMaxY = (0, _m_type.isDefined)(maxValue) && isFinite(maxValue);
const valCoef = .15 * (rangeData.val.max - rangeData.val.min);
if (isBarType || isWinlossType || "area" === type) {
if (0 !== rangeData.val.min) {
rangeData.val.min -= valCoef
}
if (0 !== rangeData.val.max) {
rangeData.val.max += valCoef
}
} else {
rangeData.val.min -= valCoef;
rangeData.val.max += valCoef
}
if (hasMinY || hasMaxY) {
if (hasMinY && hasMaxY) {
rangeData.val.minVisible = Math.min(minValue, maxValue);
rangeData.val.maxVisible = Math.max(minValue, maxValue)
} else {
rangeData.val.minVisible = hasMinY ? Number(minValue) : void 0;
rangeData.val.maxVisible = hasMaxY ? Number(maxValue) : void 0
}
if (isWinlossType) {
rangeData.val.minVisible = hasMinY ? Math.max(rangeData.val.minVisible, -1) : void 0;
rangeData.val.maxVisible = hasMaxY ? Math.min(rangeData.val.maxVisible, 1) : void 0
}
}
if (series.getPoints().length > 1) {
if (isBarType) {
const argCoef = .1 * (rangeData.arg.max - rangeData.arg.min);
rangeData.arg.min -= argCoef;
rangeData.arg.max += argCoef
}
}
rangeData.arg.categories = this._groupsDataCategories;
this._ranges = rangeData
}
_getBarWidth(pointsCount) {
const canvas = this._canvas;
const intervalWidth = 4 * pointsCount;
const rangeWidth = canvas.width - canvas.left - canvas.right - intervalWidth;
let width = Math.round(rangeWidth / pointsCount);
if (width < 1) {
width = 1
}
if (width > 50) {
width = 50
}
return width
}
_correctPoints() {
const seriesType = this._allOptions.type;
const seriesPoints = this._series.getPoints();
const pointsLength = seriesPoints.length;
if ("bar" === seriesType || "winloss" === seriesType) {
const barWidth = this._getBarWidth(pointsLength);
for (let i = 0; i < pointsLength; i += 1) {
seriesPoints[i].correctCoordinates({
width: barWidth,
offset: 0
})
}
}
}
_drawSeries() {
if (this._simpleDataSource.length > 0) {
this._correctPoints();
this._series.draw();
this._seriesGroup.append(this._renderer.root)
}
}
_isTooltipEnabled() {
return !!this._simpleDataSource.length
}
_getTooltipData() {
const options = this._allOptions;
const dataSource = this._winlossDataSource || this._simpleDataSource;
const tooltip = this._tooltip;
if (0 === dataSource.length) {
return {}
}
const minMax = this._minMaxIndexes;
const {
valueField: valueField
} = options;
const first = dataSource[0][valueField];
const last = dataSource[dataSource.length - 1][valueField];
const min = (0, _m_type.isDefined)(minMax.minIndexes[0]) ? dataSource[minMax.minIndexes[0]][valueField] : first;
const max = (0, _m_type.isDefined)(minMax.maxIndexes[0]) ? dataSource[minMax.maxIndexes[0]][valueField] : first;
const formattedFirst = tooltip.formatValue(first);
const formattedLast = tooltip.formatValue(last);
const formattedMin = tooltip.formatValue(min);
const formattedMax = tooltip.formatValue(max);
const customizeObject = {
firstValue: formattedFirst,
lastValue: formattedLast,
minValue: formattedMin,
maxValue: formattedMax,
originalFirstValue: first,
originalLastValue: last,
originalMinValue: min,
originalMaxValue: max,
valueText: ["Start:", formattedFirst, "End:", formattedLast, "Min:", formattedMin, "Max:", formattedMax]
};
if ("winloss" === options.type) {
customizeObject.originalThresholdValue = options.winlossThreshold;
customizeObject.thresholdValue = tooltip.formatValue(options.winlossThreshold)
}
return customizeObject
}
}(0, _helpers.setupWidgetPrototype)(Sparkline, {
_rootClassPrefix: "dxsl",
_rootClass: "dxsl-sparkline",
_themeSection: "sparkline",
_defaultSize: {
width: 250,
height: 30
},
_initialChanges: ["DATA_SOURCE"],
_optionChangesMap: {
dataSource: "DATA_SOURCE"
},
_optionChangesOrder: ["DATA_SOURCE"]
});
["lossColor", "lineColor", "lineWidth", "areaOpacity", "minColor", "maxColor", "barPositiveColor", "barNegativeColor", "winColor", "lessColor", "firstLastColor", "pointSymbol", "pointColor", "pointSize", "type", "argumentField", "valueField", "winlossThreshold", "showFirstLast", "showMinMax", "ignoreEmptyPoints", "minValue", "maxValue"].forEach(name => {
Sparkline.prototype._optionChangesMap[name] = "OPTIONS"
});
(0, _component_registrator.registerComponent)("dxSparkline", Sparkline);
Sparkline.addPlugin(_data_source.plugin);
var _default = exports.default = Sparkline;