Skip to content

Add custom hide plugin #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions inst/plugins/hide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @fileoverview Plug-in for hiding/showing time-series.
*/
Dygraph.Plugins.Hide = (function() {

"use strict";

/**
* Create a new instance.
*
* @constructor
*/
var hide = function() {
this.showSeriesArray = [];
};

hide.prototype.toString = function() {
return 'hide Plugin';
};

/**
* This is called during the dygraph constructor, after options have been set
* but before the data is available.
* @param {Dygraph} g Graph instance.
* @return {object.<string, function(ev)>} Mapping of event names to callbacks.
*/
hide.prototype.activate = function(g) {
return {
willDrawChart: this.willDrawChart
};
};

hide.prototype.willDrawChart = function(e) {
var g = e.dygraph;
var area = g.plotter_.area;
var parent = g.graphDiv;

var visibility = g.visibility();
for (var i = 0; i < visibility.length; i++) {
var offset = 20*i+4;
var cb = document.createElement('input');
cb.type = 'checkbox';
cb.checked = true;
cb.id = i;
cb.style.position = 'absolute';
cb.style.top = (area.y + 4) + 'px';
cb.style.left = (area.x + offset) + 'px';
cb.style.zIndex = 11;
this.showSeriesArray.push(true);

var self = this;

g.addAndTrackEvent(cb, 'click', function(e) {
var targetId = e.target.id;
self.showSeriesArray[targetId] = !self.showSeriesArray[targetId];
g.setVisibility(targetId, self.showSeriesArray[targetId]);
});

parent.insertBefore(cb, parent.firstChild);
}
};

return hide;

})();