Last active
February 21, 2020 15:00
-
-
Save SergioGeeK7/565ac7da79378f80422f4eac14c090b2 to your computer and use it in GitHub Desktop.
Lazy load D3 library ReactJS
This file contains hidden or 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
// 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>; | |
} | |
}; |
This file contains hidden or 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
// 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