✦Template Printable Ideas Insights

Chart.js Line Graph Examples

By

Chart.js is a powerful JavaScript library that helps us create attractive and interactive charts for our websites. Among its many chart types, the line graph is particularly useful for displaying changes over time or trends. Let's delve into some practical examples of line graphs using Chart.js to understand its functionalities better.

Before we begin, ensure you have included the Chart.js library in your project. You can link to its CDN in your HTML file or download and host the script locally. Once set up, you can start creating engaging line graphs with just a few lines of code.

Creating a Basic Line Graph

Let's start with a simple line graph example that plots a series of data points connected by straight line segments.

a line graph showing the number of people who have been reported on social media platforms
a line graph showing the number of people who have been reported on social media platforms
70+ Chart.js Examples & Free Chart Type Picker
70+ Chart.js Examples & Free Chart Type Picker
Chris Ference - x4-03651.jpg.pagespeed.ic.obQu7nxk6t.webp (703×795) – Savee
Chris Ference - x4-03651.jpg.pagespeed.ic.obQu7nxk6t.webp (703×795) – Savee

First, prepare your HTML canvas where the chart will be rendered:

<canvas id="lineChart"></canvas>

Then, in your JavaScript file, include the following code:

the graph shows that there are many different types of people
the graph shows that there are many different types of people
jQuery Line Chart Axis Marker
jQuery Line Chart Axis Marker
Vertical Charts in AnyChart JavaScript Charting Library
Vertical Charts in AnyChart JavaScript Charting Library
var ctx = document.getElementById('lineChart').getContext('2d');
var chart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [{
            label: 'Sales',
            data: [12, 10, 15, 19, 3, 5, 7],
            borderColor: 'rgba(75, 192, 192, 1)',
            backgroundColor: 'rgba(75, 192, 192, 0.2)',
            fill: false,
        }]
    },
    options: {}
});

This example creates a line graph with sales data for seven months.

Changing Line Colors and Styles

You can customize the line's color, width, and style using the 'borderColor', 'borderWidth', and 'borderDash' properties, respectively:

borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 3,
borderDash: [5, 3]

Experiment with different values to achieve your desired look.

Line Chart Template | Beautiful.ai
Line Chart Template | Beautiful.ai
a line graph with numbers and arrows
a line graph with numbers and arrows
Coloring Formatters in JS Stock Charts
Coloring Formatters in JS Stock Charts
A simple line graph chart with a legend.
A simple line graph chart with a legend.
Create Interactive Vertical Bar Charts in JavaScript (Interactive Example)
Create Interactive Vertical Bar Charts in JavaScript (Interactive Example)
Peaks Function on 3D Surface Plot Powered by AnyChart JS Charts
Peaks Function on 3D Surface Plot Powered by AnyChart JS Charts

Adding Multiple Data Sets

To display multiple lines on the same graph, add more datasets to the 'datasets' array:

datasets: [
    // First dataset
    {
        label: 'Sales',
        data: [12, 10, 15, 19, 3, 5, 7],
        borderColor: 'rgba(75, 192, 192, 1)',
        backgroundColor: 'rgba(75, 192, 192, 0.2)',
        fill: false,
    },
    // Second dataset
    {
        label: 'Expenses',
        data: [4, 7, 5, 3, 9, 6, 8],
        borderColor: 'rgba(255, 99, 132, 1)',
        backgroundColor: 'rgba(255, 99, 132, 0.2)',
        fill: false,
    }
]

Each dataset is represented by a separate line on the chart.

Interacting with Line Graphs

Chart.js offers a variety of interaction features that enhance user engagement with your line graphs.

chart/README.md at master · pavelkuligin/chart
chart/README.md at master · pavelkuligin/chart
Custom Axis Position in AnyChart JS Charts
Custom Axis Position in AnyChart JS Charts
the different types of graphs and numbers in each language, including one that shows data
the different types of graphs and numbers in each language, including one that shows data

Adding Tooltips

Tooltips enable users to hover over data points to view additional information. By default, Chart.js includes tooltips, but you can customize their appearance:

options: {
    tooltips: {
        backgroundColor: 'rgba(0, 0, 0, 0.8)',
        bodyFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
        bodyFontSize: 14,
        bodySpacing: 4,
        mode: 'index',
        intersect: false
    }
}

Play around with different properties to create visually appealing tooltips.

Enabling Hover and Interaction

You can make lines respond to mouse events by setting 'hover' and 'interaction' options:

options: {
    hover: {
        mode: 'nearest',
        intersect: true
    },
    interaction: {
        intersect: true
    }
}

These settings ensure that lines react to user interactions, such as changing color or thickness when hovered over.

In conclusion, Chart.js offers a wealth of possibilities for creating compelling line graphs. Whether you're representing sales trends, stock prices, or user growth, line graphs help visualize data effectively. Keep exploring Chart.js documentation to discover more features and chart types to enrich your web projects.