CanvasJS React Charts can be rendered from JSON data from an endpoint using AJAX. Below example shows rendering React Line Chart from JSON data alongside source code that you can try running locally.
/* App.js */ import React, { Component } from 'react'; import CanvasJSReact from './canvasjs.react'; var CanvasJS = CanvasJSReact.CanvasJS; var CanvasJSChart = CanvasJSReact.CanvasJSChart; var dataPoints =[]; class App extends Component { render() { const options = { theme: "light2", title: { text: "Nifty 50 Index" }, data: [{ type: "line", xValueFormatString: "MMM YYYY", yValueFormatString: "#,##0.00", dataPoints: dataPoints }] } 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> ); } componentDidMount(){ var chart = this.chart; fetch('https://canvasjs.com/data/gallery/react/nifty-stock-price.json') .then(function(response) { return response.json(); }) .then(function(data) { for (var i = 0; i < data.length; i++) { dataPoints.push({ x: new Date(data[i].x), y: data[i].y }); } chart.render(); }); } } module.exports = App;
Charts can also be plotted from CSV, XML or text file data.