|
| 1 | +/** |
| 2 | + * @fileoverview Plug-in for hiding/showing time-series. |
| 3 | + */ |
| 4 | +Dygraph.Plugins.Hide = (function() { |
| 5 | + |
| 6 | + "use strict"; |
| 7 | + |
| 8 | + /** |
| 9 | + * Create a new instance. |
| 10 | + * |
| 11 | + * @constructor |
| 12 | + */ |
| 13 | + var hide = function() { |
| 14 | + this.showSeriesArray = []; |
| 15 | + }; |
| 16 | + |
| 17 | + hide.prototype.toString = function() { |
| 18 | + return 'hide Plugin'; |
| 19 | + }; |
| 20 | + |
| 21 | +/** |
| 22 | + * This is called during the dygraph constructor, after options have been set |
| 23 | + * but before the data is available. |
| 24 | + * @param {Dygraph} g Graph instance. |
| 25 | + * @return {object.<string, function(ev)>} Mapping of event names to callbacks. |
| 26 | + */ |
| 27 | + hide.prototype.activate = function(g) { |
| 28 | + return { |
| 29 | + willDrawChart: this.willDrawChart |
| 30 | + }; |
| 31 | + }; |
| 32 | + |
| 33 | + hide.prototype.willDrawChart = function(e) { |
| 34 | + var g = e.dygraph; |
| 35 | + var area = g.plotter_.area; |
| 36 | + var parent = g.graphDiv; |
| 37 | + |
| 38 | + var visibility = g.visibility(); |
| 39 | + for (var i = 0; i < visibility.length; i++) { |
| 40 | + var offset = 20*i+4; |
| 41 | + var cb = document.createElement('input'); |
| 42 | + cb.type = 'checkbox'; |
| 43 | + cb.checked = true; |
| 44 | + cb.id = i; |
| 45 | + cb.style.position = 'absolute'; |
| 46 | + cb.style.top = (area.y + 4) + 'px'; |
| 47 | + cb.style.left = (area.x + offset) + 'px'; |
| 48 | + cb.style.zIndex = 11; |
| 49 | + this.showSeriesArray.push(true); |
| 50 | + |
| 51 | + var self = this; |
| 52 | + |
| 53 | + g.addAndTrackEvent(cb, 'click', function(e) { |
| 54 | + var targetId = e.target.id; |
| 55 | + self.showSeriesArray[targetId] = !self.showSeriesArray[targetId]; |
| 56 | + g.setVisibility(targetId, self.showSeriesArray[targetId]); |
| 57 | + }); |
| 58 | + |
| 59 | + parent.insertBefore(cb, parent.firstChild); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + return hide; |
| 64 | + |
| 65 | +})(); |
0 commit comments