Logo

dev-resources.site

for different kinds of informations.

Display Chart in Express.js App using CanvasJS

Published at
12/2/2024
Categories
express
node
chart
javascript
Author
manoj_004d
Categories
4 categories in total
express
open
node
open
chart
open
javascript
open
Author
10 person written this
manoj_004d
open
Display Chart in Express.js App using CanvasJS

Express.js is a powerful tool for creating server-side applications, and CanvasJS makes it easy to visualize data with interactive charts. This tutorial will show you how to integrate the two for a seamless charting experience in your Express.js application.

Prerequisites

Before you start, make sure you have the following installed:

  • Node.js (LTS version recommended)
  • npm (comes with Node.js)
  • Basic knowledge of JavaScript and Express.js

Step 1: Set Up Your Express.js Application

  1. Create a new directory for your project and navigate to it:
mkdir express-canvasjs-chart
cd express-canvasjs-chart
Enter fullscreen mode Exit fullscreen mode
  1. Initialize a new Node.js project:
npm init -y
Enter fullscreen mode Exit fullscreen mode
  1. Install Express:
npm install express
Enter fullscreen mode Exit fullscreen mode
  1. Create a file named server.js and set up a basic Express server:
const express = require('express');
const app = express();
const port = 3000;

app.use(express.static('public'));

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/public/index.html');
});

app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Step 2: Add CanvasJS to Your Project

CanvasJS is a client-side library, so you need to include it in your front-end code.

  1. Download the CanvasJS library from CanvasJS's official website.
  2. Create a public folder in your project directory.
  3. Place the downloaded CanvasJS script (e.g., canvasjs.min.js) inside the public folder.

Step 3: Create an HTML File to Display the Chart

  1. Inside the public folder, create a file named index.html.
  2. Add the following content to index.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Display Chart in Express App using CanvasJS</title>
    <script src="canvasjs.min.js"></script>
</head>
<body>
    <div id="chartContainer" style="height: 370px; width: 100%;"></div>
    <script>
        window.onload = function () {
            var chart = new CanvasJS.Chart("chartContainer", {
                animationEnabled: true,
                theme: "light2",
                title: {
                    text: "Display Chart in Express.js App using CanvasJS"
                },
                axisY: {
                    title: "Revenue (in USD)"
                },
                data: [{
                    type: "column",
                    dataPoints: [
                        { label: "January", y: 3000 },
                        { label: "February", y: 5000 },
                        { label: "March", y: 7000 },
                        { label: "April", y: 4000 },
                        { label: "May", y: 6000 }
                    ]
                }]
            });
            chart.render();
        };
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 4: Run Your Application

  1. Start the server:
node server.js
Enter fullscreen mode Exit fullscreen mode
  1. Open your browser and navigate to http://localhost:3000.
  2. You should see a column chart displaying the sales data.

Step 5: Customize Your Chart

To customize your chart, you can modify the dataPoints array with dynamic data from an API or database, such as MySQL or MongoDB. For example, you could replace the static sales data with real-time sales fetched from a backend service. You can also experiment with different chart types like line, bar, or pie to suit your visualization needs. For detailed customization options, refer to the CanvasJS documentation.

Conclusion

By integrating CanvasJS with Express.js, you unlock the ability to present data in a visually compelling and interactive manner. Whether you're building dashboards or enhancing user interfaces, this combination provides a seamless way to make data insights more engaging and accessible. Additionally, you can display charts using data sourced from databases like MySQL or MongoDB, which we will explore in future articles.

chart Article's
30 articles in total
Favicon
Top 5 React Chart Libraries for 2025
Favicon
Create Stunning AI-Powered .NET MAUI Charts Using Natural Language
Favicon
Transform JSON into Stunning Charts: Auto-Generate Visuals with Syncfusion® .NET MAUI Toolkit 
Favicon
Create a Flutter 3D Column Chart to Showcase the Top 6 Renewable Energy-Consuming Countries
Favicon
Visualizing Skyscraper Data with .NET MAUI Doughnut Chart and Maps
Favicon
How to Animate SVG Path in Angular Charts?
Favicon
Building a Neumorphic UI with .NET MAUI Column Chart to Showcase Gen Z’s Favourite Social Media Platforms
Favicon
Create a .NET MAUI Spline Area Chart to Track Annual Gold Price Fluctuations Across Major Global Currencies
Favicon
Display Chart in Express.js App using CanvasJS
Favicon
Create a Flutter Column Chart to Visualize the World’s Largest Wind Power Producers
Favicon
Creating a Dynamic WPF Chart Dashboard to Showcase 2024 Women’s T20 World Cup Statistics
Favicon
Enhance Data Visualization with Markers in Angular Charts
Favicon
Implement Chart Export in Different Formats in Flutter
Favicon
Easily Create Dynamic Charts in Excel Using C#
Favicon
Visualize U.S. Gulf Coast Kerosene-Type Jet Fuel Prices with .NET MAUI Fast Line Chart
Favicon
billboard.js 3.14 release: viewBox resizing!
Favicon
Create a WPF Multi-Bar Chart to Visualize U.S. vs. China Superpower Status
Favicon
Easily Draw Custom Shapes in Flutter Cartesian Charts
Favicon
Create a WPF FastLine Chart to Analyze Global Population Trends by Age Group
Favicon
View 100+ Years of Economic Superpowers’ Exports with .NET MAUI Stacked Area Chart
Favicon
Create a .NET MAUI Bubble Chart to Visualize Market Cap Distribution by Country – GDP and Company Analysis
Favicon
Chart of the Week: Create a .NET MAUI Drill-Down Chart to View U.S. Workforce Distribution by Industry
Favicon
Effectively Visualize Data: Add Grids and Charts in JavaScript Pivot Field List
Favicon
Get 3 Proven Trading Signals in Real-Time: Automate Your Profits
Favicon
Create a Flutter Column Chart to View the Top 15 Water-Consuming Countries in the World
Favicon
Enhancing Angular Charts: Boosting Readability with Dynamic Colors
Favicon
React Synchronized Charts: The Perfect Tool to Compare Multiple Datasets
Favicon
Chart of the Week: Clean and Preprocess E-Commerce Website Traffic Data Using an AI-Powered Smart WPF Chart
Favicon
Chart of the Week: Creating a .NET MAUI Radar Chart to Visualize Wind Directions
Favicon
Chart of the Week: Visualizing Top 25 Largest Countries Using .NET MAUI Column Chart

Featured ones: