How to implement a column chart in React
Today we’ll show you how to implement a column chart in React using Highcharts. There are several npm react packages available to create a chart in a web application but we will use the Highcharts JS library to draw a chart in simple steps.
Let’s take a sample example to create a column chart using Highcharts in ReactJS with minimum configuration.
Implement a column chart in React
1. Create a react application
Let’s create a startup react application using the create-react-app
package. Run the following command to create a react app.
1 | npx create-react-app column-chart-react |
2. Add npm dependency
As discussed, we will use the Highcharts to implement a column chart. Run the following command to add the dependency in react app.
1 | npm i highcharts |
3. Create a column chart
Now, we will use the chart
method of the highcharts with minimum configuration to create a column chart. We will use the following options to create a chart.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | { chart: { type: 'column' }, // type of the chart title: { text: 'Column Chart Title' }, // title of the chart subtitle: { text: 'Lorem Ipsum is simply dummy text' }, // subtitle of the chart xAxis: { categories: [ 'X1', 'X2', 'X3', 'X4', 'X5' ], // the categories of the X Axis crosshair: true }, yAxis: { min: 0, // minimum value of the Y Axis title: { text: 'Y Axis Title' } // the title of the Y Axis }, tooltip: { headerFormat: '<span style="font-size:10px">{point.key}</span><table>', pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' + '<td style="padding:0"><b>{point.y}</b></td></tr>', footerFormat: '</table>', shared: true, useHTML: true }, // tooltip appears when hovering over a point credits: { enabled: false }, series: dataSource // set of the data } |
Check out this link for more information about the options.
Let’s assume that we are receiving the data source from the API response. To mimic the scenario, we will use the setTimeout
method to set the dataSource
after 2 seconds.
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import React, { useEffect, useRef, useState } from 'react'; import Highcharts from 'highcharts'; function App() { const refContainer = useRef(null); const [dataSource, setDataSource] = useState([]); useEffect(() => { const chart = Highcharts.chart(refContainer.current, { chart: { type: 'column' }, // type of the chart title: { text: 'Column Chart Title' }, // title of the chart subtitle: { text: 'Lorem Ipsum is simply dummy text' }, // subtitle of the chart xAxis: { categories: [ 'X1', 'X2', 'X3', 'X4', 'X5' ], // the categories of the X Axis crosshair: true }, yAxis: { min: 0, // minimum value of the Y Axis title: { text: 'Y Axis Title' } // the title of the Y Axis }, tooltip: { headerFormat: '<span style="font-size:10px">{point.key}</span><table>', pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' + '<td style="padding:0"><b>{point.y}</b></td></tr>', footerFormat: '</table>', shared: true, useHTML: true }, // tooltip appears when hovering over a point credits: { enabled: false }, series: dataSource // set of the data }); if (dataSource.length > 0) { chart.hideLoading(); } else { chart.showLoading(); } }, [dataSource]); useEffect(() => { setTimeout(() => { setDataSource([{ name: 'Japan', data: [50, 72, 88, 92, 34] }, { name: 'Germany', data: [84, 79, 99, 94, 66] }, { name: 'London', data: [49, 39, 47, 40, 42] }, { name: 'Canada', data: [43, 34, 77, 35, 53] }]); }, 2000); }, []); return ( <div className="App"> <h3>Column chart in React - <a href="http://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a></h3> <div ref={refContainer} /> </div> ); } export default App; |
Here, we used the refs to load the chart in div element.
4. Output
Run the application and check the output in the browser.
That’s it for today.
Thank you for reading. Happy Coding..!!