简体   繁体   中英

React line chart using Chart.js

How can I implement dynamic chart in React using Chart.js. I have data coming from API every 10min and I want to populate chart with that data. Here is code for getting data

fetch('http://localhost:8080/data')
    .then(res => res.json())
    .then(json => this.setState({chartData: [...this.state.chartData, json[0]]})); 

Here is code for chart data

const data = {
          labels: this.props.data.date
          datasets: [
            {
              label: 'Data1',
              data: this.props.data.entry
            }

Passing props to Chart component

<Chart data={this.props.chartData} />

Line chart in component

<Line data={data}  />

There is a React wrapper around ChartJS available from the React team. It supports line charts. This may be simpler than building your own, or at least use it for inspiration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM