Chart libraries and SVG are the usual answers for dashboards, but they carry weight a performance-sensitive site may not want. For a plain line chart, CSS alone can do the job — with the help of a little trigonometry — using nothing more than a few custom properties in the markup.

Laying out the data

The starting point is a data set whose X-axis holds days of the week and whose Y-axis holds numeric values.

[
  { value: 25, dimension: "Monday" },
  { value: 60, dimension: "Tuesday" },
  { value: 45, dimension: "Wednesday" },
  { value: 50, dimension: "Thursday" },
  { value: 40, dimension: "Friday" }
]

The data goes into an unordered list wrapped in a <figure>, which marks the chart as self-contained content and leaves the door open for a <figcaption>. Each list item holds a single .data-point div carrying the raw number in a data-value attribute; keeping that element separate from the list item pays off once lines are drawn. An inline --widget-size custom property on the <figure> supplies the chart dimensions to the CSS.

<figure class="css-chart" style="--widget-size: 200px;">
  <ul class="line-chart">
    <li>
      <div class="data-point"></div>
    </li>
    <li>
      <div class="data-point"></div>
    </li>
    <li>
      <div class="data-point"></div>
    </li>
    <li>
      <div class="data-point"></div>
    </li>
    <li>
      <div class="data-point"></div>
    </li>
  </ul>
</figure>
/* The parent element */
.css-chart {
  /* The chart borders */
  border-bottom: 1px solid;
  border-left: 1px solid;
  /* The height, which is initially defined in the HTML */
  height: var(--widget-size);
  /* A little breathing room should there be others items around the chart */
  margin: 1em;
  /* Remove any padding so we have as much space to work with inside the element */
  padding: 0;
  position: relative;
  /* The chart width, as defined in the HTML */
  width: var(--widget-size);
}


/* The unordered list holding the data points, no list styling and no spacing */
.line-chart {
  list-style: none;
  margin: 0;
  padding: 0;
}


/* Each point on the chart, each a 12px circle with a light border */
.data-point {
  background-color: white;
  border: 2px solid lightblue;
  border-radius: 50%;
  height: 12px;
  position: absolute;
  width: 12px;
}
Well, it sort of looks like a chart.

Turning values into coordinates

With .data-point absolutely positioned inside a .css-chart container of fixed size, each value can be mapped onto the plane. Take a chart height of 200px and a maximum value of 60: setting that peak at 200px on the Y-axis means any value's Y coordinate is its ratio to 60 multiplied by 200. The largest value maps to:

(60 / 60) * 200 = 200px 

and the smallest, 25, lands at:

(25 / 60) * 200 = 83.33333333333334px

X coordinates are simpler. Spacing points evenly means dividing the 200px width by the five values in the array to get 40px, so the first point sits at 40px — leaving room for a left axis if desired — and the last at 200px.

The calculated positions go in as inline styles on each point div:

<figure class="css-chart">
  <ul class="line-chart">
    <li>
      <div class="data-point" style="bottom: 83.33333333333334px; left: 40px;"></div>
    </li>
    <li>
      <div class="data-point" style="bottom: 200px; left: 80px;"></div>
    </li>
    <li>
      <div class="data-point" style="bottom: 150px; left: 120px;"></div>
    </li>
    <li>
      <div class="data-point" style="bottom: 166.66666666666669pxpx; left: 160px;"></div>
    </li>
    <li>
      <div class="data-point" style="bottom: 133.33333333333331px; left: 200px;"></div>
    </li>
  </ul>
</figure>
We have a chart!

Drawing segments from right triangles

Adjacent data points and the gap between them form right triangles. The vertical difference between two points is one leg — the run of a stair step — and the 40px horizontal gap is the other. The hypotenuse of each triangle is exactly the length of line to draw to connect the dots. Considering the second and third points:

The second point has a Y of 200 and the third a Y of 150, so the opposite side is 200 − 150 = 50px against a 40px adjacent side. The hypotenuse is the square root of 50² plus 40², or 64.03124237432849.

<!-- ... --> 


<li>
  <div class="data-point" style="bottom: 200px; left: 80px;"></div>
</li>
<li>
  <div class="data-point" style="bottom: 150px; left: 120px;"></div>
</li>


<!-- ... -->
The hypotenuse is what we need to draw our line graph

Each list item gains an extra div acting as the segment, with an inline custom property for the hypotenuse length. The data points are repositioned through --x and --y custom properties placed on the containing <li> — the segments don't need to know the hypotenuse, so that value lives directly on .line-segment:

<!-- ... -->


<li>
  <div class="data-point"></div>
  <div class="line-segment" style="--hypotenuse: 64.03124237432849;"></div>
</li>


<!-- ... -->
<!-- ... -->


<li style="--y: 200px; --x: 80px">
  <div class="data-point"></div>
  <div class="line-segment" style="--hypotenuse: 64.03124237432849;"></div>
</li>


<!-- ... -->

The CSS is updated to read the new properties and style the segment element:

.data-point {
  /* Same as before */


  bottom: var(--y);
  left: var(--x);
}


.line-segment {
  background-color: blue;
  bottom: var(--y);
  height: 3px;
  left: var(--x);
  position: absolute;
  width: calc(var(--hypotenuse) * 1px);
}
We have all of the parts now. They just don’t fit together correctly yet.

Two corrections are needed before the result reads as a chart. The segments should originate at the centre of the data point circles rather than their base, which is handled by shifting the .data-point position to account for the point's size and border plus the segment width. And the segments should sit behind the points, which is done by placing the segment markup first.

.data-point {
  /* ... */


  /* The data points have a radius of 8px and the line segment has a width of 3px, 
    so we split the difference to center the data points on the line segment origins */
  bottom: calc(var(--y) - 6.5px);
  left: calc(var(--x) - 9.5px);
}
<!-- ... -->


<li style="--y: 200px; --x: 80px">
    <div class="line-segment" style="--hypotenuse: 64.03124237432849;"></div>
    <div class="data-point"></div>
</li>


<!-- ... -->

Rotating each segment into place

The last step is finding the angle facing the opposite side of each triangle and rotating the segment by that many degrees. Sine, cosine or tangent all work since every side is known; the mnemonic is sine = opposite over hypotenuse, cosine = adjacent over hypotenuse, tangent = opposite over adjacent. Using sine:

sin(x) = Opposite / Hypotenuse

In JavaScript, Math.asin(Opposite / Hypotenuse) returns radians, so the result is multiplied by (180 / Math.PI). For the example points, with an opposite of 50 and a hypotenuse of 64.03124237432849:

sin(x) = 50 /  64.03124237432849 = 51.34019174590991

Solving that for every point and passing the result as a custom property on each .line-segment yields markup like this:

<!-- ... -->


<li style="--y: 200px; --x: 80px">
  <div class="data-point"></div>
  <div class="line-segment" style="--hypotenuse: 64.03124237432849; --angle: 51.34019174590991;"></div>
</li>


<!-- ... -->
.line-segment {
  /* ... */
  transform: rotate(calc(var(--angle) * 1deg));
  width: calc(var(--hypotenuse) * 1px);
}

Rendered with those properties, the chart is finally recognisable:

Well, the line segments are definitely rotated. We need one more step.

The segments now point the wrong way because transform: rotate() pivots around an element's centre by default. Setting one more property on .line-segment moves the pivot to the bottom-left corner, so the rotation angles away from the current point toward the next:

.line-segment {
  /* ... */
  transform: rotate(calc(var(--angle) * 1deg));
  transform-origin: left bottom;
  width: calc(var(--hypotenuse) * 1px);
}
At last! A line graph!

Watch the sign. The opposite side (the rise) must be calculated as the current point's Y position minus the next point's Y position. A larger next value then produces a negative number and a negative rotation, which is what makes the line slope upwards.

Where it fits, and what to tighten

The technique suits a static site or server-side generated content, and it can serve client-side dynamic content too — at the cost of running JavaScript on the client. calc() cannot compute sine, cosine or tangent, so the X, Y, hypotenuse and angle values are either worked out by hand or generated by a small script, on either side. All rendering itself, including the points and the connecting lines, is CSS and HTML, and works without JavaScript enabled. That distinction matters: a script may produce the numbers, but no library is downloaded to draw a simple chart.

Three areas invite improvement:

  • Responsiveness. The fixed chart size is the opposite of responsive design. With client-side JavaScript, --widget-size can drive every calculation and be updated on initial display or resize via a container query or resize listener.
  • Tooltips. A ::before pseudo-element on .data-point could surface the data-value content on hover.
  • Axis labels and margins. Labels for the highest value, zero and the points between could be distributed along the axis. For margins, subtracting the width of a data point from the Y coordinate range, and from the total chart width before splitting it into equal regions, keeps points clear of container edges.