{"spec_id":"renko-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// renko-basic: Basic Renko Chart\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/100 | Updated: 2026-11-08\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 BRICK_SHARE = 0.055; // brick size as a share of the close-price range, rounded to two significant digits\nconst PAD_SHARE = 0.06; // price-axis padding, share of the brick span\nconst BRICK_WIDTH_SHARE = 0.78; // brick width as a share of its column; the rest is the gap\nconst TICK_COUNT = 10; // about this many brick-index labels on the x axis\nconst DIRECTIONS = [\n  { sign: 1, label: \"Bullish brick (price up)\", color: t.palette[0] },\n  { sign: -1, label: \"Bearish brick (price down)\", color: t.palette[4] },\n]; // finance up/down semantics: brand green up, the palette's red down\nconst TITLE_H = 48;\nconst SUBTITLE_H = 28;\nconst LEGEND_H = 44;\n\n// Columns: set these to your data's column names\nconst DATE = \"Date\"; // role: date\nconst CLOSE = \"Close (USD)\"; // role: close\n\n// Data: replace this block with your own records, one object per row\n// Daily closes of a fictional solar-energy stock through four regimes (uptrend, consolidation,\n// downtrend, recovery), 241 trading days (weekdays) from 2 Jan 2024, from a fixed-seed LCG\nconst data = (() => {\n  let seed = 42;\n  const rand = () => {\n    seed = (seed * 1103515245 + 12345) & 0x7fffffff;\n    return seed / 0x7fffffff;\n  };\n  const drifts = [...Array(60).fill(0.34), ...Array(50).fill(0), ...Array(70).fill(-0.3), ...Array(60).fill(0.26)];\n  const rows = [{ [DATE]: new Date(2024, 0, 2), [CLOSE]: 64 }];\n  const day = new Date(2024, 0, 2);\n  drifts.forEach((drift) => {\n    do day.setDate(day.getDate() + 1);\n    while (day.getDay() === 0 || day.getDay() === 6);\n    const prev = rows[rows.length - 1][CLOSE];\n    rows.push({ [DATE]: new Date(day), [CLOSE]: Math.max(prev + drift + (rand() - 0.5) * 1.6, 5) });\n  });\n  return rows.map((r) => ({ ...r, [CLOSE]: Math.round(r[CLOSE] * 100) / 100 }));\n})();\n\n// Plot: reads only data and the constants above\nconst rows = data.filter((d) => Number.isFinite(d[CLOSE])).sort((a, b) => a[DATE] - b[DATE]);\nconst closes = rows.map((d) => d[CLOSE]);\nconst brickSize = Number(((Math.max(...closes) - Math.min(...closes)) * BRICK_SHARE).toPrecision(2));\n\n// A brick is drawn each time the close moves one full brick above the last brick's top or below\n// its bottom, so a reversal starts one brick beyond the previous brick, never level with it\nconst bricks = [];\nlet bottom = Math.round(closes[0] / brickSize) * brickSize;\nlet top = bottom;\nrows.slice(1).forEach((d) => {\n  while (d[CLOSE] >= top + brickSize) {\n    bricks.push({ base: top, sign: 1, date: d[DATE] });\n    bottom = top;\n    top += brickSize;\n  }\n  while (d[CLOSE] <= bottom - brickSize) {\n    bricks.push({ base: bottom - brickSize, sign: -1, date: d[DATE] });\n    top = bottom;\n    bottom -= brickSize;\n  }\n});\nconst columns = bricks.map((_, i) => i + 1);\nconst low = Math.min(...bricks.map((b) => b.base));\nconst high = Math.max(...bricks.map((b) => b.base)) + brickSize;\nconst pad = (high - low) * PAD_SHARE;\nconst tickEvery = Math.max(1, Math.ceil(bricks.length / TICK_COUNT));\n\nconst unit = CLOSE.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst dateFormat = { month: \"short\", day: \"numeric\", year: \"numeric\" };\nconst firstDate = rows[0][DATE].toLocaleDateString(\"en-US\", dateFormat);\nconst lastDate = rows[rows.length - 1][DATE].toLocaleDateString(\"en-US\", dateFormat);\nconst TITLE = \"SolarGrid Energy · renko-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${rows.length} closes ${firstDate} – ${lastDate} · brick size ${brickSize} ${unit} · ${bricks.length} bricks`;\n\nfunction Bricks() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const column = xScale.bandwidth();\n  const width = column * BRICK_WIDTH_SHARE;\n\n  return (\n    <g>\n      {bricks.map((b, i) => (\n        <rect\n          key={i}\n          x={xScale(columns[i]) + (column - width) / 2}\n          y={yScale(b.base + brickSize)}\n          width={width}\n          height={yScale(b.base) - yScale(b.base + brickSize)}\n          fill={DIRECTIONS.find((dir) => dir.sign === b.sign).color}\n        />\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: TITLE_H, display: \"flex\", alignItems: \"flex-end\", justifyContent: \"center\", fontSize: 22, fontWeight: 600, color: t.ink }}>\n        {TITLE}\n      </div>\n      <div style={{ height: SUBTITLE_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\", fontSize: 15, color: t.inkSoft }}>\n        {SUBTITLE}\n      </div>\n      <ChartsContainer\n        width={width}\n        height={height - TITLE_H - SUBTITLE_H - LEGEND_H}\n        skipAnimation\n        series={[]}\n        margin={{ top: 16, right: 40, bottom: 8, left: 16 }}\n        xAxis={[\n          {\n            scaleType: \"band\",\n            data: columns,\n            label: \"Brick sequence (date the brick formed)\",\n            height: 64,\n            valueFormatter: (n) => bricks[n - 1].date.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\" }),\n            tickLabelInterval: (_value, index) => index % tickEvery === 0,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"linear\",\n            min: low - pad,\n            max: high + pad,\n            label: CLOSE,\n            width: 76,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        sx={{\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      >\n        <ChartsGrid horizontal />\n        <Bricks />\n        <ChartsXAxis />\n        <ChartsYAxis />\n      </ChartsContainer>\n      <div style={{ height: LEGEND_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\", gap: 32 }}>\n        {DIRECTIONS.map((dir) => (\n          <div key={dir.sign} style={{ display: \"flex\", alignItems: \"center\", gap: 8, fontSize: 15, color: t.ink }}>\n            <div style={{ width: 16, height: 16, borderRadius: 2, background: dir.color }} />\n            {dir.label}\n          </div>\n        ))}\n      </div>\n    </div>\n  );\n}\n"}