React Dynamic Charts, also referred as Live Chart, are Charts that changes when you change the scope of data. Dynamic update is supported in all available chart types including Line, Column, Area, Pie, etc. Below example shows rendering Dynamic Chart in React. It also includes React source code that you can run locally.
/* App.js */ import React, { Component } from 'react'; import CanvasJSReact from '@canvasjs/react-charts'; //var CanvasJSReact = require('@canvasjs/react-charts'); var CanvasJS = CanvasJSReact.CanvasJS; var CanvasJSChart = CanvasJSReact.CanvasJSChart; var dps = [{x: 1, y: 10}, {x: 2, y: 13}, {x: 3, y: 18}, {x: 4, y: 20}, {x: 5, y: 17},{x: 6, y: 10}, {x: 7, y: 13}, {x: 8, y: 18}, {x: 9, y: 20}, {x: 10, y: 17}]; //dataPoints. var xVal = dps.length + 1; var yVal = 15; var updateInterval = 1000; class App extends Component { constructor() { super(); this.updateChart = this.updateChart.bind(this); } componentDidMount() { setInterval(this.updateChart, updateInterval); } updateChart() { yVal = yVal + Math.round(5 + Math.random() *(-5-5)); dps.push({x: xVal,y: yVal}); xVal++; if (dps.length > 10 ) { dps.shift(); } this.chart.render(); } render() { const options = { title :{ text: "Dynamic Line Chart" }, data: [{ type: "line", dataPoints : dps }] } return ( <div> <CanvasJSChart options = {options} onRef={ref => this.chart = ref} /> {/*You can get reference to the chart instance as shown above using onRef. This allows you to access all chart properties and methods*/} </div> ); } } export default App;
The thickness and the dash-type of the line can be modified using lineThickness and lineDashType properties. Other commonly used customization options include markerColor, lineColor, etc.
Note For step by step instructions, follow our React Integration Tutorial