prometheus
1/*
2SPDX-License-Identifier: MIT
3Source: https://github.com/grafana/grafana/blob/main/public/vendor/flot/jquery.flot.stock.js
4*/
5
6/* eslint-disable prefer-spread */
7/* eslint-disable no-loop-func */
8/* eslint-disable @typescript-eslint/no-this-alias */
9/* eslint-disable no-redeclare */
10/* eslint-disable no-useless-escape */
11/* eslint-disable prefer-const */
12/* eslint-disable @typescript-eslint/explicit-function-return-type */
13/* eslint-disable @typescript-eslint/no-use-before-define */
14/* eslint-disable eqeqeq */
15/* eslint-disable no-var */
16
17/* Flot plugin for stacking data sets rather than overlyaing them.
18
19Copyright (c) 2007-2014 IOLA and Ole Laursen.
20Licensed under the MIT license.
21
22The plugin assumes the data is sorted on x (or y if stacking horizontally).
23For line charts, it is assumed that if a line has an undefined gap (from a
24null point), then the line above it should have the same gap - insert zeros
25instead of "null" if you want another behaviour. This also holds for the start
26and end of the chart. Note that stacking a mix of positive and negative values
27in most instances doesn't make sense (so it looks weird).
28
29Two or more series are stacked when their "stack" attribute is set to the same
30key (which can be any number or string or just "true"). To specify the default
31stack, you can set the stack option like this:
32
33series: {
34stack: null/false, true, or a key (number/string)
35}
36
37You can also specify it for a single series, like this:
38
39$.plot( $("#placeholder"), [{
40data: [ ... ],
41stack: true
42}])
43
44The stacking order is determined by the order of the data series in the array
45(later series end up on top of the previous).
46
47Internally, the plugin modifies the datapoints in each series, adding an
48offset to the y value. For line series, extra data points are inserted through
49interpolation. If there's a second y value, it's also adjusted (e.g for bar
50charts or filled areas).
51
52*/
53
54(function($) {55const options = {56series: { stack: null }, // or number/string57};58
59function init(plot) {60function findMatchingSeries(s, allseries) {61let res = null;62for (let i = 0; i < allseries.length; ++i) {63if (s == allseries[i]) break;64
65if (allseries[i].stack == s.stack) res = allseries[i];66}67
68return res;69}70
71function stackData(plot, s, datapoints) {72if (s.stack == null || s.stack === false) return;73
74const other = findMatchingSeries(s, plot.getData());75if (!other) return;76
77let ps = datapoints.pointsize,78points = datapoints.points,79otherps = other.datapoints.pointsize,80otherpoints = other.datapoints.points,81newpoints = [],82px,83py,84intery,85qx,86qy,87bottom,88withlines = s.lines.show,89horizontal = s.bars.horizontal,90withbottom = ps > 2 && (horizontal ? datapoints.format[2].x : datapoints.format[2].y),91withsteps = withlines && s.lines.steps,92keyOffset = horizontal ? 1 : 0,93accumulateOffset = horizontal ? 0 : 1,94i = 0,95j = 0,96l,97m;98
99while (true) {100if (i >= points.length && j >= otherpoints.length) break;101
102l = newpoints.length;103
104if (i < points.length && points[i] == null) {105// take the points from the previous series106for (m = 0; m < ps; ++m) newpoints.push(otherpoints[i + m]);107if (withbottom) newpoints[l + 2] = otherpoints[i + accumulateOffset];108i += ps;109} else if (i >= points.length) {110j += otherps;111} else if (j >= otherpoints.length) {112// take the remaining points from the current series113for (m = 0; m < ps; ++m) newpoints.push(points[i + m]);114i += ps;115} else if (j < otherpoints.length && otherpoints[j] == null) {116// ignore point117j += otherps;118} else {119// cases where we actually got two points120px = points[i + keyOffset];121py = points[i + accumulateOffset];122qx = otherpoints[j + keyOffset];123qy = otherpoints[j + accumulateOffset];124bottom = 0;125
126if (px == qx) {127for (m = 0; m < ps; ++m) newpoints.push(points[i + m]);128
129newpoints[l + accumulateOffset] += qy;130bottom = qy;131
132i += ps;133j += otherps;134} else if (px > qx) {135// take the point from the previous series so that next series will correctly stack136if (i == 0) {137for (m = 0; m < ps; ++m) newpoints.push(otherpoints[j + m]);138bottom = qy;139}140// we got past point below, might need to141// insert interpolated extra point142if (i > 0 && points[i - ps] != null) {143intery = py + ((points[i - ps + accumulateOffset] - py) * (qx - px)) / (points[i - ps + keyOffset] - px);144newpoints.push(qx);145newpoints.push(intery + qy);146for (m = 2; m < ps; ++m) newpoints.push(points[i + m]);147bottom = qy;148}149
150j += otherps;151} else {152// px < qx153for (m = 0; m < ps; ++m) newpoints.push(points[i + m]);154
155// we might be able to interpolate a point below,156// this can give us a better y157if (j > 0 && otherpoints[j - otherps] != null)158bottom =159qy +160((otherpoints[j - otherps + accumulateOffset] - qy) * (px - qx)) /161(otherpoints[j - otherps + keyOffset] - qx);162
163newpoints[l + accumulateOffset] += bottom;164
165i += ps;166}167
168if (l != newpoints.length && withbottom) newpoints[l + 2] = bottom;169}170
171// maintain the line steps invariant172if (173withsteps &&174l != newpoints.length &&175l > 0 &&176newpoints[l] != null &&177newpoints[l] != newpoints[l - ps] &&178newpoints[l + 1] != newpoints[l - ps + 1]179) {180for (m = 0; m < ps; ++m) newpoints[l + ps + m] = newpoints[l + m];181newpoints[l + 1] = newpoints[l - ps + 1];182}183}184
185datapoints.points = newpoints;186}187
188plot.hooks.processDatapoints.push(stackData);189}190
191$.plot.plugins.push({192init: init,193options: options,194name: 'stack',195version: '1.2',196});197})(window.jQuery);198