Creating Sparkline Charts with CanvasJS

Vishwas R
2 min readSep 13, 2023

--

Sparkline charts are a powerful visualization tool that allows you to display trends and variations in data within a compact space. These miniature charts are great for presenting information at a glance. In this article, we’ll explore how to create sparkline charts using CanvasJS, a JavaScript charting library.

What is a Sparkline Chart?

A sparkline is a small, simple chart that provides a concise visual representation of data trends. Unlike traditional charts, sparklines are designed to be embedded within text or tables, making them ideal for displaying data in a confined space. They are often used to show trends, variations, or comparisons in data without the need for a full-sized chart.

Getting Started with CanvasJS

To create sparkline charts with CanvasJS, you’ll need to include the CanvasJS library in your HTML file. You can easily do this by adding the following script tag to your HTML’s <head> section:

<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>

Once you have CanvasJS included in your project, you can start building your sparkline chart. You can use a <div> element to hold your sparkline chart. Give it an id attribute for reference:

<div id="sparklineChart" style="width: 300px; height: 100px;"></div>

You need to remove all the axis elements like axis line, axis labels, grids, ticks to make it miniature chart.

var chart = new CanvasJS.Chart("sparklineChart", {

axisX: {
labelFontSize: 0, // Remove labels
tickLength: 0, // Remove ticks
lineThickness: 0, // Remove axis line
gridThickness: 0 // Remove grid lines
},
axisY: {
labelFontSize: 0,
tickLength: 0,
lineThickness: 0,
gridThickness: 0
},
data: [{
type: "line",
dataPoints: [
{ y: 10 },
{ y: 15 },
{ y: 7 },
{ y: 12 },
{ y: 9 }
]
}]
});

chart.render();

Below is an example of using sparkline charts in a table.

Customize Your Sparkline Chart

CanvasJS offers a wide range of customization options to tailor your sparkline chart to suit your needs. You can personalize colors, fonts, tooltips, and more to make your chart visually appealing and informative.

--

--

Vishwas R

<< Engineer || Software Developer || Techie || Cricketer || Traveler || Blogger >>