I would like to make a simple d3 plugin but can't find information on how to do it.
It needs to be very simple:
s.append('text').text("Some Text").editable();
should just translate to
s.append('text').text("Some Text").attr('data-editable', true);
How would I do this?
D3 is already bundled with Grafana, and you can access it by importing the d3 package.
D3 uses SVG to create and modify the graphical elements of the visualization. Because SVG has a structured form, D3 can make stylistic and attribute changes to the shapes being drawn.
Download D3. js is an open-source library and the source code of the library is freely available on the web at https://d3js.org/ website. Visit the D3. js website and download the latest version of D3. js (d3.
js, or D3, stands for data-driven documents. It is an open-source JavaScript library that creates interactive data visualizations in a web browser using SVG, HTML, and CSS.
Had to go digging through the source but got it finally.
(function() {
d3.selection.prototype.editable = function() {
return this.attr('data-editable', true);
};
})();
Jsbin here
also note that if you also want the plugin to apply after you .enter()
you need to assign the d3.selection.enter.prototype
.
If (as in my case) you want your plugin available in both scenarios:
(function() {
d3.selection.prototype.editable = d3.selection.enter.prototype.editable = function() {
return this.attr('data-editable', true);
};
})();
Mike Bostock wrote Towards Reusable Charts http://bost.ocks.org/mike/chart/
I followed this pattern to make an extremely simple example d3 plugin, please see this block: http://bl.ocks.org/cpbotha/5073718 (and the source gist: https://gist.github.com/cpbotha/5073718).
According to Mike Bostock, reusable charts should be implemented as "closures with getter-setter methods". I did exactly that in my example above.
The answer of @Wex follows this pattern too, except that it doesn't demonstrate the closure idea, as the original question did not specify requiring any properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With