{"spec_id":"area-mountain-panorama","library":"muix","language":"javascript","code":"// anyplot.ai\n// area-mountain-panorama: Mountain Panorama Profile with Labeled Peaks\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 89/100 | Updated: 2026-11-08\n//# anyplot-orientation: landscape\n\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { useDrawingArea, useXScale, useYScale } from \"@mui/x-charts/hooks\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"area-mountain-panorama · javascript · muix · anyplot.ai\";\nconst TITLE_H = 84;\nconst Y_PAD_SHARE = 0.06; // room below the lowest skyline sample, as a share of the elevation range\nconst LABEL_HEADROOM_SHARE = 0.28; // sky above the highest summit for the label shelves\nconst SILHOUETTE_SHADE = 0.32; // the silhouette fill is the brand green darkened to this share of its brightness\nconst Y_TICK_TARGET = 15; // about this many tick steps across the whole elevation axis\nconst SHELF_H = 44; // vertical step between label shelves (px)\nconst LABEL_GAP_PX = 16; // minimum clear space between two labels on one shelf\nconst CHAR_W_PX = 8.6; // approximate width of one 15 px bold label character\nconst CHAR_W_ANCHOR_PX = 10; // the same at the anchor summit's 17 px\n\n// Columns: set these to your data's column names\nconst BEARING = \"Bearing (°)\"; // role: angle_deg\nconst ELEVATION = \"Elevation (m)\"; // role: elevation_m\n// A name on a sample marks a summit whose apex is that sample's bearing and elevation\nconst PEAK = \"Peak\"; // role: peaks\nconst ANCHOR = \"Anchor summit\"; // role: extra\nconst BACKDROP = \"Background ridge (m)\"; // role: extra\n\n// Data: replace this block with your own records, one object per row\n// Wallis (Valais) panorama from the Gornergrat ridge, anchored on the Matterhorn.\n// Each ridge is built from control points (col, apex, col per summit, flank widths\n// and saddle depths varied per peak) and roughened by midpoint displacement whose\n// jitter scales with each segment's span, so apexes stay sharp while long saddles\n// pick up notches. A lower, hazier background ridge, which rises behind the front\n// ridge's saddles, is resampled onto the same bearings. Seeded mulberry32, since\n// the browser has no seeded random generator.\nconst data = (() => {\n  const peaks = [\n    { name: \"Weisshorn\", angle: -62, elev: 4506 },\n    { name: \"Zinalrothorn\", angle: -54, elev: 4221 },\n    { name: \"Ober Gabelhorn\", angle: -47, elev: 4063 },\n    { name: \"Dent Blanche\", angle: -39, elev: 4358 },\n    { name: \"Matterhorn\", angle: -28, elev: 4478 },\n    { name: \"Dom\", angle: -14, elev: 4545 },\n    { name: \"Täschhorn\", angle: -6, elev: 4491 },\n    { name: \"Alphubel\", angle: 3, elev: 4206 },\n    { name: \"Allalinhorn\", angle: 11, elev: 4027 },\n    { name: \"Rimpfischhorn\", angle: 19, elev: 4199 },\n    { name: \"Strahlhorn\", angle: 27, elev: 4190 },\n    { name: \"Breithorn\", angle: 36, elev: 4164 },\n    { name: \"Pollux\", angle: 44, elev: 4092 },\n    { name: \"Castor\", angle: 50, elev: 4223 },\n    { name: \"Liskamm\", angle: 57, elev: 4527 },\n    { name: \"Monte Rosa\", angle: 64, elev: 4634 },\n  ];\n  const anchor = \"Matterhorn\";\n  const baseline = 2650; // valley-floor elevation at the panorama's flat edges\n  const xMin = peaks[0].angle - 11;\n  const xMax = peaks[peaks.length - 1].angle + 11;\n\n  const [front, back] = [\n    { summits: peaks, seed: 20260820, prominence: [380, 620], depth: 5, amp: 40, persistence: 0.58 },\n    {\n      summits: peaks.map((p) => ({ angle: p.angle + 4, elev: baseline + (p.elev - baseline) * 0.88 })),\n      seed: 777,\n      prominence: [200, 520],\n      depth: 4,\n      amp: 26,\n      persistence: 0.55,\n    },\n  ].map(({ summits, seed, prominence, depth, amp, persistence }) => {\n    let state = seed;\n    const rng = () => {\n      state |= 0;\n      state = (state + 0x6d2b79f5) | 0;\n      let x = Math.imul(state ^ (state >>> 15), 1 | state);\n      x = (x + Math.imul(x ^ (x >>> 7), 61 | x)) ^ x;\n      return ((x ^ (x >>> 14)) >>> 0) / 4294967296;\n    };\n    let pts = [{ angle: xMin, elev: baseline }];\n    summits.forEach((p, i) => {\n      const prevAngle = i === 0 ? xMin : summits[i - 1].angle;\n      const nextAngle = i === summits.length - 1 ? xMax : summits[i + 1].angle;\n      const widthBefore = Math.min(1.4 + rng() * 3.8, (p.angle - prevAngle) * 0.42);\n      const widthAfter = Math.min(1.4 + rng() * 3.8, (nextAngle - p.angle) * 0.42);\n      const drop = prominence[0] + rng() * (prominence[1] - prominence[0]);\n      const colElev = Math.max(baseline, p.elev - drop);\n      pts.push({ angle: p.angle - widthBefore, elev: colElev + (rng() - 0.5) * 90 });\n      pts.push({ angle: p.angle, elev: p.elev });\n      pts.push({ angle: p.angle + widthAfter, elev: colElev + (rng() - 0.5) * 90 });\n    });\n    pts.push({ angle: xMax, elev: baseline });\n    let jitterAmp = amp;\n    for (let d = 0; d < depth; d += 1) {\n      const next = [pts[0]];\n      for (let i = 0; i < pts.length - 1; i += 1) {\n        const a = pts[i];\n        const b = pts[i + 1];\n        const jitter = (rng() - 0.5) * 2 * jitterAmp * (b.angle - a.angle);\n        next.push({ angle: (a.angle + b.angle) / 2, elev: Math.max(baseline - 40, (a.elev + b.elev) / 2 + jitter) });\n        next.push(b);\n      }\n      pts = next;\n      jitterAmp *= persistence;\n    }\n    return pts;\n  });\n\n  return front.map(({ angle, elev }) => {\n    let j = 0;\n    while (j < back.length - 2 && back[j + 1].angle <= angle) j += 1;\n    const f = Math.max(0, Math.min(1, (angle - back[j].angle) / (back[j + 1].angle - back[j].angle)));\n    const peak = peaks.find((p) => p.angle === angle);\n    return {\n      [BEARING]: Number(angle.toFixed(3)),\n      [ELEVATION]: Math.round(elev),\n      [PEAK]: peak ? peak.name : null,\n      [ANCHOR]: peak ? peak.name === anchor : false,\n      [BACKDROP]: Math.round(back[j].elev + (back[j + 1].elev - back[j].elev) * f),\n    };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst rows = [...data].sort((a, b) => a[BEARING] - b[BEARING]);\nconst bearings = rows.map((d) => d[BEARING]);\nconst skyline = rows.map((d) => d[ELEVATION]);\nconst backdrop = rows.map((d) => d[BACKDROP]);\nconst peaks = rows.filter((d) => d[PEAK]);\nconst anchorIdx = peaks.findIndex((d) => d[ANCHOR]);\n\nconst known = [...skyline, ...backdrop].filter((v) => v != null);\nconst elevMin = Math.min(...known);\nconst elevMax = Math.max(...known);\nconst elevSpan = elevMax - elevMin || 1;\nconst yLo = elevMin - Y_PAD_SHARE * elevSpan;\nconst yHi = elevMax + LABEL_HEADROOM_SHARE * elevSpan;\n\n// Round tick step from the axis range; ticks and grid stop at the highest summit\n// so the label sky stays clear\nconst rawStep = (yHi - yLo) / Y_TICK_TARGET;\nconst stepMag = 10 ** Math.floor(Math.log10(rawStep));\nconst yStep = ([1, 2, 2.5, 5, 10].find((m) => m * stepMag >= rawStep) ?? 10) * stepMag;\nconst yTicks: number[] = [];\nfor (let v = Math.ceil(yLo / yStep) * yStep; v <= elevMax; v += yStep) yTicks.push(v);\n\nconst BEARING_UNIT = BEARING.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst ELEV_UNIT = ELEVATION.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst fmtElev = (v: number) => `${Math.round(v).toLocaleString(\"en-US\")} ${ELEV_UNIT}`.trim();\n// Dusk silhouette: a deep shade of the brand green, the same in both themes, outlined\n// in the full brand green so the ridgeline still stands out against the dark page\nconst silhouette = `#${(t.palette[0] as string)\n  .slice(1)\n  .match(/../g)!\n  .map((c) => Math.round(parseInt(c, 16) * SILHOUETTE_SHADE).toString(16).padStart(2, \"0\"))\n  .join(\"\")}`;\nconst highest = peaks.reduce((best, d) => (d[ELEVATION] > best[ELEVATION] ? d : best), peaks[0]);\n\nconst SUBTITLE = [\n  `${peaks.length} named summits`,\n  `${Math.round(bearings[bearings.length - 1] - bearings[0])}${BEARING_UNIT} field of view`,\n  highest ? `highest: ${highest[PEAK]}, ${fmtElev(highest[ELEVATION])}` : null,\n]\n  .filter(Boolean)\n  .join(\" · \");\n\n// Summit labels on shelves in the sky: as many shelves as it takes for labels that\n// share one to clear each other, then each leader line drops to its apex\nfunction PeakLabels() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const { top } = useDrawingArea();\n\n  const xs = peaks.map((d) => xScale(d[BEARING]));\n  const widths = peaks.map((d, i) => String(d[PEAK]).length * (i === anchorIdx ? CHAR_W_ANCHOR_PX : CHAR_W_PX));\n  let shelves = 1;\n  while (\n    shelves < peaks.length &&\n    xs.some((x, i) => i + shelves < xs.length && xs[i + shelves] - x < (widths[i] + widths[i + shelves]) / 2 + LABEL_GAP_PX)\n  ) {\n    shelves += 1;\n  }\n\n  return (\n    <g>\n      {peaks.map((d, i) => {\n        const isAnchor = i === anchorIdx;\n        const x = xs[i];\n        const apexY = yScale(d[ELEVATION]);\n        const labelY = top + 18 + (i % shelves) * SHELF_H;\n        return (\n          <g key={`${d[PEAK]}-${d[BEARING]}`}>\n            <line\n              x1={x}\n              x2={x}\n              y1={labelY + 26}\n              y2={apexY - (isAnchor ? 8 : 5)}\n              stroke={isAnchor ? t.ink : t.inkSoft}\n              strokeWidth={isAnchor ? 2 : 1}\n              strokeOpacity={isAnchor ? 0.9 : 0.6}\n            />\n            <circle\n              cx={x}\n              cy={apexY}\n              r={isAnchor ? 6 : 3.5}\n              fill={t.palette[0]}\n              stroke={isAnchor ? t.ink : t.pageBg}\n              strokeWidth={isAnchor ? 2 : 1.5}\n            />\n            <text\n              x={x}\n              y={labelY}\n              textAnchor=\"middle\"\n              fontSize={isAnchor ? 17 : 15}\n              fontWeight={isAnchor ? 800 : 700}\n              fill={t.ink}\n            >\n              {d[PEAK]}\n            </text>\n            <text\n              x={x}\n              y={labelY + 18}\n              textAnchor=\"middle\"\n              fontSize={isAnchor ? 15 : 14}\n              fontWeight={isAnchor ? 700 : 400}\n              fill={t.inkSoft}\n            >\n              {fmtElev(d[ELEVATION])}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <Box sx={{ width, height, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <Box sx={{ height: TITLE_H, px: \"40px\", pt: \"18px\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ color: t.ink, fontSize: \"22px\", fontWeight: 600, lineHeight: 1.2 }}>\n          {TITLE}\n        </Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: \"15px\", lineHeight: 1.2, mt: \"8px\" }}>\n          {SUBTITLE}\n        </Typography>\n      </Box>\n      <LineChart\n        width={width}\n        height={height - TITLE_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true }}\n        xAxis={[\n          {\n            data: bearings,\n            scaleType: \"linear\",\n            min: bearings[0],\n            max: bearings[bearings.length - 1],\n            label: BEARING,\n            position: \"none\",\n          },\n        ]}\n        yAxis={[\n          {\n            label: ELEVATION,\n            min: yLo,\n            max: yHi,\n            width: 84,\n            tickInterval: yTicks,\n            valueFormatter: (v: number) => v.toLocaleString(\"en-US\"),\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={[\n          {\n            id: \"haze\",\n            data: backdrop,\n            label: BACKDROP,\n            color: t.inkSoft,\n            area: true,\n            baseline: \"min\",\n            showMark: false,\n            curve: \"linear\",\n          },\n          {\n            id: \"ridge\",\n            data: skyline,\n            label: ELEVATION,\n            color: t.palette[0],\n            area: true,\n            baseline: \"min\",\n            showMark: false,\n            curve: \"linear\",\n          },\n        ]}\n        margin={{ top: 12, right: 40, bottom: 24, left: 24 }}\n        sx={{\n          '& .MuiLineChart-area[data-series=\"haze\"]': { fillOpacity: 0.22 },\n          // lines are drawn above every area, so the haze keeps no outline across the front ridge\n          '& .MuiLineChart-line[data-series=\"haze\"]': { display: \"none\" },\n          // MUI brightens area fills by default; the silhouette keeps its dark solid fill\n          '& .MuiLineChart-area[data-series=\"ridge\"]': { fill: silhouette, fillOpacity: 1, filter: \"none\" },\n          '& .MuiLineChart-line[data-series=\"ridge\"]': { strokeWidth: 2.5 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      >\n        <PeakLabels />\n      </LineChart>\n    </Box>\n  );\n}\n"}