{"spec_id":"ohlc-bar","library":"muix","language":"javascript","code":"// anyplot.ai\n// ohlc-bar: OHLC Bar Chart\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"ohlc-bar · javascript · muix · anyplot.ai\";\nconst PRICE_AXIS_LABEL = \"Price (USD)\";\nconst TITLE_HEIGHT = 56;\nconst Y_PAD_SHARE = 0.06;\nconst TICK_INTERVAL_STEP = 5;\nconst MONTHS = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"];\n\n// Columns: set these to your data's column names\nconst DATE = \"Trading session\"; // role: date\nconst OPEN = \"Open (USD)\"; // role: open\nconst HIGH = \"High (USD)\"; // role: high\nconst LOW = \"Low (USD)\"; // role: low\nconst CLOSE = \"Close (USD)\"; // role: close\n\n// Data: replace this block with your own records\nconst data: Array<Record<string, string | number>> = [\n  { [DATE]: \"2024-01-02\", [OPEN]: 218.26, [HIGH]: 221.39, [LOW]: 216.69, [CLOSE]: 219.51 },\n  { [DATE]: \"2024-01-03\", [OPEN]: 219.59, [HIGH]: 221.25, [LOW]: 216.2, [CLOSE]: 218.08 },\n  { [DATE]: \"2024-01-04\", [OPEN]: 217.56, [HIGH]: 222.07, [LOW]: 216.12, [CLOSE]: 220.43 },\n  { [DATE]: \"2024-01-05\", [OPEN]: 220.99, [HIGH]: 222.39, [LOW]: 217.99, [CLOSE]: 219.72 },\n  { [DATE]: \"2024-01-08\", [OPEN]: 219.35, [HIGH]: 223.95, [LOW]: 218.3, [CLOSE]: 222.58 },\n  { [DATE]: \"2024-01-09\", [OPEN]: 222.25, [HIGH]: 225.73, [LOW]: 220.51, [CLOSE]: 224.09 },\n  { [DATE]: \"2024-01-10\", [OPEN]: 224.51, [HIGH]: 226.47, [LOW]: 220.69, [CLOSE]: 222.36 },\n  { [DATE]: \"2024-01-11\", [OPEN]: 221.96, [HIGH]: 225.56, [LOW]: 220.76, [CLOSE]: 224.22 },\n  { [DATE]: \"2024-01-12\", [OPEN]: 224.58, [HIGH]: 226.15, [LOW]: 220.89, [CLOSE]: 222.71 },\n  { [DATE]: \"2024-01-16\", [OPEN]: 226.14, [HIGH]: 229.88, [LOW]: 224.36, [CLOSE]: 228.11 },\n  { [DATE]: \"2024-01-17\", [OPEN]: 227.79, [HIGH]: 229.47, [LOW]: 223.82, [CLOSE]: 225.63 },\n  { [DATE]: \"2024-01-18\", [OPEN]: 225.95, [HIGH]: 229.74, [LOW]: 224.14, [CLOSE]: 227.85 },\n  { [DATE]: \"2024-01-19\", [OPEN]: 228.19, [HIGH]: 232.42, [LOW]: 226.78, [CLOSE]: 230.89 },\n  { [DATE]: \"2024-01-22\", [OPEN]: 230.52, [HIGH]: 232.28, [LOW]: 227.05, [CLOSE]: 228.94 },\n  { [DATE]: \"2024-01-23\", [OPEN]: 229.28, [HIGH]: 233.61, [LOW]: 227.91, [CLOSE]: 231.98 },\n  { [DATE]: \"2024-01-24\", [OPEN]: 231.69, [HIGH]: 234.12, [LOW]: 228.44, [CLOSE]: 230.15 },\n  { [DATE]: \"2024-01-25\", [OPEN]: 230.49, [HIGH]: 235.18, [LOW]: 229.12, [CLOSE]: 233.54 },\n  { [DATE]: \"2024-01-26\", [OPEN]: 233.86, [HIGH]: 236.24, [LOW]: 230.65, [CLOSE]: 232.41 },\n  { [DATE]: \"2024-01-29\", [OPEN]: 232.11, [HIGH]: 236.89, [LOW]: 230.94, [CLOSE]: 235.22 },\n  { [DATE]: \"2024-01-30\", [OPEN]: 235.58, [HIGH]: 237.45, [LOW]: 231.38, [CLOSE]: 233.19 },\n  { [DATE]: \"2024-01-31\", [OPEN]: 232.88, [HIGH]: 236.12, [LOW]: 229.76, [CLOSE]: 231.42 },\n  { [DATE]: \"2024-02-01\", [OPEN]: 231.76, [HIGH]: 236.48, [LOW]: 230.52, [CLOSE]: 234.86 },\n  { [DATE]: \"2024-02-02\", [OPEN]: 235.21, [HIGH]: 239.62, [LOW]: 233.84, [CLOSE]: 237.94 },\n  { [DATE]: \"2024-02-05\", [OPEN]: 237.62, [HIGH]: 239.44, [LOW]: 233.91, [CLOSE]: 235.78 },\n  { [DATE]: \"2024-02-06\", [OPEN]: 236.12, [HIGH]: 240.35, [LOW]: 234.68, [CLOSE]: 238.65 },\n  { [DATE]: \"2024-02-07\", [OPEN]: 238.98, [HIGH]: 242.84, [LOW]: 237.21, [CLOSE]: 241.12 },\n  { [DATE]: \"2024-02-08\", [OPEN]: 240.79, [HIGH]: 242.56, [LOW]: 237.14, [CLOSE]: 239.08 },\n  { [DATE]: \"2024-02-09\", [OPEN]: 239.42, [HIGH]: 243.91, [LOW]: 238.05, [CLOSE]: 242.18 },\n  { [DATE]: \"2024-02-12\", [OPEN]: 241.86, [HIGH]: 244.12, [LOW]: 238.22, [CLOSE]: 240.04 },\n  { [DATE]: \"2024-02-13\", [OPEN]: 239.71, [HIGH]: 242.15, [LOW]: 235.88, [CLOSE]: 237.62 },\n  { [DATE]: \"2024-02-14\", [OPEN]: 237.98, [HIGH]: 242.64, [LOW]: 236.54, [CLOSE]: 240.89 },\n  { [DATE]: \"2024-02-15\", [OPEN]: 241.24, [HIGH]: 245.38, [LOW]: 239.72, [CLOSE]: 243.65 },\n  { [DATE]: \"2024-02-16\", [OPEN]: 243.31, [HIGH]: 245.19, [LOW]: 239.84, [CLOSE]: 241.72 },\n  { [DATE]: \"2024-02-20\", [OPEN]: 244.51, [HIGH]: 246.78, [LOW]: 241.12, [CLOSE]: 242.96 },\n  { [DATE]: \"2024-02-21\", [OPEN]: 243.29, [HIGH]: 247.85, [LOW]: 241.94, [CLOSE]: 246.12 },\n  { [DATE]: \"2024-02-22\", [OPEN]: 246.48, [HIGH]: 250.42, [LOW]: 244.86, [CLOSE]: 248.75 },\n  { [DATE]: \"2024-02-23\", [OPEN]: 248.41, [HIGH]: 250.36, [LOW]: 244.92, [CLOSE]: 246.88 },\n  { [DATE]: \"2024-02-26\", [OPEN]: 247.22, [HIGH]: 251.64, [LOW]: 245.78, [CLOSE]: 249.92 },\n  { [DATE]: \"2024-02-27\", [OPEN]: 249.58, [HIGH]: 251.84, [LOW]: 246.15, [CLOSE]: 248.04 },\n  { [DATE]: \"2024-02-28\", [OPEN]: 248.39, [HIGH]: 252.91, [LOW]: 246.98, [CLOSE]: 251.18 },\n  { [DATE]: \"2024-02-29\", [OPEN]: 251.54, [HIGH]: 255.12, [LOW]: 249.62, [CLOSE]: 253.46 },\n  { [DATE]: \"2024-03-01\", [OPEN]: 253.12, [HIGH]: 255.28, [LOW]: 249.74, [CLOSE]: 251.82 },\n  { [DATE]: \"2024-03-04\", [OPEN]: 252.18, [HIGH]: 256.84, [LOW]: 250.88, [CLOSE]: 255.14 },\n];\n\n// Plot: reads only data and the constants above\nconst sortedData = [...data].sort((a, b) => String(a[DATE]).localeCompare(String(b[DATE])));\nconst dates = sortedData.map((r) => String(r[DATE]));\nconst opens = sortedData.map((r) => Number(r[OPEN]));\nconst highs = sortedData.map((r) => Number(r[HIGH]));\nconst lows = sortedData.map((r) => Number(r[LOW]));\nconst closes = sortedData.map((r) => Number(r[CLOSE]));\n\nconst years = Array.from(new Set(dates.map((d) => d.slice(0, 4)).filter((y) => /^\\d{4}$/.test(y))));\nconst yearSuffix =\n  years.length === 1\n    ? ` (${years[0]})`\n    : years.length > 1\n      ? ` (${years[0]}–${years[years.length - 1]})`\n      : \"\";\nconst xAxisLabel = `${DATE}${yearSuffix}`;\n\nconst yMin = Math.min(...lows);\nconst yMax = Math.max(...highs);\nconst yPad = (yMax - yMin) * Y_PAD_SHARE;\n\nconst legendItems = [\n  { color: t.palette[0], label: \"Up (close > open)\" },\n  { color: t.palette[4], label: \"Down (close < open)\" },\n];\n\nfunction OhlcBars() {\n  const xScale = useXScale(\"sessions\") as unknown as ((d: string) => number) & {\n    bandwidth: () => number;\n  };\n  const yScale = useYScale(\"price\") as unknown as (v: number) => number;\n  const bandwidth = xScale.bandwidth();\n  const tickWidth = Math.min(bandwidth * 0.4, 11);\n\n  return (\n    <g>\n      {dates.map((date, i) => {\n        const center = xScale(date) + bandwidth / 2;\n        const isUp = closes[i] >= opens[i];\n        const color = isUp ? t.palette[0] : t.palette[4];\n\n        return (\n          <g key={date} stroke={color} strokeWidth={2.4} strokeLinecap=\"round\">\n            <line x1={center} x2={center} y1={yScale(highs[i])} y2={yScale(lows[i])} />\n            <line x1={center - tickWidth} x2={center} y1={yScale(opens[i])} y2={yScale(opens[i])} />\n            <line x1={center} x2={center + tickWidth} y1={yScale(closes[i])} y2={yScale(closes[i])} />\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const chartHeight = window.ANYPLOT_SIZE.height - TITLE_HEIGHT;\n\n  return (\n    <div\n      style={{\n        width: window.ANYPLOT_SIZE.width,\n        height: window.ANYPLOT_SIZE.height,\n        backgroundColor: t.pageBg,\n        fontFamily: \"Inter, system-ui, sans-serif\",\n      }}\n    >\n      <div\n        style={{\n          height: TITLE_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"space-between\",\n          paddingLeft: 24,\n          paddingRight: 40,\n        }}\n      >\n        <span style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</span>\n        <div style={{ display: \"flex\", alignItems: \"center\", gap: 20 }}>\n          {legendItems.map((item) => (\n            <div key={item.label} style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n              <span\n                style={{\n                  width: 18,\n                  height: 3,\n                  borderRadius: 2,\n                  backgroundColor: item.color,\n                }}\n              />\n              <span style={{ fontSize: 14, color: t.inkSoft }}>{item.label}</span>\n            </div>\n          ))}\n        </div>\n      </div>\n      <ChartsContainer\n        width={window.ANYPLOT_SIZE.width}\n        height={chartHeight}\n        series={[]}\n        skipAnimation\n        margin={{ top: 18, right: 36, bottom: 14, left: 18 }}\n        xAxis={[\n          {\n            id: \"sessions\",\n            data: dates,\n            scaleType: \"band\",\n            height: 58,\n            label: xAxisLabel,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            tickLabelInterval: (_value: unknown, index: number) =>\n              index % TICK_INTERVAL_STEP === 0,\n            valueFormatter: (iso: string) => {\n              const parts = String(iso).split(\"-\");\n              if (parts.length === 3) {\n                const m = Number(parts[1]) - 1;\n                const d = Number(parts[2]);\n                return `${MONTHS[m]} ${d}`;\n              }\n              return String(iso);\n            },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"price\",\n            min: yMin - yPad,\n            max: yMax + yPad,\n            width: 88,\n            label: PRICE_AXIS_LABEL,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            valueFormatter: (v: number) => v.toFixed(0),\n          },\n        ]}\n      >\n        <ChartsGrid\n          horizontal\n          sx={{\n            \"& .MuiChartsGrid-line\": {\n              stroke: t.grid,\n              opacity: 0.55,\n              strokeDasharray: \"2 5\",\n            },\n          }}\n        />\n        <OhlcBars />\n        <ChartsXAxis axisId=\"sessions\" />\n        <ChartsYAxis axisId=\"price\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}