Open In App

How to create an Area Chart using CSS ?

In this article, we will see how to create Area charts using CSS. There are many Libraries available, which can help us to build area charts, although, here, we will be using pure HTML and CSS to create an Area Chart.

An Area Chart is a graphical representation of quantitative data where multiple data items are combined to form a graph.



 

The above diagram represents the basic area chart in the XY-plane with 4 data items. Now, we will create a similar area chart with the below approach.

Steps to create an Area Chart:



<div class="area_chart">
 <div></div>
 <div></div>
 <div></div>
 <div></div>
</div>
<div class="area_chart">
 <div style="--top_left: 0.9; --top_right: 0.6;"></div>
 <div style="--top_left: 0.6; --top_right: 0.4;"></div>
 <div style="--top_left: 0.4; --top_right: 0.5;"></div>
 <div style="--top_left: 0.5; --top_right: 0.2;"></div>
</div>
.area_chart {
     width: 700px;
     height:400px;
     display: flex;
}
clip-path: polygon(
   0% calc(100% * var(--top_left)),
   100% calc(100% * var(--top_right)),
   100% 100%,
   0% 100%
 );

This is an example of a data item in the graph:

 

Note: The custom variables –top_left and –top_right are important to get the length of the data item in the graph. So, we can apply the same style with different dimensions for different data items.

Example 1: This example describes the basic implementation of the above steps mentioned to create the Area Chart.




<!DOCTYPE html>
<html>
  
<head>
    <title>Area Chart</title>
    <style>
        .area_chart {
            width: 700px;
            height: 400px;
            display: flex;
        }
  
        .area_chart>* {
            flex-grow: 1;
            border: 0.1px solid;
            background: rgba(118, 255, 30, .75);
            clip-path: polygon(0% calc(100% * var(--top_left)),
                    100% calc(100% * var(--top_right)),
                    100% 100%,
                    0% 100%);
        }
    </style>
</head>
  
<body>
    <h1 style="color:green">GeekForGeeks</h1>
    <h3>How to create an Area Chart using CSS ?</h3>
  
    <div class="area_chart">
        <div style="--top_left: 0.9; --top_right: 0.6;"></div>
        <div style="--top_left: 0.6; --top_right: 0.4;"></div>
        <div style="--top_left: 0.4; --top_right: 0.5;"></div>
        <div style="--top_left: 0.5; --top_right: 0.2;"></div>
    </div>
</body>
  
</html>

Output:

 

Example 2: This is the more elegant look of the above chart. In this, we are using paragraph tag <p> to represent the data items and also we are adding some space between the data items using justify-content. Adding 20% as width and rounding the data items with border-radius as shown in the output.




<!DOCTYPE html>
<html>
  
<head>
    <title>Area Chart </title>
    <style>
        .area_chart {
            width: 700px;
            height: 400px;
            border-bottom: 6px solid rgba(118, 255, 30, .75);
            display: flex;
            justify-content: space-between;
        }
  
        .area_chart>* {
            width: 20%;
            border-radius: 50px;
            background: rgba(118, 255, 30, .75);
            clip-path: polygon(0% calc(100% * var(--top_left)),
                    100% calc(100% * var(--top_right)),
                    100% 100%,
                    0% 100%);
        }
  
        p {
            margin: 0;
        }
    </style>
</head>
  
<body>
    <h1 style="color:green">GeekForGeeks</h1>
    <h3>How to create an Area Chart using CSS ?</h3>
  
    <div class="area_chart">
        <p style="--top_left: 0.9; --top_right: 0.6;"></p>
        <p style="--top_left: 0.6; --top_right: 0.4;"></p>
        <p style="--top_left: 0.4; --top_right: 0.5;"></p>
        <p style="--top_left: 0.5; --top_right: 0.2;"></p>
    </div>
</body>
  
</html>

Output:


Article Tags :