Using React
This tutorial will show you every step you need to use amCharts 4 with React. Please refer to the section of this article that is relevant to how you are using React - with classes or hooks.
Creating a chart
Section titled “Creating a chart”First, create a new React project:
npx create-react-app my-projectcd my-projectYou can now run your project by using these commands:
npm installnpm startIt should automatically load up your project in a browser. If it doesn’t, then go to the http://localhost:3000/ URL.
Now it’s time to add in an amCharts 4 chart. Use this command to install amCharts 4:
npm install @amcharts/amcharts4Inside of the src/App.js file, add the following near the top:
import * as am4core from "@amcharts/amcharts4/core";import * as am4charts from "@amcharts/amcharts4/charts";import am4themes_animated from "@amcharts/amcharts4/themes/animated";
am4core.useTheme(am4themes_animated);And use this code for the App class:
class App extends Component { componentDidMount() { let chart = am4core.create("chartdiv", am4charts.XYChart);
// ... chart code goes here ...
this.chart = chart; }
componentWillUnmount() { if (this.chart) { this.chart.dispose(); } }
render() { return ( <div id="chartdiv" style={{ width: "100%", height: "500px" }}></div> ); }}It uses the following three methods:
componentDidMountis used to create the chart.componentWillUnmountis used to cleanup the chart when it’s done being used.renderis used to create the<div>which the chart will be displayed inside of.
Here is a full working example:
import React, { Component } from 'react';import logo from './logo.svg';import './App.css';import * as am4core from "@amcharts/amcharts4/core";import * as am4charts from "@amcharts/amcharts4/charts";import am4themes_animated from "@amcharts/amcharts4/themes/animated";
am4core.useTheme(am4themes_animated);
class App extends Component { componentDidMount() { let chart = am4core.create("chartdiv", am4charts.XYChart);
chart.paddingRight = 20;
let data = []; let visits = 10; for (let i = 1; i < 366; i++) { visits += Math.round((Math.random() < 0.5 ? 1 : -1) * Math.random() * 10); data.push({ date: new Date(2018, 0, i), name: "name" + i, value: visits }); }
chart.data = data;
let dateAxis = chart.xAxes.push(new am4charts.DateAxis()); dateAxis.renderer.grid.template.location = 0;
let valueAxis = chart.yAxes.push(new am4charts.ValueAxis()); valueAxis.tooltip.disabled = true; valueAxis.renderer.minWidth = 35;
let series = chart.series.push(new am4charts.LineSeries()); series.dataFields.dateX = "date"; series.dataFields.valueY = "value";
series.tooltipText = "{valueY.value}"; chart.cursor = new am4charts.XYCursor();
let scrollbarX = new am4charts.XYChartScrollbar(); scrollbarX.series.push(series); chart.scrollbarX = scrollbarX;
this.chart = chart; }
componentWillUnmount() { if (this.chart) { this.chart.dispose(); } }
render() { return ( <div id="chartdiv" style={{ width: "100%", height: "500px" }}></div> ); }}
export default App;Updating chart
Section titled “Updating chart”If you want to update the chart, you can use the componentDidUpdate method.
Here is an example:
class App extends Component { componentDidMount() { let chart = am4core.create("chartdiv", am4charts.XYChart); chart.paddingRight = this.props.paddingRight; // ... this.chart = chart; } componentDidUpdate(oldProps) { if (oldProps.paddingRight !== this.props.paddingRight) { this.chart.paddingRight = this.props.paddingRight; } }}When you create the chart you can now pass in a paddingRight property, like this:
<App paddingRight={20} />When your app’s state changes, it will call the componentDidUpdate method, which then checks whether the paddingRight property has changed, and it then changes the chart.
Using with hooks
Section titled “Using with hooks”Using React hooks, creating the chart would look like this:
function App(props) { const chart = useRef(null);
useLayoutEffect(() => { let x = am4core.create("chartdiv", am4charts.XYChart);
// ... chart.current = x;
return () => { x.dispose(); }; }, []);
return ( <div id="chartdiv" style={{ width: "100%", height: "500px" }}></div> );}Here’s a complete example from this article:
import React, { useRef, useLayoutEffect } from 'react';import logo from './logo.svg';import './App.css';import * as am4core from "@amcharts/amcharts4/core";import * as am4charts from "@amcharts/amcharts4/charts";import am4themes_animated from "@amcharts/amcharts4/themes/animated";
am4core.useTheme(am4themes_animated);
function App(props) { const chart = useRef(null);
useLayoutEffect(() => { let x = am4core.create("chartdiv", am4charts.XYChart);
x.paddingRight = 20;
let data = []; let visits = 10;
for (let i = 1; i < 366; i++) { visits += Math.round((Math.random() < 0.5 ? 1 : -1) * Math.random() * 10); data.push({ date: new Date(2018, 0, i), name: "name" + i, value: visits }); }
x.data = data;
let dateAxis = x.xAxes.push(new am4charts.DateAxis()); dateAxis.renderer.grid.template.location = 0;
let valueAxis = x.yAxes.push(new am4charts.ValueAxis()); valueAxis.tooltip.disabled = true; valueAxis.renderer.minWidth = 35;
let series = x.series.push(new am4charts.LineSeries()); series.dataFields.dateX = "date"; series.dataFields.valueY = "value"; series.tooltipText = "{valueY.value}"; x.cursor = new am4charts.XYCursor();
let scrollbarX = new am4charts.XYChartScrollbar(); scrollbarX.series.push(series); x.scrollbarX = scrollbarX;
chart.current = x;
return () => { x.dispose(); }; }, []);
return ( <div id="chartdiv" style={{ width: "100%", height: "500px" }}></div> );}export default App;Updating the chart object looks like this:
function App(props) { const chart = useRef(null);
useLayoutEffect(() => { let x = am4core.create("chartdiv", am4charts.XYChart);
// ...
chart.current = x;
return () => { x.dispose(); }; }, []);
// When the paddingRight prop changes it will update the chart useLayoutEffect(() => { chart.current.paddingRight = props.paddingRight; }, [props.paddingRight]);
return ( <div id="chartdiv" style={{ width: "100%", height: "500px" }}></div> );}