How to create a Line Chart With CSS ?
Last Updated :
01 Sep, 2022
The Chart is a pictorial or graphical representation of the data visualization, based on certain conditions There are various types of charts available such as Bar Charts, Line Charts, Pie Charts, etc, that are used to represent important data. In this article, we will see how to create the Line chart using HTML & CSS.
A Line Chart is a chart that is used to display data in the form of points connected by straight lines. It is usually used to display those data that changes from time to time in a continuous manner.
Steps to create a Line chart: Here, we will see the steps to make the Line Chart by applying a little bit of knowledge of mathematics. There are some points that will be plotted on the coordinate axes, X-axis & Y-axis. So suppose we have 5 points ie., (40,80) , (80,120) , (120,80) , (160,120) , (200,80) and take x-axis and y-axis of 250 Units. Now, we will plot these points on the XY plane using the following steps:
Step 1: Create XY Plane: We will use a div tag and put an id selector with the name "chart". After that, we will apply CSS using that id sector on this div to make it like XY Plane.
<div id="chart"></div>
CSS:
#chart {
border-bottom: 2px solid black;
border-left: 2px solid black;
margin-left:500px;
margin-top: 100px;
padding: 0;
height: 250px;
width:250px;
position: relative;
}
On implementing the above style, we will see the below output:
Step 2: Mark which axis is the x-axis and which axis is the y-axis:
We will use two <p> tags inside that div that contains the x-axis and y-axis and after that position it accordingly using CSS.
<div id="chart">
<p id="x1">x-axis</p>
<p id="y1">y-axis</p>
</div>
CSS:
#x1, #y1 {
position: absolute;
font-size: larger;
}
#x1 {
bottom: -50px;
left: 100px;
}
#y1{
left: -60px;
bottom: 110px;
}
Here in CSS, we have made the position absolute, so that it can be positioned relative to its parent and then uses left and bottom to do so.
Step 3: Create Points and line segments joining those points:
We will make 4 sets of point and line segments. In those sets, the line segment will be starting from the point present in that set and goes to the nearer point. We will also make a point which will be our 5th point. This point will not be in any set with line segments because we only need 4 lines. So, no lines will be originating from this 5th point. Only one line will come and connect with this point. After that, our task is to position those points and line them according to their given coordinates.
Now, we will make 5 <div> inside that main <div> which was representing coordinate-axes. Then from that 5 div, we will take 4 div and make two div inside each of them. Where 1st div will be representing a point and the other will be representing a line. In the 5th div, we will only make a single div that will be representing the 5th point. Here, We haven't created any div for the line because we only need 4 lines. There is no need for 5th line.
Now, we will use some Mathematics by taking the two nearest points and those are (40,80) and (80,120). So the length of the line between these two points will be the hypotenuse of the right-angled triangle made by the difference between their x-coordinate and y-coordinate. See below Fig. 1. Now for inclination, we will use the following trigonometric formula:
tan(angle) = Perpendicular / Base
Fig. 1So, we will calculate the length of the line and its inclination for every other line segment. We will use this information while styling every line segment. This is what our generalized HTML code will look like:
<div id="chart">
<p id="x1">x-axis</p>
<p id="y1">y-axis</p>
<div>
<div id="point..."></div>
<div id="line..."></div>
</div>
...
</div>
Step 4: Applying the CSS properties to those points & Lines:
Now, we have to use CSS to make those points and lines visible. We can see in the above HTML code that we have given "id" to every div that are representing a line and point. We will use these id selectors to style lines and points. Let's see the CSS for one point and similarly, will be done for other points:
#point1 {
background-color: violet;
border-radius: 50%;
bottom: 76px;
left: 36px;
position: absolute;
height: 12px;
width: 12px;
z-index: 1;
}
Now, let's see the CSS for one line, and similarly, will be done for other lines:
#line1 {
background-color: black;
bottom: 80px ;
height: 3px;
left: 40px;
position: absolute;
transform: rotate(-45deg);
transform-origin: left bottom;
width: 56.57px;
}
Note: You must note that rotate() will by default rotate the line in a clockwise direction if the angle is passed without any sign. If you will pass the angle with the -ve sign then it will rotate in an anticlockwise direction. For instance, if we pass 45 degrees then it will rotate 45 degrees in the Clockwise direction and if we will pass -45 degrees then it will pass 45 degrees in Anticlockwise Direction.
Example: This example describes the creation of the Line Chart with the help of HTML & CSS.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Line Chart</title>
<style>
#x1,
#y1 {
position: absolute;
font-size: larger;
}
#x1 {
bottom: -50px;
left: 100px;
}
#y1 {
left: -60px;
bottom: 110px;
}
#h1,
#h3 {
text-align: center;
color: green;
}
#chart {
border-bottom: 2px solid black;
border-left: 2px solid black;
margin-left: 600px;
margin-top: 45x;
height: 250px;
width: 250px;
position: relative;
}
#point1,
#point2,
#point3,
#point4,
#point5 {
border-radius: 50%;
position: absolute;
height: 12px;
width: 12px;
z-index: 1;
}
#point1 {
background-color: violet;
bottom: 76px;
left: 36px;
}
#point2 {
background-color: pink;
bottom: 116px;
left: 76px;
}
#point3 {
background-color: orange;
bottom: 76px;
left: 116px;
}
#point4 {
background-color: greenyellow;
bottom: 116px;
left: 156px;
}
#point5 {
background-color: green;
bottom: 76px;
left: 196px;
}
#line1,
#line2,
#line3,
#line4 {
height: 3px;
position: absolute;
transform-origin: left bottom;
width: 56.57px;
}
#line1 {
background-color: black;
bottom: 80px;
left: 40px;
transform: rotate(-45deg);
}
#line2 {
background-color: black;
bottom: 120px;
left: 80px;
transform: rotate(45deg);
}
#line3 {
background-color: black;
bottom: 80px;
left: 120px;
transform: rotate(-45deg);
}
#line4 {
background-color: black;
bottom: 120px;
left: 160px;
transform: rotate(45deg);
}
</style>
</head>
<body>
<h1 id="h1">GeeksforGeeks</h1>
<h3 id="h3">Line Chart using HTML & CSS</h3>
<div id="chart">
<p id="x1">x-axis</p>
<p id="y1">y-axis</p>
<div>
<div id="point1"></div>
<div id="line1"></div>
</div>
<div>
<div id="point2"></div>
<div id="line2"></div>
</div>
<div>
<div id="point3"></div>
<div id="line3"></div>
</div>
<div>
<div id="point4"></div>
<div id="line4"></div>
</div>
<div>
<div id="point5"></div>
</div>
</div>
</body>
</html>
Output:
Similar Reads
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
CSS Introduction CSS (Cascading Style Sheets) is a language designed to simplify the process of making web pages presentable.It allows you to apply styles to HTML documents by prescribing colors, fonts, spacing, and positioning.The main advantages are the separation of content (in HTML) and styling (in CSS) and the
4 min read
CSS Syntax CSS is written as a rule set, which consists of a selector and a declaration block. The basic syntax of CSS is as follows:The selector is a targeted HTML element or elements to which we have to apply styling.The Declaration Block or " { } " is a block in which we write our CSS.HTML<html> <h
2 min read
CSS Selectors CSS Selectors are used to target HTML elements on your pages, allowing you to apply styles based on their ID, class, type attributes, and more. There are mainly 5 types of selectors.Basic CSS Selectors: These are used to target elements by tag, .class, or # ID for fundamental styling needs.Combinato
7 min read
CSS Comments CSS comments are used to add notes or explanations to your code, helping you and others understand it better. They start with /* and end with */ and can be used for both single-line and multi-line comments. Note: Comments are ignored by browsers, so they wonât affect how your webpage looks or works.
2 min read
CSS Colors CSS colors are used to set the color of different parts of a webpage, like text, background, and borders. This helps make the page look more attractive and easier to read. You can define colors using names, hex codes, RGB values, and more.You can try different formats of colors here- #content-iframe
5 min read
CSS Borders Borders in CSS are used to create a visible outline around an element. They can be customized in terms ofWidth: The thickness of the border.Style: The appearance of the border (solid, dashed, dotted, etc.).Color: The color of the border.You can try different types of borders here- #custom-iframe{ he
5 min read
CSS Margins CSS margins are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and readability.Syntax:body { margin: value;}HTML<html> <head>
4 min read
CSS Height and Width Height and Width in CSS are used to set the height and width of boxes. Their values can be set using length, percentage, or auto.Width and HeightThe width and height properties in CSS are used to define the dimensions of an element. The values can be set in various units, such as pixels (px), centim
4 min read
CSS Outline CSS outline is a property used to draw a line around an element's border. It does not affect the layout, unlike borders. It's often used to highlight elements, providing a visual emphasis without altering the dimensions of the element.Syntaxselector{ outline: outline-width outline-type outline-color
4 min read