Skip to content

Instantly share code, notes, and snippets.

@SergioGeeK7
Last active February 21, 2020 15:00
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 SergioGeeK7/565ac7da79378f80422f4eac14c090b2 to your computer and use it in GitHub Desktop.
Save SergioGeeK7/565ac7da79378f80422f4eac14c090b2 to your computer and use it in GitHub Desktop.
Lazy load D3 library ReactJS
// chart.js
import React from "react";
import * as d3 from "d3"; // big dependency
const data = [30, 86, 168, 281, 303, 365];
export default class D3Chart extends React.PureComponent{
constructor(props) {
super(props);
this.canvas = React.createRef();
}
componentDidMount() {
this.drawBarChart(data);
};
drawBarChart(data) {
d3.select(this.canvas.current)
.selectAll("div")
.data(data)
.enter()
.append("div")
.style("width", function(d) { return d + "px"; })
.style("background-color", "steelblue")
.style("margin", "1px")
.text(function(d) { return d; });
};
render(){
return <div ref={this.canvas} className="canvas"></div>;
}
};
// index.js
import D3Chart from "../components/d3chart"
// Further down in the render method
<D3Chart/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment