webdevpuneet.com

Candlestick Chart — Stock OHLC HTML CSS JS Snippet

Candlestick Chart · Charts · Plain HTML, CSS & JS · Live preview

CategoryCharts

Copy & paste

Source Code

<div class="ck-card">
  <div class="ck-head">
    <div>
      <div class="ck-sym">AURX <span class="ck-tf">· 1D</span></div>
      <div class="ck-price"><span id="ckPrice">—</span> <span class="ck-chg" id="ckChg">hover a candle</span></div>
    </div>
    <div class="ck-hl"><span class="up">H <b id="ckHi">—</b></span><span class="down">L <b id="ckLo">—</b></span></div>
  </div>
  <div class="ck-plot" id="ckPlot">
    <svg class="ck-svg" id="ckSvg" viewBox="0 0 320 180" preserveAspectRatio="none"></svg>
    <div class="ck-tip" id="ckTip"></div>
  </div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

True OHLC candles
Each candle is a wick <line> (high→low) plus a body <rect> (open→close), coloured green/red by direction — the standard candlestick anatomy.
Auto-scaling axis
build maps prices to the data's min/max within padding, so any instrument or price range renders correctly with no config.
Doji-safe bodies
A minimum body height keeps open≈close candles visible as a thin line instead of disappearing.
Generous hit areas
A transparent full-height <rect> per candle makes hovering forgiving — you needn't land on the thin body.
Hover crosshair
A dashed vertical line snaps to the hovered candle, the crosshair UX traders expect from a terminal.
OHLC tooltip + readout
show fills a tooltip with formatted O/H/L/C and updates the header price and coloured percent change.
Latest-candle default
Leaving the chart restores the readout to the most recent candle, never leaving a stale hovered value.
Crisp scalable SVG
Candles are hoverable DOM elements that stay sharp at any size via preserveAspectRatio — no canvas hit-testing.

About this UI Snippet

Candlestick Chart — OHLC Bodies & Wicks, Hover Crosshair & Live Price Readout

Screenshot of the Candlestick Chart snippet rendered live

The candlestick chart is the language of financial markets — every trading app, crypto dashboard, and stock tracker uses it because a single candle encodes four values (open, high, low, close) and its colour shows direction at a glance. This snippet draws a real OHLC candlestick chart with an SVG in plain HTML, CSS, and vanilla JavaScript: green/red bodies, high-low wicks, a hover crosshair, and a tooltip with the candle's OHLC plus a live price and percent-change readout.

Anatomy of a candle

Each candle is an SVG <g> containing a thin <line> wick from the high to the low and a <rect> body spanning the open-to-close range. The body is green (.ck-up) when close ≥ open and red (.ck-down) otherwise — the universal convention. build computes the data's overall high and low, derives a y mapping that flips the axis (higher price = higher on screen) and fits everything within padding, then positions each candle at an evenly spaced cx. A minimum body height (Math.max(1.5, …)) keeps doji candles (open ≈ close) visible as a thin line rather than vanishing.

Hover crosshair and OHLC tooltip

Each candle's <g> includes a full-height transparent <rect> as a generous hit area, so you do not have to land precisely on the thin body. On mouseenter, show moves a dashed vertical crosshair to that candle, updates the header price and percent change (coloured green/red), and fills a tooltip with the formatted O, H, L, C values positioned over the candle. Leaving the plot snaps the readout back to the latest candle and hides the crosshair — exactly how trading terminals behave.

Auto-scaling and data generation

The chart auto-scales to the min/max of the dataset, so it works for any price range without configuration. A genData function produces a realistic random walk (each candle opens at the previous close, with randomised high/low spreads) so the demo looks like a real instrument; replace it with your OHLC array from a market API and call build.

Why SVG over Canvas

SVG candles are individually hoverable DOM elements — no hit-testing math, no manual redraws on hover — which makes the crosshair and tooltip trivial, and it stays crisp at any size via preserveAspectRatio. For thousands of candles you would switch to Canvas, but for the typical 20–60 visible candles a dashboard shows, SVG is simpler and exports cleanly to every framework.

Pair this with a realtime line chart for live ticks, a sparkline chart for compact trends, or a metric card grid for portfolio stats.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA dark "AURX · 1D" card appears with 24 green/red candlesticks and the latest candle's price and % change in the header.
  2. 2
    Hover a candleA dashed crosshair snaps to it, the header price and percent change update, and a tooltip shows that candle's O/H/L/C.
  3. 3
    Read direction at a glanceGreen candles closed up, red closed down; the wick shows the full high-to-low range, the body the open-to-close range.
  4. 4
    Move along the seriesSweep across the candles — the crosshair, price, and tooltip follow each one in turn.
  5. 5
    Leave the chartThe readout returns to the most recent candle and the crosshair hides.
  6. 6
    Plug in real OHLC dataReplace genData with your market data array of { o, h, l, c } and call build; the chart auto-scales.

Real-world uses

Common Use Cases

Trading and brokerage apps
The core price chart for stocks, forex, or crypto. Pair with a realtime line chart for the live tick line.
Crypto dashboards
Show OHLC candles per coin with the header price/percent readout; combine with a metric card grid for portfolio stats.
Market and watchlist widgets
Compact candlestick cards in a watchlist; a sparkline chart works for the tighter rows.
Backtesting and analytics tools
Visualise historical OHLC bars with hover inspection of each candle's values.
Fintech marketing and demos
An impressive, dependency-free chart for landing pages and product tours.
Education and trading tutorials
Teach candle anatomy — body, wick, colour — with an interactive, hoverable example.
Related: Dumbbell Chart
See the Dumbbell Chart for a related charts pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

Replace genData with your data: fetch an array of { o, h, l, c } (open/high/low/close) from a market API and pass it to build (assign it to DATA and skip the generator). The chart auto-scales to the data's min/max, so no axis configuration is needed. For live updates, append the newest candle and re-run build, or update only the last candle's rect/line for efficiency.

For volume, reserve the bottom ~20% of the viewBox and draw a thin <rect> per candle scaled to its volume. For a moving average, compute the rolling mean and draw a smooth <path> (a polyline or Bézier) over the candles. Both overlay cleanly because everything shares the same x step and y scale used by the candles.

SVG is ideal up to a few hundred candles: each is a real DOM element, so hover, crosshair, and tooltips need no hit-testing and it stays crisp at any size. For thousands of candles or high-frequency live updates, Canvas (or WebGL) is faster because it avoids per-element DOM overhead. This snippet uses SVG for the typical 20–60 visible candles a dashboard shows.

An SVG price chart is hard for screen readers, so expose the data textually: the header already shows the current price and change as real text, and you can add an aria-label to the SVG summarising the trend plus an off-screen data table of OHLC values. Ensure up/down is conveyed by more than colour — the percent arrow (▲/▼) and the tooltip labels do this.

In React, compute the candle geometry with useMemo from your OHLC array and render <g>/<line>/<rect> elements in JSX, tracking the hovered index in useState for the crosshair and tooltip. In Vue, use a computed for the candles and v-for. In Angular, compute in the component and *ngFor. The y-scale math and colour logic port unchanged.