Skip to content

CandlestickChart

For FX/crypto pairs.
Implemented
Status
Implemented
Family
7 of 13
Registry
@lopifi/candlestick-chart
npx shadcn@latest add @lopifi/theme @lopifi/candlestick-chart

The component, rendering

Real data, the shipped component. Switch the theme to check the claims the spec makes below.

Preview

The four-layer spec

For FX and crypto pairs. Up and down candles differ in fill (hollow vs. solid) as well as color — the traditional convention, and it happens to be the correct accessibility answer.

Lifted verbatim from Dashboard & charts. The doc and this page are the same paragraphs, so they cannot disagree.

What lands in your repository

One component is never one file. Everything below is copied into your project, where you own it.

Files

components/ledger/candlestick-chart.tsx

Lopifi dependencies · 5
chart-primitiveschart-utilscnformat-moneymoney.types
npm packages
clsxtailwind-merge
Registry entry
/r/candlestick-chart.json

The whole component

No compiled bundle, no black box. This is the file the registry copies, and the file you will edit.

components/ledger/candlestick-chart.tsx
"use client";
import { niceTicks } from "@/lib/ledger/chart-utils";
import { DataTableToggle } from "@/components/ledger/chart-primitives";

export type Candle = {
  label: string;
  open: number;
  high: number;
  low: number;
  close: number;
};

export interface CandlestickChartProps {
  candles: Candle[];
  className?: string;
  height?: number;
  pair: string;
}

/**
 * Up and down candles differ in **fill** (hollow vs. solid) as well as
 * colour — the traditional convention, which happens to be the correct
 * accessibility answer.
 *
 * A rate axis does not start at zero; forcing it destroys the resolution.
 */
export function CandlestickChart({ candles, pair, height = 280, className }: CandlestickChartProps) {
  const width = 720;
  const PAD = { bottom: 28, left: 72, right: 16, top: 16 };
  const lows = candles.map((c) => c.low);
  const highs = candles.map((c) => c.high);
  const pad = (Math.max(...highs) - Math.min(...lows)) * 0.1 || 0.001;
  const min = Math.min(...lows) - pad;
  const max = Math.max(...highs) + pad;
  const ticks = niceTicks(min, max, 4);

  const plotW = width - PAD.left - PAD.right;
  const plotH = height - PAD.top - PAD.bottom;
  const y = (v: number) => PAD.top + plotH - ((v - min) / (max - min || 1)) * plotH;
  const slot = plotW / candles.length;
  const bodyW = slot * 0.6;

  return (
    <DataTableToggle
      className={className}
      table={
        <table className="w-full text-body">
          <caption className="sr-only">{pair} price data</caption>
          <thead>
            <tr className="border-b border-border-subtle">
              {["Period", "Open", "High", "Low", "Close"].map((h) => (
                <th
                  className="py-2 text-right text-label text-text-secondary first:text-left"
                  key={h}
                  scope="col"
                >
                  {h}
                </th>
              ))}
            </tr>
          </thead>
          <tbody>
            {candles.map((c) => (
              <tr className="border-b border-border-subtle" key={c.label}>
                <th className="py-2 text-left font-normal" scope="row">
                  {c.label}
                </th>
                {[c.open, c.high, c.low, c.close].map((v, i) => (
                  <td className="numeric py-2 text-right" key={i}>
                    {v.toFixed(5)}
                  </td>
                ))}
              </tr>
            ))}
          </tbody>
        </table>
      }
    >
      <svg
        aria-label={`${pair} candlestick chart`}
        className="w-full"
        role="img"
        style={{ height }}
        viewBox={`0 0 ${width} ${height}`}
      >
        {ticks.map((t) => (
          <g key={t}>
            <line stroke="var(--border-subtle)" x1={PAD.left} x2={width - PAD.right} y1={y(t)} y2={y(t)} />
            <text
              className="numeric"
              fill="var(--text-tertiary)"
              fontSize="12"
              textAnchor="end"
              x={PAD.left - 8}
              y={y(t) + 4}
            >
              {t.toFixed(4)}
            </text>
          </g>
        ))}
        {candles.map((c, i) => {
          const cx = PAD.left + i * slot + slot / 2;
          const up = c.close >= c.open;
          const color = up ? "var(--positive)" : "var(--negative)";
          const top = y(Math.max(c.open, c.close));
          const h = Math.max(1, Math.abs(y(c.open) - y(c.close)));
          return (
            <g key={c.label}>
              <line stroke={color} strokeWidth="1" x1={cx} x2={cx} y1={y(c.high)} y2={y(c.low)} />
              <rect
                fill={up ? "var(--surface)" : color}
                height={h}
                stroke={color}
                strokeWidth="1.5"
                width={bodyW}
                x={cx - bodyW / 2}
                y={top}
              />
            </g>
          );
        })}
      </svg>
      <p className="mt-2 text-caption text-text-tertiary">
        Hollow candles closed up, solid closed down. The rate axis does not start at zero.
      </p>
    </DataTableToggle>
  );
}

Also in Dashboard & charts

See also

A component is specified in one place, defined in another, and running in a third. All three, from here.

Elsewhere in the system

Where CandlestickChart sits in the rest of the system — the live section it renders in, the calculators that compute what it displays, and the ledger payloads it is fed from.

Components are copied into your repository and meant to be edited — rounding policy, fiscal calendar and compliance copy differ per company.

Told when Dashboard & charts changes

Components are copied into your repo, so a fix here does not reach you on its own. One email when a component in this family changes in a way that needs a migration — and nothing when it does not.

One address, no list rental, unsubscribe in one click.

Works in your QuickBooks Online or Xero file

Get your books closed every month

Lopifi is building AI bookkeeping that keeps your books closed on US GAAP as transactions happen, with a CPA review every quarter, for $200 a month. We’re not open yet.

$200/month when we open. No card now.