1. Products
  2.   Spreadsheet
  3.   Node.js
  4.   XLSX-Chart
 
  

Free Node.js Library to Create Multiple Excel Charts

Top Open Source Node.js Library to Generate Simple and Advanced Charts in Excel Worksheet. It Creates Multiple Charts in One File, Charts for Web Downloads & Chart from a Template inside Node.js.

What is XLSX-Chart?

If you work in Node.js and need to generate Excel files with embedded charts, the open-source package XLSX-Chart is a very good option to consider. Many libraries allow you to generate Excel Worksheets, set cell values, etc., but fewer support charts/graphs embedded in the XLSX. XLSX-Chart fills that gap. The API is fairly straightforward — you specify chart type, titles, fields, data, and then write the file. It supports several types (column, bar, line, area, radar, scatter, pie) so you can choose what best fits your data. If you need more control (layout, styles), you can provide your own Excel template to base the chart generation on.

XLSX-Chart is a lightweight Node.js package developed by Dmitriy Samortsev that enables software developers to create Excel charts programmatically inside Node.js apps. It gives you methods to write directly to a file or generate binary data (for download via HTTP) which is handy in web applications. The library abstracts the complexity of working with Excel's Open XML format, providing an intuitive API for generating various chart types. Whether you're building business intelligence dashboards, automated reporting systems, or data analysis tools, XLSX-Chart offers a straightforward solution for Excel chart generation. Its intuitive API, support for multiple chart types, and flexible output options make it an excellent choice for developers building reporting systems, business intelligence tools, or data visualization applications.

Previous Next

Getting Started with XLSX-Chart

To install XLSX-Chart, you can use npm, the package manager for JavaScript. Please use the following commands for a successful installation.

Install XLSX-Chart via npm

npm install xlsx-chart

You can download the compiled shared library from GitHub repository and install it.

Creating a Simple Column Chart in Node.js

The open source XLSX-Chart library has included complete support for creating simple and advanced charts in Excel worksheet inside Node.js applications. The following code example creates a column chart comparing quarterly sales across four products. The titles array represents the data series (quarters), while fields represent the categories (products). The data object contains the actual values for each combination.

How to Create a Simple Excel Chart Showing Quarterly Sales inside Node.js?

var XLSXChart = require("xlsx-chart");
var xlsxChart = new XLSXChart();

var opts = {
    file: "sales-report.xlsx",
    chart: "column",
    titles: [
        "Q1 Sales",
        "Q2 Sales",
        "Q3 Sales"
    ],
    fields: [
        "Product A",
        "Product B",
        "Product C",
        "Product D"
    ],
    data: {
        "Q1 Sales": {
            "Product A": 5,
            "Product B": 10,
            "Product C": 15,
            "Product D": 20
        },
        "Q2 Sales": {
            "Product A": 10,
            "Product B": 5,
            "Product C": 20,
            "Product D": 15
        },
        "Q3 Sales": {
            "Product A": 20,
            "Product B": 15,
            "Product C": 10,
            "Product D": 5
        }
    }
};

xlsxChart.writeFile(opts, function(err) {
    if (err) {
        console.error("Error generating chart:", err);
    } else {
        console.log("Chart created successfully:", opts.file);
    }
});

Create Multiple Charts in One File in Node.js

One of XLSX-Chart's powerful features is generating multiple charts in a single workbook inside Node.js applications. The library enables software developers to create charts with custom values and multiple visualizations. The following code example shows how developers can create a comprehensive report with three different chart types. The charts array allows you to define multiple visualizations, each with its own configuration. The chartTitle property gives each chart a descriptive name. This is particularly useful for executive dashboards or detailed analytical reports.

How to Create Multiple Charts in One File inside Node.js Apps?

let fs = require("fs");
let XLSXChart = require("xlsx-chart");
let xlsxChart = new XLSXChart();

let opts = {
    charts: [{
        chart: "column",
        titles: ["2023", "2024"],
        fields: ["Jan", "Feb", "Mar", "Apr"],
        data: {
            "2023": {
                "Jan": 100,
                "Feb": 120,
                "Mar": 140,
                "Apr": 130
            },
            "2024": {
                "Jan": 110,
                "Feb": 135,
                "Mar": 150,
                "Apr": 145
            }
        },
        chartTitle: "Monthly Performance Comparison"
    }, {
        chart: "line",
        titles: ["Website Traffic", "Mobile Traffic"],
        fields: ["Week 1", "Week 2", "Week 3", "Week 4"],
        data: {
            "Website Traffic": {
                "Week 1": 5000,
                "Week 2": 5500,
                "Week 3": 6000,
                "Week 4": 6200
            },
            "Mobile Traffic": {
                "Week 1": 3000,
                "Week 2": 3500,
                "Week 3": 4200,
                "Week 4": 4800
            }
        },
        chartTitle: "Traffic Analysis"
    }, {
        chart: "pie",
        titles: ["Market Share"],
        fields: ["Company A", "Company B", "Company C", "Others"],
        data: {
            "Market Share": {
                "Company A": 35,
                "Company B": 28,
                "Company C": 22,
                "Others": 15
            }
        },
        chartTitle: "Market Distribution"
    }]
};

xlsxChart.generate(opts, function(err, data) {
    if (err) {
        console.error("Error:", err);
        return;
    }
    fs.writeFileSync("comprehensive-report.xlsx", data);
    console.log("Multi-chart report created successfully!");
});


require('fs').writeFileSync('inventory.xlsx', report);

Generating Charts for Web Downloads

The open source XLSX-Chart library makes it easy for software developers to generate charts that can be easily downloaded via a web applications. Here is a powerful example that demonstrates generating a bar chart within an Express.js route. The generate method returns the chart data as a buffer, which is then sent to the client with appropriate headers for downloading. This approach is ideal for dynamic report generation without creating temporary files on the server.

How to Create Excel Charts for Web Downloads inside Node.js?

var XLSXChart = require("xlsx-chart");
var xlsxChart = new XLSXChart();

// Express.js route example
app.get('/download-report', function(req, res) {
    var opts = {
        chart: "bar",
        titles: ["Revenue", "Expenses", "Profit"],
        fields: ["January", "February", "March", "April"],
        data: {
            "Revenue": {
                "January": 50000,
                "February": 55000,
                "March": 60000,
                "April": 58000
            },
            "Expenses": {
                "January": 30000,
                "February": 32000,
                "March": 35000,
                "April": 33000
            },
            "Profit": {
                "January": 20000,
                "February": 23000,
                "March": 25000,
                "April": 25000
            }
        }
    };

    xlsxChart.generate(opts, function(err, data) {
        if (err) {
            return res.status(500).send("Error generating chart");
        }
        
        res.set({
            "Content-Type": "application/vnd.ms-excel",
            "Content-Disposition": "attachment; filename=financial-report.xlsx",
            "Content-Length": data.length
        });
        res.status(200).send(data);
    });
});

Use Custom Templates to Create Excel Charts

For organizations with specific branding requirements, custom templates provide complete control for creating and managing Excel charts. The templatePath option points to a pre-formatted Excel file. XLSX-Chart will use this template as the foundation, applying your data while preserving custom formatting, colors, fonts, and layouts. Note that custom templates currently work best with column charts. The following code demonstrates how to achieve it.

How to Create Excel Charts via Custom Templates inside Node.js?

var XLSXChart = require("xlsx-chart");
var xlsxChart = new XLSXChart();

var opts = {
    file: "branded-report.xlsx",
    chart: "column",
    templatePath: __dirname + "/templates/company-template.xlsx",
    titles: ["Sales Team A", "Sales Team B"],
    fields: ["Q1", "Q2", "Q3", "Q4"],
    data: {
        "Sales Team A": {
            "Q1": 45000,
            "Q2": 52000,
            "Q3": 58000,
            "Q4": 61000
        },
        "Sales Team B": {
            "Q1": 42000,
            "Q2": 48000,
            "Q3": 55000,
            "Q4": 59000
        }
    }
};

xlsxChart.writeFile(opts, function(err) {
    if (err) {
        console.error("Template error:", err);
    } else {
        console.log("Branded report created!");
    }
});