Skip to content

Instantly share code, notes, and snippets.

@jasongonzales23
Last active June 11, 2017 20:05
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 jasongonzales23/c07390aac98b9387ac75c8155c45db4c to your computer and use it in GitHub Desktop.
Save jasongonzales23/c07390aac98b9387ac75c8155c45db4c to your computer and use it in GitHub Desktop.
Bad example of integrating ReactJS + d3.js
// inside yr React component
componentDidMount: function() {
var el = this.getDOMNode();
D3Chart.create(el, props);
},
componentDidUpdate: function(prevProps, prevState) {
var el = this.getDOMNode();
D3Chart.update(el, props);
},
render() {
return (
<div>
<h3>Some cool chart</h3>
<div className={"chart"}></div>
</div>
);
}
// then in yr d3 component
const create = (el, props, state) => {
this.update(el, props, state);
};
const update = (node, props, state) => {
const el = d3.select(node).select('.chart').node();
const svg = ChartUtils.createSVG(el, props);
const chart = svg.append("g").attr("class", "d3-chart")
.attr("transform", "translate(0," + props.margin.top +")");
const scales = ChartUtils.scales(props, domain);
const dateAccessor = ChartUtils.dateAccessor(scales.x);
const heartRateAccessor = ChartUtils.propertyAccessor(scales.y, "hrv");
const areaName = "other";
drawTicks(chart, props, scales, numYTicks);
drawArea(chart, props, scales, maxData, dateAccessor, heartRateAccessor, areaName);
drawPoints(chart, props, scales, props.data, dateAccessor, heartRateAccessor, "hrv");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment