prometheus

Форк
0
/
jquery.flot.crosshair.js 
185 строк · 5.2 Кб
1
/*
2
SPDX-License-Identifier: MIT
3
Source: https://github.com/grafana/grafana/blob/main/public/vendor/flot/jquery.flot.crosshair.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
/* Flot plugin for showing crosshairs when the mouse hovers over the plot.
17

18
Copyright (c) 2007-2014 IOLA and Ole Laursen.
19
Licensed under the MIT license.
20

21
The plugin supports these options:
22

23
	crosshair: {
24
		mode: null or "x" or "y" or "xy"
25
		color: color
26
		lineWidth: number
27
	}
28

29
Set the mode to one of "x", "y" or "xy". The "x" mode enables a vertical
30
crosshair that lets you trace the values on the x axis, "y" enables a
31
horizontal crosshair and "xy" enables them both. "color" is the color of the
32
crosshair (default is "rgba(170, 0, 0, 0.80)"), "lineWidth" is the width of
33
the drawn lines (default is 1).
34

35
The plugin also adds four public methods:
36

37
  - setCrosshair( pos )
38

39
    Set the position of the crosshair. Note that this is cleared if the user
40
    moves the mouse. "pos" is in coordinates of the plot and should be on the
41
    form { x: xpos, y: ypos } (you can use x2/x3/... if you're using multiple
42
    axes), which is coincidentally the same format as what you get from a
43
    "plothover" event. If "pos" is null, the crosshair is cleared.
44

45
  - clearCrosshair()
46

47
    Clear the crosshair.
48

49
  - lockCrosshair(pos)
50

51
    Cause the crosshair to lock to the current location, no longer updating if
52
    the user moves the mouse. Optionally supply a position (passed on to
53
    setCrosshair()) to move it to.
54

55
    Example usage:
56

57
	var myFlot = $.plot( $("#graph"), ..., { crosshair: { mode: "x" } } };
58
	$("#graph").bind( "plothover", function ( evt, position, item ) {
59
		if ( item ) {
60
			// Lock the crosshair to the data point being hovered
61
			myFlot.lockCrosshair({
62
				x: item.datapoint[ 0 ],
63
				y: item.datapoint[ 1 ]
64
			});
65
		} else {
66
			// Return normal crosshair operation
67
			myFlot.unlockCrosshair();
68
		}
69
	});
70

71
  - unlockCrosshair()
72

73
    Free the crosshair to move again after locking it.
74
*/
75

76
(function($) {
77
  const options = {
78
    crosshair: {
79
      mode: null, // one of null, "x", "y" or "xy",
80
      color: 'rgba(170, 0, 0, 0.80)',
81
      lineWidth: 1,
82
    },
83
  };
84

85
  function init(plot) {
86
    // position of crosshair in pixels
87
    const crosshair = { x: -1, y: -1, locked: false };
88

89
    plot.setCrosshair = function setCrosshair(pos) {
90
      if (!pos) crosshair.x = -1;
91
      else {
92
        const o = plot.p2c(pos);
93
        crosshair.x = Math.max(0, Math.min(o.left, plot.width()));
94
        crosshair.y = Math.max(0, Math.min(o.top, plot.height()));
95
      }
96

97
      plot.triggerRedrawOverlay();
98
    };
99

100
    plot.clearCrosshair = plot.setCrosshair; // passes null for pos
101

102
    plot.lockCrosshair = function lockCrosshair(pos) {
103
      if (pos) plot.setCrosshair(pos);
104
      crosshair.locked = true;
105
    };
106

107
    plot.unlockCrosshair = function unlockCrosshair() {
108
      crosshair.locked = false;
109
    };
110

111
    function onMouseOut() {
112
      if (crosshair.locked) return;
113

114
      if (crosshair.x != -1) {
115
        crosshair.x = -1;
116
        plot.triggerRedrawOverlay();
117
      }
118
    }
119

120
    function onMouseMove(e) {
121
      if (crosshair.locked) return;
122

123
      if (plot.getSelection && plot.getSelection()) {
124
        crosshair.x = -1; // hide the crosshair while selecting
125
        return;
126
      }
127

128
      const offset = plot.offset();
129
      crosshair.x = Math.max(0, Math.min(e.pageX - offset.left, plot.width()));
130
      crosshair.y = Math.max(0, Math.min(e.pageY - offset.top, plot.height()));
131
      plot.triggerRedrawOverlay();
132
    }
133

134
    plot.hooks.bindEvents.push(function(plot, eventHolder) {
135
      if (!plot.getOptions().crosshair.mode) return;
136

137
      eventHolder.mouseout(onMouseOut);
138
      eventHolder.mousemove(onMouseMove);
139
    });
140

141
    plot.hooks.drawOverlay.push(function(plot, ctx) {
142
      const c = plot.getOptions().crosshair;
143
      if (!c.mode) return;
144

145
      const plotOffset = plot.getPlotOffset();
146

147
      ctx.save();
148
      ctx.translate(plotOffset.left, plotOffset.top);
149

150
      if (crosshair.x != -1) {
151
        const adj = plot.getOptions().crosshair.lineWidth % 2 ? 0.5 : 0;
152

153
        ctx.strokeStyle = c.color;
154
        ctx.lineWidth = c.lineWidth;
155
        ctx.lineJoin = 'round';
156

157
        ctx.beginPath();
158
        if (c.mode.indexOf('x') != -1) {
159
          const drawX = Math.floor(crosshair.x) + adj;
160
          ctx.moveTo(drawX, 0);
161
          ctx.lineTo(drawX, plot.height());
162
        }
163
        if (c.mode.indexOf('y') != -1) {
164
          const drawY = Math.floor(crosshair.y) + adj;
165
          ctx.moveTo(0, drawY);
166
          ctx.lineTo(plot.width(), drawY);
167
        }
168
        ctx.stroke();
169
      }
170
      ctx.restore();
171
    });
172

173
    plot.hooks.shutdown.push(function(plot, eventHolder) {
174
      eventHolder.unbind('mouseout', onMouseOut);
175
      eventHolder.unbind('mousemove', onMouseMove);
176
    });
177
  }
178

179
  $.plot.plugins.push({
180
    init: init,
181
    options: options,
182
    name: 'crosshair',
183
    version: '1.0',
184
  });
185
})(window.jQuery);
186

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.