Published on August 21, 2024By DeveloperBreeze

Tutorial: Getting Started with ApexCharts

Introduction

ApexCharts is a modern charting library that helps developers create interactive and responsive charts in web applications. It supports a wide range of chart types, including line charts, bar charts, pie charts, and more. ApexCharts is easy to integrate with popular frameworks like React, Vue, Angular, and even plain JavaScript, making it a versatile tool for data visualization.

In this tutorial, we’ll cover how to set up ApexCharts, create various types of charts, customize them, and integrate them into a web project.

Prerequisites

Before starting, ensure you have the following:

  • Basic knowledge of HTML, CSS, and JavaScript.

  • A text editor or an integrated development environment (IDE) like Visual Studio Code.

  • A web browser for testing.

Step 1: Setting Up ApexCharts

Method 1: Using a CDN

The easiest way to start using ApexCharts is by including it directly in your HTML file using a CDN.

    • Create an HTML file (e.g., index.html) and add the following code:

<!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>ApexCharts Example</title>
       <!-- Include ApexCharts CDN -->
       <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
   </head>
   <body>
       <div id="chart"></div>
       <script src="script.js"></script>
   </body>
   </html>
   

Method 2: Installing via npm (for frameworks)

If you’re working with a JavaScript framework like React or Vue, you can install ApexCharts via npm.

    • Install ApexCharts:

npm install apexcharts
   

    • Import ApexCharts into your JavaScript file:

import ApexCharts from 'apexcharts';
   

Step 2: Creating Your First Chart

Let’s start by creating a basic line chart.

    • Create a JavaScript file (e.g., script.js) and add the following code:

var options = {
       chart: {
           type: 'line'
       },
       series: [{
           name: 'Sales',
           data: [10, 20, 15, 30, 25, 40, 35]
       }],
       xaxis: {
           categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
       }
   }

   var chart = new ApexCharts(document.querySelector("#chart"), options);

   chart.render();
   

    • Run the HTML file (index.html) in your browser, and you should see a simple line chart.

Step 3: Customizing the Chart

ApexCharts offers a wide range of customization options, from changing colors to adding tooltips and legends.

Customizing Colors

You can customize the colors of the chart using the colors option.

var options = {
    chart: {
        type: 'line'
    },
    series: [{
        name: 'Sales',
        data: [10, 20, 15, 30, 25, 40, 35]
    }],
    xaxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
    },
    colors: ['#FF5733'] // Custom line color
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

Adding a Title

You can add a title to your chart for better context.

var options = {
    chart: {
        type: 'line'
    },
    series: [{
        name: 'Sales',
        data: [10, 20, 15, 30, 25, 40, 35]
    }],
    xaxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
    },
    title: {
        text: 'Monthly Sales Data',
        align: 'center'
    }
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

Adding Tooltips and Legends

Tooltips and legends can help users understand the data more easily.

var options = {
    chart: {
        type: 'line'
    },
    series: [{
        name: 'Sales',
        data: [10, 20, 15, 30, 25, 40, 35]
    }],
    xaxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
    },
    title: {
        text: 'Monthly Sales Data',
        align: 'center'
    },
    tooltip: {
        enabled: true,
        theme: 'dark'
    },
    legend: {
        position: 'top',
        horizontalAlign: 'right'
    }
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

Step 4: Creating Different Types of Charts

ApexCharts supports various chart types. Let’s explore some of them.

Bar Chart

var options = {
    chart: {
        type: 'bar'
    },
    series: [{
        name: 'Revenue',
        data: [30, 40, 45, 50, 49, 60, 70, 91]
    }],
    xaxis: {
        categories: ['2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022']
    },
    title: {
        text: 'Annual Revenue',
        align: 'center'
    }
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

Pie Chart

var options = {
    chart: {
        type: 'pie'
    },
    series: [44, 55, 13, 43, 22],
    labels: ['Apples', 'Oranges', 'Bananas', 'Berries', 'Mangoes'],
    title: {
        text: 'Fruit Consumption',
        align: 'center'
    }
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

Area Chart

var options = {
    chart: {
        type: 'area'
    },
    series: [{
        name: 'Visitors',
        data: [31, 40, 28, 51, 42, 109, 100]
    }],
    xaxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
    },
    title: {
        text: 'Website Visitors',
        align: 'center'
    }
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

Step 5: Responsive Charts

ApexCharts are responsive by default, but you can further customize how charts behave on different screen sizes.

var options = {
    chart: {
        type: 'line',
        height: '100%',
        width: '100%'
    },
    series: [{
        name: 'Sales',
        data: [10, 20, 15, 30, 25, 40, 35]
    }],
    xaxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
    },
    responsive: [{
        breakpoint: 480,
        options: {
            chart: {
                width: '100%'
            },
            legend: {
                position: 'bottom'
            }
        }
    }]
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

Step 6: Exporting and Downloading Charts

ApexCharts provides options to export your charts as images or CSV files, making it easy to share your data.

var options = {
    chart: {
        type: 'line',
        toolbar: {
            show: true,
            tools: {
                download: true
            }
        }
    },
    series: [{
        name: 'Sales',
        data: [10, 20, 15, 30, 25, 40, 35]
    }],
    xaxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
    },
    title: {
        text: 'Sales Data with Export Option',
        align: 'center'
    }
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

Step 7: Integrating ApexCharts with Frameworks

ApexCharts can be easily integrated with popular JavaScript frameworks like React, Vue, and Angular.

React Example

    • Install ApexCharts and the React wrapper:

npm install apexcharts react-apexcharts
   

    • Create a chart component in your React project:

import React from 'react';
   import Chart from 'react-apexcharts';

   class MyChart extends React.Component {
       constructor(props) {
           super(props);

           this.state = {
               options: {
                   chart: {
                       type: 'line'
                   },
                   xaxis: {
                       categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May

', 'Jun', 'Jul']
                   }
               },
               series: [{
                   name: 'Sales',
                   data: [10, 20, 15, 30, 25, 40, 35]
               }]
           }
       }

       render() {
           return (
               <div>
                   <Chart options={this.state.options} series={this.state.series} type="line" />
               </div>
           );
       }
   }

   export default MyChart;
   

    • Use the component in your application:

import React from 'react';
   import MyChart from './MyChart';

   function App() {
       return (
           <div className="App">
               <MyChart />
           </div>
       );
   }

   export default App;
   

Conclusion

ApexCharts is a powerful and flexible charting library that makes it easy to create beautiful and responsive charts. Whether you're working on a simple web page or a complex application, ApexCharts provides the tools you need to visualize data effectively.

This tutorial covered the basics of setting up ApexCharts, creating various chart types, customizing charts, and integrating them into web projects. With these skills, you can now create and customize charts to fit your specific needs.

Comments

Please log in to leave a comment.

Continue Reading:

Tailwind Browser Mockup

Published on January 26, 2024

Simple and Clean Tailwind Buttons

Published on January 26, 2024

Tailwind Buttons with Arrow Icon

Published on January 26, 2024

AI Interactive Chat Interface

Published on January 26, 2024

AI Chat Interface with Online Assistant

Published on January 26, 2024

CSS Grid and Flexbox: Mastering Modern Layouts

Published on August 03, 2024

csshtml

Creating a Simple REST API with Flask

Published on August 03, 2024

python

Building a Real-Time Chat Application with WebSockets in Node.js

Published on August 03, 2024

javascriptcsshtml

JavaScript Code Snippet: Fetch and Display Data from an API

Published on August 04, 2024

javascriptjson