Skip to content

Instantly share code, notes, and snippets.

@biovisualize
Last active August 18, 2017 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biovisualize/09546a25f1a89532ba8c5f883e9ff6f2 to your computer and use it in GitHub Desktop.
Save biovisualize/09546a25f1a89532ba8c5f883e9ff6f2 to your computer and use it in GitHub Desktop.
Simple Britecharts interactor plugin
define(function(require) {
const d3Selection = require('d3-selection');
/**
* Toggles selection on clickable elements. Will set classes "selected" and "dimmed".
* @param {Number} selector A valid selector of the elements to toggle
* @example
* barChart.on('customClick', britecharts.interactors.multiToggle('rect.bar'))
*
* @return {void}
*/
function multiToggle(selector) {
return function() {
const that = this;
let hasSelection = false;
const selection = d3Selection.select(this.farthestViewportElement)
.selectAll(selector);
selection.classed('selected', function(){
const isSelected = this.classList.contains('selected');
const shouldBeSelected = this === that ? !isSelected : isSelected;
hasSelection = hasSelection || shouldBeSelected;
return shouldBeSelected;
});
selection.classed('dimmed', function(){
return hasSelection && !this.classList.contains('selected');
});
}
}
return {
multiToggle
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment