Add a chart to a Next.js Application
We assume that, you already know how to create a Next.js app. I kindly recommend, if you don’t, to go back.
We will work on new Next.js, so create a new one or follow this.
Having beautiful and interactive chart on your app would be very nice. As we know ApexChart for his interoperability, it is the best choice for this tutorial. This article won’t be long, so our article will have only three main parts:
- Installing ApexChart
- Loading ApexChart
- Usage
Install ApexChart
ApexChart is built for developers because of its integrating visualizations and 100+ samples chart ready to be used. You can install it via npm.
npm install react-apexcharts apexcharts
Load ApexChart into your Next.js app
Initially, ApexChart is designed for React, Vue, Angular and etc, the most near way to load it, is to install React.js ApexChart version because Next.js is a React.js framework. and in Next.js loading it as the older ways like import Chart from "react-apexcharts" can throws error. to avoid that, you must load or import it dynamically.
import dynamic from "next/dynamic";
const ApexCharts = dynamic( () => import("react-apexcharts"), {ssr: false})
dynamicis used to allow us to loadreact-apexchartswithout error. Often load React.js module throws some errors, I can garentee you, I tested it.ssr: false: ssr stands for server side rendering. It’s set tofalsewhich means that Next.js to do not load it on the server side.
Usage of ApexChart on Next.js app
It is very easy to use. You just have to get data in a list and populate your chart.
- data to be shown: let’s have useless data about 10 hours of temperature collection data.
const temperature = {
values: [21, 22, 30, 31, 27, 30, 23, 24, 18, 15],
times: ["2022-06-06T00:00:00:00.000", "2022-06-06T00:01:00:00.000",
"2022-06-06T00:02:00:00.000", "2022-06-06T00:03:00:00.000",
"2022-06-06T00:04:00:00.000", "2022-06-06T00:05:00:00.000",
"2022-06-06T00:06:00:00.000", "2022-06-06T00:07:00:00.000",
"2022-06-06T00:08:00:00.000", "2022-06-06T00:09:00:00.000"
]
}
- chart property: for apexchart, we need only series and options. series: which contains the values of temperature
var series = [{
name: 'Temperature',
type: 'area',
data: temperature.values
}]
options: which contains options of the chart including the timeseries
var options = {
chart: {
type: 'area',
zoom: {
type: "x",
enabled: true,
},
},
labels: temperature.times,
xaxis: {
type: 'datetime'
},
yaxis: [{
title: {
text: 'Temperature (°C)',
},
labels: {
formatter: function (val) {
return val
},
}
}
],
fill: {
type: 'gradient',
gradient: {
shadeIntensity: 1,
inverseColors: false,
opacityFrom: 0.7,
opacityTo: 0,
stops: [0, 90, 100]
}
}
}
Full code
// loading ApexChart
import dynamic from "next/dynamic";
const ApexCharts = dynamic( () => import("react-apexcharts"), {ssr: false})
// usage
export default function Home() {
//he Temperature values with timeseries
const temperature = {
values: [21, 22, 30, 31, 27, 30, 23, 24, 18, 15],
times: ["2022-06-06T00:00:00:00.000", "2022-06-06T00:01:00:00.000",
"2022-06-06T00:02:00:00.000", "2022-06-06T00:03:00:00.000",
"2022-06-06T00:04:00:00.000", "2022-06-06T00:05:00:00.000",
"2022-06-06T00:06:00:00.000", "2022-06-06T00:07:00:00.000",
"2022-06-06T00:08:00:00.000", "2022-06-06T00:09:00:00.000"
]
}
// series var to be load in the chart
var series = [{
name: 'Temperature',
type: 'area',
data: temperature.values
}]
// the options
var options = {
chart: {
type: 'area',
zoom: {
type: "x",
enabled: true,
},
},
labels: temperature.times,
xaxis: {
type: 'datetime'
},
yaxis: [{
title: {
text: 'Temperature (°C)',
},
labels: {
formatter: function (val) {
return val
},
}
}
],
fill: {
type: 'gradient',
gradient: {
shadeIntensity: 1,
inverseColors: false,
opacityFrom: 0.7,
opacityTo: 0,
stops: [0, 90, 100]
}
}
}
return (
<ApexCharts width="50%" options={options} series={series} type="area" />
)
}
The result
