3 * Copyright 2015 Petr Shevtsov (petr.shevtsov@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
7 /*global Dygraph:false */
8 /*jshint globalstrict: true */
9 Dygraph
.Plugins
.Crosshair
= (function() {
13 * Creates the crosshair
18 var crosshair
= function(opt_options
) {
19 this.canvas_
= document
.createElement("canvas");
20 opt_options
= opt_options
|| {};
21 this.direction_
= opt_options
.direction
|| null;
24 crosshair
.prototype.toString
= function() {
25 return "Crosshair Plugin";
29 * @param {Dygraph} g Graph instance.
30 * @return {object.<string, function(ev)>} Mapping of event names to callbacks.
32 crosshair
.prototype.activate
= function(g
) {
33 g
.graphDiv
.appendChild(this.canvas_
);
37 deselect
: this.deselect
41 crosshair
.prototype.select
= function(e
) {
42 if (this.direction_
=== null) {
46 var width
= e
.dygraph
.width_
;
47 var height
= e
.dygraph
.height_
;
48 this.canvas_
.width
= width
;
49 this.canvas_
.height
= height
;
50 this.canvas_
.style
.width
= width
+ "px"; // for IE
51 this.canvas_
.style
.height
= height
+ "px"; // for IE
53 var ctx
= this.canvas_
.getContext("2d");
54 ctx
.clearRect(0, 0, width
, height
);
55 ctx
.strokeStyle
= "rgba(0, 0, 0,0.3)";
58 var canvasx
= Math
.floor(e
.dygraph
.selPoints_
[0].canvasx
) + 0.5; // crisper rendering
60 if (this.direction_
=== "vertical" || this.direction_
=== "both") {
61 ctx
.moveTo(canvasx
, 0);
62 ctx
.lineTo(canvasx
, height
);
65 if (this.direction_
=== "horizontal" || this.direction_
=== "both") {
66 for (var i
= 0; i
< e
.dygraph
.selPoints_
.length
; i
++) {
67 var canvasy
= Math
.floor(e
.dygraph
.selPoints_
[i
].canvasy
) + 0.5; // crisper rendering
68 ctx
.moveTo(0, canvasy
);
69 ctx
.lineTo(width
, canvasy
);
77 crosshair
.prototype.deselect
= function(e
) {
78 var ctx
= this.canvas_
.getContext("2d");
79 ctx
.clearRect(0, 0, this.canvas_
.width
, this.canvas_
.height
);
82 crosshair
.prototype.destroy
= function() {