Source

47 chart lines · 1 file · 1.3 kBBenchmark harness excluded · shared/mount.ts

Apple daily stock prices1,260 records · CSV · 92.4 kB
@charts-poc/demo-data/aapl

Selection: Complete published snapshot

Date
Date
Open
number
High
number
Low
number
Close
number
Adj Close
number
Volume
number

Yahoo! Finance@observablehq/sample-datasets@1.0.1 · revision 732c0148de74 · aapl.csv · ISC distribution; upstream source credited · SHA-256 18dc8bf6542dPinned snapshot

cases/13-interval-timeline/tanstack.ts47 lines · entry
cases/13-interval-timeline/tanstack.ts
import { aapl } from '@charts-poc/demo-data/aapl'
import { barX, colorLegend, defineChart } from '@tanstack/charts'
import { scaleBand, scaleLinear } from 'd3-scale'
import { tanstackMount } from '../../shared/mount'
import type { ConformanceInput } from '../../types'

const colors = ['#10b981', '#ef4444']
const date = new Intl.DateTimeFormat('en-US', {
  month: 'short',
  day: 'numeric',
  timeZone: 'UTC',
})

const definition = (input: ConformanceInput) => {
  const rows = aapl.slice(input.revision * 3, input.revision * 3 + 8)

  return defineChart({
    marks: [
      barX(rows, {
        x1: 'Open',
        x2: 'Close',
        y: 'Date',
        color: (row) => (row.Close >= row.Open ? 'Gain' : 'Loss'),
        inset: 1,
        radius: 3,
      }),
    ],
    x: { scale: scaleLinear, grid: true, axis: { label: 'Share price ($)' } },
    y: {
      scale: () => scaleBand<Date>().paddingInner(0.16),
      axis: { ticks: { format: (value) => date.format(value) } },
    },
    color: {
      range: colors,
      legend: colorLegend({ label: 'Session' }),
    },
  })
}

export const mount = tanstackMount(
  definition,
  'Apple daily open-to-close price ranges',
  {
    format: (point) =>
      `${date.format(point.datum.Date)} · Open $${point.datum.Open.toFixed(2)} · Close $${point.datum.Close.toFixed(2)}`,
  },
)