Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3284 vikas 1
/*
2
Flot plugin for showing crosshairs, thin lines, when the mouse hovers
3
over the plot.
4
 
5
  crosshair: {
6
    mode: null or "x" or "y" or "xy"
7
    color: color
8
    lineWidth: number
9
  }
10
 
11
Set the mode to one of "x", "y" or "xy". The "x" mode enables a
12
vertical crosshair that lets you trace the values on the x axis, "y"
13
enables a horizontal crosshair and "xy" enables them both. "color" is
14
the color of the crosshair (default is "rgba(170, 0, 0, 0.80)"),
15
"lineWidth" is the width of the drawn lines (default is 1).
16
 
17
The plugin also adds four public methods:
18
 
19
  - setCrosshair(pos)
20
 
21
    Set the position of the crosshair. Note that this is cleared if
22
    the user moves the mouse. "pos" is in coordinates of the plot and
23
    should be on the form { x: xpos, y: ypos } (you can use x2/x3/...
24
    if you're using multiple axes), which is coincidentally the same
25
    format as what you get from a "plothover" event. If "pos" is null,
26
    the crosshair is cleared.
27
 
28
  - clearCrosshair()
29
 
30
    Clear the crosshair.
31
 
32
  - lockCrosshair(pos)
33
 
34
    Cause the crosshair to lock to the current location, no longer
35
    updating if the user moves the mouse. Optionally supply a position
36
    (passed on to setCrosshair()) to move it to.
37
 
38
    Example usage:
39
      var myFlot = $.plot( $("#graph"), ..., { crosshair: { mode: "x" } } };
40
      $("#graph").bind("plothover", function (evt, position, item) {
41
        if (item) {
42
          // Lock the crosshair to the data point being hovered
43
          myFlot.lockCrosshair({ x: item.datapoint[0], y: item.datapoint[1] });
44
        }
45
        else {
46
          // Return normal crosshair operation
47
          myFlot.unlockCrosshair();
48
        }
49
      });
50
 
51
  - unlockCrosshair()
52
 
53
    Free the crosshair to move again after locking it.
54
*/
55
 
56
(function ($) {
57
    var options = {
58
        crosshair: {
59
            mode: null, // one of null, "x", "y" or "xy",
60
            color: "rgba(170, 0, 0, 0.80)",
61
            lineWidth: 1
62
        }
63
    };
64
 
65
    function init(plot) {
66
        // position of crosshair in pixels
67
        var crosshair = { x: -1, y: -1, locked: false };
68
 
69
        plot.setCrosshair = function setCrosshair(pos) {
70
            if (!pos)
71
                crosshair.x = -1;
72
            else {
73
                var o = plot.p2c(pos);
74
                crosshair.x = Math.max(0, Math.min(o.left, plot.width()));
75
                crosshair.y = Math.max(0, Math.min(o.top, plot.height()));
76
            }
77
 
78
            plot.triggerRedrawOverlay();
79
        };
80
 
81
        plot.clearCrosshair = plot.setCrosshair; // passes null for pos
82
 
83
        plot.lockCrosshair = function lockCrosshair(pos) {
84
            if (pos)
85
                plot.setCrosshair(pos);
86
            crosshair.locked = true;
87
        }
88
 
89
        plot.unlockCrosshair = function unlockCrosshair() {
90
            crosshair.locked = false;
91
        }
92
 
93
        function onMouseOut(e) {
94
            if (crosshair.locked)
95
                return;
96
 
97
            if (crosshair.x != -1) {
98
                crosshair.x = -1;
99
                plot.triggerRedrawOverlay();
100
            }
101
        }
102
 
103
        function onMouseMove(e) {
104
            if (crosshair.locked)
105
                return;
106
 
107
            if (plot.getSelection && plot.getSelection()) {
108
                crosshair.x = -1; // hide the crosshair while selecting
109
                return;
110
            }
111
 
112
            var offset = plot.offset();
113
            crosshair.x = Math.max(0, Math.min(e.pageX - offset.left, plot.width()));
114
            crosshair.y = Math.max(0, Math.min(e.pageY - offset.top, plot.height()));
115
            plot.triggerRedrawOverlay();
116
        }
117
 
118
        plot.hooks.bindEvents.push(function (plot, eventHolder) {
119
            if (!plot.getOptions().crosshair.mode)
120
                return;
121
 
122
            eventHolder.mouseout(onMouseOut);
123
            eventHolder.mousemove(onMouseMove);
124
        });
125
 
126
        plot.hooks.drawOverlay.push(function (plot, ctx) {
127
            var c = plot.getOptions().crosshair;
128
            if (!c.mode)
129
                return;
130
 
131
            var plotOffset = plot.getPlotOffset();
132
 
133
            ctx.save();
134
            ctx.translate(plotOffset.left, plotOffset.top);
135
 
136
            if (crosshair.x != -1) {
137
                ctx.strokeStyle = c.color;
138
                ctx.lineWidth = c.lineWidth;
139
                ctx.lineJoin = "round";
140
 
141
                ctx.beginPath();
142
                if (c.mode.indexOf("x") != -1) {
143
                    ctx.moveTo(crosshair.x, 0);
144
                    ctx.lineTo(crosshair.x, plot.height());
145
                }
146
                if (c.mode.indexOf("y") != -1) {
147
                    ctx.moveTo(0, crosshair.y);
148
                    ctx.lineTo(plot.width(), crosshair.y);
149
                }
150
                ctx.stroke();
151
            }
152
            ctx.restore();
153
        });
154
 
155
        plot.hooks.shutdown.push(function (plot, eventHolder) {
156
            eventHolder.unbind("mouseout", onMouseOut);
157
            eventHolder.unbind("mousemove", onMouseMove);
158
        });
159
    }
160
 
161
    $.plot.plugins.push({
162
        init: init,
163
        options: options,
164
        name: 'crosshair',
165
        version: '1.0'
166
    });
167
})(jQuery);