- React Cookbook
- Carlos Santana Roldan
- 545字
- 2021-07-16 17:49:40
C3.js chart – implementing componentDidUpdate
C3.js is a third-party library that makes it easy to generate D3-based charts by wrapping the code required to construct the entire chart. That means you don't need to write any D3 code anymore:
- componentDidUpdate: This React method is normally used to manage third-party UI elements and interact with the native UI. When we use a third-party library such as C3.js, we need to update the UI library with the new data. Install C3.js with npm:
npm install c3
- After we install C3.js, we need to add the C3 CSS file to our index.html. For now, we can use the CDN they provide:
<!-- Add this on the <head> tag -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.css" rel="stylesheet" />
File: public/index.html
- Now we can create our Chart component:
import React, { Component } from 'react';
import c3 from 'c3';
import './Chart.css';
class Chart extends Component {
componentDidMount() {
// When the component mounts the first time we update
// the chart.
this.updateChart();
}
componentDidUpdate() {
// When we receive a new prop then we update the chart again.
this.updateChart();
}
updateChart() {
c3.generate({
bindto: '#chart',
data: {
columns: this.props.columns,
type: this.props.chartType
}
});
}
render() {
return <div id="chart" />;
}
}
export default Chart;
File: src/components/Chart/Chart.js
- As you can see, we are executing the updateChart method on componentDidUpdate, which is executed every time the user receives a new prop from App.js. Let's add some logic that we need in our App.js file:
import React, { Component } from 'react';
import Chart from './Chart/Chart';
import Header from '../shared/components/layout/Header';
import Content from '../shared/components/layout/Content';
import Footer from '../shared/components/layout/Footer';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
chartType: 'line'
};
this.columns = [
['BTC', 3000, 6000, 10000, 15000, 13000, 11000],
['ETH', 2000, 3000, 5000, 4000, 3000, 940],
['XRP', 100, 200, 300, 500, 400, 300],
];
}
setBarChart = () => {
this.setState({
chartType: 'bar'
});
}
setLineChart = () => {
this.setState({
chartType: 'line'
});
}
render() {
return (
<div className="App">
<Header title="Charts" />
<Content>
<Chart
columns={this.columns}
chartType={this.state.chartType}
/>
<p>
Chart Type
<button onClick={this.setBarChart}>Bar</button>
<button onClick={this.setLineChart}>Line</button>
</p>
</Content>
<Footer />
</div>
);
}
}
export default App;
File: src/components/App.js
- Now let's add some basic styles to our Chart component:
p {
text-align: center;
}
button {
background: #159fff;
border: none;
color: white;
margin-left: 1em;
padding: 0.5em 2em;
text-transform: uppercase;
&:hover {
background: darken(#159fff, 5%);
}
}
#chart {
background: #fff;
width: 90%;
margin: 1em auto;
}
File: src/components/Chart.css
- In this case, we are creating some charts to display information about the most important cryptocurrencies today (BTC - Bitcoin, ETH - Ethereum and XRP - Ripple). This is how it should look:

This image gives you an idea of how the line charts look like
- We have two buttons to switch between chart types (bar or line). If we click on BAR, we should see this chart:

This image gives you an idea of how the bar charts look like.
- If you remove the componentDidUpdate method from the Chart component, then when you press the buttons the chart is not going to update. This is because every time we need to refresh the data, we need to call the c3.generate method, and in this case, React's componentDidUpdate method is very useful.
推薦閱讀
- 社交網(wǎng)絡(luò)對(duì)齊
- 通信網(wǎng)絡(luò)基礎(chǔ)與設(shè)備
- 物聯(lián)網(wǎng)安全(原書第2版)
- 異構(gòu)基因共表達(dá)網(wǎng)絡(luò)的分析方法
- 物聯(lián)網(wǎng)之魂:物聯(lián)網(wǎng)協(xié)議與物聯(lián)網(wǎng)操作系統(tǒng)
- 社交電商運(yùn)營(yíng)策略、技巧與實(shí)操
- Hands-On Chatbots and Conversational UI Development
- Proxmox High Availability
- Learning Karaf Cellar
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- 網(wǎng)絡(luò)互聯(lián)技術(shù)(實(shí)踐篇)
- 互聯(lián)網(wǎng)安全的40個(gè)智慧洞見:2015年中國(guó)互聯(lián)網(wǎng)安全大會(huì)文集
- 數(shù)字通信同步技術(shù)的MATLAB與FPGA實(shí)現(xiàn):Altera/Verilog版(第2版)
- 計(jì)算機(jī)網(wǎng)絡(luò)工程實(shí)用教程(第2版)
- 中國(guó)互聯(lián)網(wǎng)發(fā)展報(bào)告2018