Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/shared/history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ test('the x grid: sections about as wide as the value lines are apart, each wide
expect(chartColumns(150, 360, 33, 71)).toEqual([50, 99]);
// a line pushed by rounding so its date would touch the last date is not drawn at all, never a line without a date
expect(chartColumns(4, 300, 100)).toEqual([]);
// the same on the left: a first line rounded so close to the left edge that its date would touch the first day's date
// is not drawn (4 sections, the first line at 94 px, its date from 59 px; the first day's date ends at 70 + 12)
expect(chartColumns(6, 470, 117)).toEqual([3]);
expect(chartColumns(2, 900, 150)).toEqual([]);
expect(chartColumns(61, 60, 150)).toEqual([]);
});
Expand Down
9 changes: 5 additions & 4 deletions src/shared/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,18 @@ export function chartLevelsShown(values: number[], label: (v: number) => string)
/**
* The x grid of a chart: the points under its inner vertical lines, each date centred under its line (owner 2026-12-03:
* "we should see line in a mid on top of label and label should represent it, left border we dont care, but right would
* be today latest date border"). The lines cut the plot into equal sections about as far apart as the value lines (owner
* 2026-12-01), never so close that two dates touch: a section is at least one and a half dates and `gap` wide, so the
* last line's date also clears the series' last date, which ends at the plot's right edge. Every line has its date: a
* be today latest date border"; then "we should have see beginning date on the left": the first day starts at the plot's
* left edge, the last day ends at its right edge). The lines cut the plot into equal sections about as far apart as the
* value lines (owner 2026-12-01), never so close that two dates touch: a section is at least one and a half dates and
* `gap` wide, so the first and the last line's dates also clear the edge dates. Every line has its date: a
* line whose date would not fit is not drawn. None when the plot is too narrow. `count`: points in the series,
* `labelWidth`: the width of one date
*/
export function chartColumns(count: number, plotWidth: number, rowGap: number, labelWidth = 70, gap = 12): number[] {
if (count < 3 || plotWidth <= 0 || rowGap <= 0) return [];
const n = Math.min(Math.max(1, Math.round(plotWidth / rowGap)), Math.floor(plotWidth / (1.5 * labelWidth + gap)), count - 1);
const out: number[] = [];
let free = 0; // where the next date may start
let free = labelWidth + gap; // where the next date may start: after the first day's date at the left edge
for (let k = 1; k < n; k++) {
const i = Math.round((k / n) * (count - 1));
const at = (i / (count - 1)) * plotWidth;
Expand Down
6 changes: 4 additions & 2 deletions src/viewer/ui/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { chartColumns, chartLevelsShown, resample, valueRange, type Point } from
import { shortUsd, siteDate, usd } from './format';

// A light SVG line chart in the Fidelity look: the line with a soft fill, five value levels on the right (three when the
// rounded labels would repeat), dates below, each centred under its grid line and the last day at the right edge, a crosshair with the date and value under the pointer. Sizes itself to its box (ResizeObserver)
// rounded labels would repeat), dates below, each centred under its grid line, the first day at the left edge and the last at the right, a crosshair with the date and value under the pointer. Sizes itself to its box (ResizeObserver)
export function LineChart({ points, height = 220, up }: { points: Point[]; height?: number; up: boolean }) {
const box = useRef<HTMLDivElement>(null);
const [width, setWidth] = useState(600);
Expand Down Expand Up @@ -99,7 +99,9 @@ export function LineChart({ points, height = 220, up }: { points: Point[]; heigh
))}
<path d={area} fill="url(#fa-fill)" />
<path d={line} fill="none" stroke={color} strokeWidth="2" strokeLinejoin="round" />
{/* the left edge has no date, the right edge the series' last day (owner 2026-12-03) */}
{/* the left edge has the series' first day, the right edge its last (owner 2026-12-03: "similarly like we have
Dec 3 for today, we should have see beginning date on the left") */}
<text x={pad.l} y={height - 6} fontSize="11" textAnchor="start" fill="currentColor" fillOpacity="0.6" data-first-date>{siteDate(points[0]!.date)}</text>
<text x={pad.l + w} y={height - 6} fontSize="11" textAnchor="end" fill="currentColor" fillOpacity="0.6" data-last-date>{siteDate(points[points.length - 1]!.date)}</text>
{hp && (
<g>
Expand Down