Last active
June 11, 2017 20:05
-
-
Save jasongonzales23/c07390aac98b9387ac75c8155c45db4c to your computer and use it in GitHub Desktop.
Bad example of integrating ReactJS + d3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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