A trend line draws itself across a chart, the area under it fills in and a marker pops on the last point. Made for dashboards and finance views; pathLength keeps the draw size independent.
Also known as: graph drawing animation, stock chart loader, trend line animation.
<div class="ldr-line-chart" role="status" aria-label="Loading chart">
<svg viewBox="0 0 90 50" aria-hidden="true">
<path class="ldr-line-chart-axis" d="M3 46 H87" />
<path class="ldr-line-chart-area" d="M6 38 L20 28 L33 32 L47 19 L60 24 L74 11 L84 7 V46 H6 Z" />
<path class="ldr-line-chart-line" pathLength="100" d="M6 38 L20 28 L33 32 L47 19 L60 24 L74 11 L84 7" />
<circle class="ldr-line-chart-dot" cx="84" cy="7" r="3.6" />
</svg>
</div>.ldr-line-chart {
--s: var(--ldr-size, 48px);
--c: var(--ldr-color, #3b82f6);
width: calc(var(--s) * 1.8);
height: var(--s);
}
.ldr-line-chart svg {
display: block;
width: 100%;
height: 100%;
overflow: visible;
}
.ldr-line-chart-axis {
fill: none;
stroke: color-mix(in srgb, var(--ldr-color, #3b82f6) 30%, transparent);
stroke-width: 2;
stroke-linecap: round;
}
.ldr-line-chart-area {
fill: color-mix(in srgb, var(--ldr-color, #3b82f6) 14%, transparent);
animation: ldr-line-chart-area 2.8s ease infinite;
}
.ldr-line-chart-line {
fill: none;
stroke: var(--c);
stroke-width: 3;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 100;
animation: ldr-line-chart-draw 2.8s infinite;
}
.ldr-line-chart-dot {
fill: var(--c);
transform-box: fill-box;
transform-origin: center;
animation: ldr-line-chart-dot 2.8s infinite;
}
@keyframes ldr-line-chart-draw {
0% { stroke-dashoffset: 100; opacity: 1; animation-timing-function: cubic-bezier(0.45, 0, 0.2, 1); }
45%, 84% { stroke-dashoffset: 0; opacity: 1; animation-timing-function: ease-in; }
95%, 100% { stroke-dashoffset: 0; opacity: 0; }
}
@keyframes ldr-line-chart-area {
0%, 30% { opacity: 0; }
55%, 84% { opacity: 1; }
95%, 100% { opacity: 0; }
}
@keyframes ldr-line-chart-dot {
0%, 42% { transform: scale(0); opacity: 1; animation-timing-function: cubic-bezier(0.34, 1.56, 0.64, 1); }
52%, 84% { transform: scale(1); opacity: 1; animation-timing-function: ease-in; }
95%, 100% { transform: scale(1); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
.ldr-line-chart, .ldr-line-chart *, .ldr-line-chart::before, .ldr-line-chart::after, .ldr-line-chart *::before, .ldr-line-chart *::after {
animation-duration: 3s;
}
}--ldr-color on the element or a parent.
Currently previewing #3b82f6.--ldr-size. Every dimension in the snippet is
derived from it. Currently previewing 64px.animation shorthand.
Delays on the children are relative, so they keep their rhythm.role="status" and an
aria-label so screen readers announce the wait. The reduced-motion rule
at the end slows the animation for users who asked for less motion.