draw
menthod.class MyComponentChart {
draw() {
// idempotent d3 stuff...
}
}
class MyComponentChart {
// Selected root element of the chart
selection(selector){
if(!selector) return this._selection;
this._selection = d3.select(selector);
return this;
}
// Display props like colors and arbitrary data
props(obj) {
// ...
}
// Chart data!
data(arr) {
// ...
}
draw() {
this.selecion()
.appendSelect('svg');
//...
}
}
import merge from 'lodash/merge';
class MyComponentChart {
defaultProps = {
stroke: '#333',
fill: 'orange',
}
props(obj){
// If no props set, return defaults
if(!obj) return this._props || this.defaultProps;
// If props set, merge with current props or defaults
this._props = merge(
this._props || this.defaultProps,
obj
);
return this;
}
draw() {
const { fill } = this.props();
this.selecion()
.appendSelect('svg');
.attr('background', fill)
//...
}
}
// In use...
const chart = new MyComponentChart();
chart.props({ fill: 'blue' });
chart.props();
// { fill: 'blue', stroke: '#333' }