Heatmap Color in HTML, CSS & JavaScript

Sunday, August 29, 2024

Currency Converter Preview

A Heatmap Color is a visual representation of data where individual values ​​are represented by colors. In this tutorial, we will create a simple heatmap consisting of a grid (table) where each cell is colored based on a specific numeric value. We will use HTML for the structure, CSS for styling, and JavaScript for logic and data manipulation.

In this blog post, I will walk you through creating a simple heatmap using HTML, CSS, and JavaScript. We will create a heatmap view that visualizes numeric data using color to indicate intensity or value. The heatmap we will create includes key features such as:

  • Navigation Bar: Sets the main view of the page with titles.
  • Grid Layout: Use CSS Grid to neatly arrange the heatmap boxes.
  • Dynamic Coloring: Applying different colors based on numeric values ​​to provide clear visualization.
  • Responsiveness: Ensures the heatmap displays well on various screen sizes.
By following these steps, you will learn how to structure grid elements with CSS, dynamically populate data using JavaScript, and present numeric data in an attractive visual way. Get your code ready and let’s start creating an effective and informative heatmap!

As you work on this project, you’ll learn how to structure and style websites effectively to ensure they look great on all devices.

Key Features:

  1. HTML Structure: Lays out the basic elements for the heatmap, including the main container that holds all the data boxes.
  2. Grid Layout with CSS: Use CSS Grid to neatly layout the heatmap boxes in a grid format.
  3. Dynamic Coloring: Apply different colors to each box based on numeric values ​​using JavaScript, to provide clear and informative visualizations.
  4. Responsiveness: Adjust the heatmap display to maintain proper proportions across screen sizes, using CSS properties such as max-width and margin.
  5. DOM Manipulation with JavaScript: Dynamically create and add div elements to the heatmap container, and set styles and content based on data.
  6. Data Visualization: Using colors to represent data values, facilitating visual understanding and analysis of data.

With these features, you will have effective and attractive heatmaps, as well as improve your skills in using HTML, CSS, and JavaScript to build data visualizations.

Why make a heatmap color in HTML, CSS & JavaScript?

    By building this heatmap project using HTML, CSS, and JavaScript, you will gain the following skills:

    • HTML & Data Structures: Understand how to structure HTML elements to create the basic structure of a heatmap.
    • CSS Grid Layout: Master the CSS Grid technique to arrange heatmap boxes in a neat and responsive grid layout.
    • Dynamic Coloring with CSS: Learn how to apply dynamic, responsive styling to grid elements to reflect data values.
    • DOM Manipulation with JavaScript: Develop skills in using JavaScript to dynamically create elements, style them, and add data to heatmaps.
    • Data Visualization: Improve skills in creating effective data visualizations through the use of color to indicate intensity or value in a heatmap.

    This project will provide a solid foundation in responsive web design and front-end development, paving the way for more complex web projects in the future. Good luck and may the skills you gain be useful in developing future projects!

Steps to make a heatmap color using HTML, CSS & JavaScript

    Here are the steps to create a Color Heatmap using HTML, CSS, and JavaScript:

    1. Create HTML Structure

    Create an HTML file as the basic structure of the heatmap. Use div elements to contain the heatmap boxes that will later be filled by JavaScript.

    • Create a div element with the id heatmap as the container of the heatmap.
    • Add an h1 element as a title.
index.html
                            
<!DOCTYPE html>
<!-- Coding By CodingIndo - https://codingindo.vercel.app/ -->
<html lang="en">
<head>
    <!-- Meta tag for character encoding -->
    <meta charset="UTF-8">
    <!-- Meta tag to make the viewport responsive on mobile devices -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Page title -->
    <title>Heatmap Color</title>
    <!-- Link to CSS file for styling -->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <!-- Page title with centered text -->
    <h1 style="text-align: center;">Heatmap Color</h1>
    <!-- Div to display heatmap color -->
    <div id="heatmap" class="heatmap"></div>
    <!-- Link to JavaScript file for functionality -->
    <script src="script.js"></script>
</body>
</html>
                            
                        

Explanation

Number Line of Code Explanation

    2. Add CSS for Styling

    Add styling using CSS to create a grid on the heatmap and adjust its appearance.

    • Use CSS Grid to create a grid-like layout.
    • Set the color and size of the box and the text inside the box.
style.css
                            
/* Sets the font for the entire body */
body {
    font-family: Arial, sans-serif;
}

/* Sets the grid display for the heatmap */
.heatmap {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 2px;
    max-width: 300px;
    margin: 0 auto;
}

/* Sets the appearance of each div in the heatmap */
.heatmap div {
    padding: 20px;
    text-align: center;
    color: white;
    font-weight: bold;
}
                            
                        

Explanation

Number Line of Code Explanation

    3. Add JavaScript to Create Heatmap

    Use JavaScript to add box elements to the heatmap based on the given data values.

    • Create a 2D array containing numeric data.
    • Create a function getColor() that determines the color based on a numeric value.
    • Loop the data to create boxes with the appropriate values ​​and colors.
script.js
                            
// Get element with id 'heatmap'
const heatmapContainer = document.getElementById('heatmap');

// Data for heatmap
const data = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20],
    [21, 22, 23, 24, 25]
];

// Function to get color based on value
function getColor(value) {
    if (value <= 5) return 'rgb(198, 235, 255)';
    if (value <= 10) return 'rgb(132, 196, 255)';
    if (value <= 15) return 'rgb(66, 157, 255)';
    if (value <= 20) return 'rgb(0, 118, 255)';
    return 'rgb(0, 78, 170)';
}

// Iterate through each row of data
data.forEach(row => {
    // Iterate through each value in the row
    row.forEach(value => {
        // Create a div element for each value
        const cell = document.createElement('div');
        // Sets the background color based on the value
        cell.style.backgroundColor = getColor(value);
        // Add value text into div element
        cell.textContent = value;
        // Add a div element to the heatmap container
        heatmapContainer.appendChild(cell);
    });
});
                            
                        

Explanation

Number Line of Code Explanation

    4. Run Your Project

    Once all the code is ready, save the file with the .html extension. For example, save it as heatmap.html. Open the file in a browser to see the heatmap result.

    The final result will display a 5x5 grid with each box a different color based on the value inside it.

    Additional Notes

    1. Data Readability:
      • Make sure that the colors used in the heatmap contrast sufficiently with the text to ensure good readability.
      • Consider providing a legend or color guide to help users understand the meaning of the various colors.
    2. Responsiveness:
      • Test the heatmap display on different devices and screen sizes to ensure that the layout and box sizes remain proportional and easy to read.
      • Use media queries if necessary to adjust CSS styles on devices with different screen sizes.
    3. Performance Optimization: If the data being displayed is very large, consider optimizing performance by reducing the number of DOM elements created or using lazy loading techniques.
    4. Accessibility: Add ARIA attributes or alternative text if necessary to ensure that the heatmap is accessible to users with special accessibility needs.
    5. Testing: Perform thorough testing on the heatmap to ensure that all functions are working properly and there are no bugs that interfere with the user experience.

    By paying attention to these additional points, you can improve the quality and usability of your heatmap, ensuring that it is not only functionally effective but also user-friendly and accessible.

Conclusion and final words

Creating a simple heatmap color using HTML, CSS and JavaScript is a process that involves a few basic steps:

  1. HTML Structure: Provides a framework for displaying heatmaps with div elements as containers.
  2. CSS Styling: Sets the layout and style of the heatmap, such as grid size, box spacing, and text styling.
  3. JavaScript Manipulation: Populate the heatmap with dynamic data, set the background color of the box based on the data value, and add the box element to the container.
This process produces a visual display that makes it easier to analyze data based on color, with each box representing a different value.

Creating heatmap color is an effective way to present numerical data in an intuitive and easy-to-understand way. With a combination of HTML, CSS and JavaScript, you can build compelling and useful data visualizations for a variety of applications, whether in data analysis, dashboards or information presentations.

Keep experimenting with different data and designs to improve your skills in creating effective data visualizations. If you have further questions or need help, don't hesitate to ask!

Good luck, and may your heatmap project be a success! 🚀

If you are having trouble creating a color heatmap using HTML, CSS and JavaScript, you can download the source code file of this project for free by clicking the "Download" button. You can also view a live demo of this color heatmap by clicking the "View Live" button to see how the design and functionality of the color heatmap work in practice.

By accessing the source code file and live demo, you can easily understand the code implementation and customize the project according to your needs. Feel free to explore the code, experiment with dynamic coloring, and modify the features to improve your skills in web development.

This is a great opportunity to learn more about the integration of HTML, CSS Grid, and JavaScript to create interactive data visualizations. Happy learning and experimenting!

View Live Demo
Comment

LEAVE A REPLY