{"spec_id":"windbarb-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// windbarb-basic: Wind Barb Plot for Meteorological Data\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 89/100 | Updated: 2026-11-08\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\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\n// Standard barb notation, counted in knots: a half barb is 5 kt, a full barb\n// 10 kt, a pennant 50 kt; below 2.5 kt the wind is calm.\nconst KNOTS = \"kt\";\nconst CALM_KT = 2.5;\nconst HALF_KT = 5;\nconst FULL_KT = 10;\nconst PENNANT_KT = 50;\nconst KEY_SPEEDS = [0, HALF_KT, 2 * FULL_KT + HALF_KT, PENNANT_KT]; // key glyphs, wind from the north\n// Feather direction, turned clockwise on screen from the staff's outward\n// direction: Northern Hemisphere feathers point toward low pressure, on the\n// staff's left seen downwind (east for a north wind), and lean out to the tip.\nconst BARB_ANGLE_DEG = 68;\nconst STAFF_PX = 46;\nconst FEATHER_PX = 16;\nconst FEATHER_GAP_PX = 8;\nconst LONE_HALF_GAPS = 1.5; // a lone half barb sits this many gaps in from the tip, so it is not read as a full barb\nconst PENNANT_BASE_PX = 12;\nconst CALM_R = 5.5;\nconst POS_PAD = 0.125; // share of the station extent added on every side\nconst TICK_PX = 130; // target spacing between axis ticks, the same on both axes\nconst BRAND = t.palette[0];\nconst TITLE = \"windbarb-basic · javascript · muix · anyplot.ai\";\nconst TITLE_H = 80;\nconst KEY_W = 200;\nconst X_AXIS_H = 72;\nconst Y_AXIS_W = 88;\nconst MARGIN = { top: 24, right: 24, bottom: 12, left: 24 };\n\n// Columns: set these to your data's column names\nconst X = \"Distance east (km)\"; // role: x\nconst Y = \"Distance north (km)\"; // role: y\nconst U = \"Eastward wind u (kt)\"; // role: u\nconst V = \"Northward wind v (kt)\"; // role: v\n\n// Data: replace this block with your own records, one object per row\n// Surface wind observations around a low-pressure system: a 6 × 5 station\n// grid plus one station in the calm eye, from a Rankine-like vortex (58 kt\n// peak at 180 km) with 22° of cross-isobar inflow.\nconst data = (() => {\n  const cx = 400;\n  const cy = 300;\n  const inflow = (22 * Math.PI) / 180;\n  const points = [];\n  for (let x = 0; x <= 800; x += 160) for (let y = 0; y <= 600; y += 150) points.push([x, y]);\n  points.push([cx + 2 * Math.cos((35 * Math.PI) / 180), cy + 2 * Math.sin((35 * Math.PI) / 180)]);\n  return points.map(([x, y]) => {\n    const r = Math.hypot(x - cx, y - cy);\n    const dirE = Math.cos(inflow) * (-(y - cy) / r) + Math.sin(inflow) * (-(x - cx) / r);\n    const dirN = Math.cos(inflow) * ((x - cx) / r) + Math.sin(inflow) * (-(y - cy) / r);\n    const speed = 58 * (r / 180) * Math.exp(1 - r / 180);\n    return {\n      [X]: Math.round(x * 10) / 10,\n      [Y]: Math.round(y * 10) / 10,\n      [U]: Math.round(speed * dirE * 100) / 100,\n      [V]: Math.round(speed * dirN * 100) / 100,\n    };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst stations = data.filter((d) => d[X] != null && d[Y] != null && d[U] != null && d[V] != null);\nconst xs = stations.map((d) => d[X]);\nconst ys = stations.map((d) => d[Y]);\nconst xMid = (Math.min(...xs) + Math.max(...xs)) / 2;\nconst yMid = (Math.min(...ys) + Math.max(...ys)) / 2;\nconst xNeed = (Math.max(...xs) - Math.min(...xs)) * (1 + 2 * POS_PAD);\nconst yNeed = (Math.max(...ys) - Math.min(...ys)) * (1 + 2 * POS_PAD);\n\n// Glyph geometry in px around the station: the staff points into the wind,\n// and the speed, rounded to the nearest half barb, is spent from the tip\n// inward on pennants, then full barbs, then at most one half barb.\nconst featherAngle = (BARB_ANGLE_DEG * Math.PI) / 180;\nconst glyphs = [...stations.map((d) => [d[U], d[V]]), ...KEY_SPEEDS.map((s) => [0, -s])].map(([u, v]) => {\n  const speed = Math.hypot(u, v);\n  if (speed < CALM_KT) return null;\n  const ox = -u / speed;\n  const oy = v / speed;\n  const fx = ox * Math.cos(featherAngle) - oy * Math.sin(featherAngle);\n  const fy = ox * Math.sin(featherAngle) + oy * Math.cos(featherAngle);\n  const tipX = ox * STAFF_PX;\n  const tipY = oy * STAFF_PX;\n  const rounded = Math.round(speed / HALF_KT) * HALF_KT;\n  const pennantCount = Math.floor(rounded / PENNANT_KT);\n  const fullCount = Math.floor((rounded - pennantCount * PENNANT_KT) / FULL_KT);\n  const hasHalf = rounded - pennantCount * PENNANT_KT - fullCount * FULL_KT >= HALF_KT;\n  const pennants = Array.from({ length: pennantCount }, (_, p) => {\n    const ax = tipX - ox * p * PENNANT_BASE_PX;\n    const ay = tipY - oy * p * PENNANT_BASE_PX;\n    const bx = tipX - ox * (p + 1) * PENNANT_BASE_PX;\n    const by = tipY - oy * (p + 1) * PENNANT_BASE_PX;\n    return `${ax},${ay} ${bx},${by} ${(ax + bx) / 2 + fx * FEATHER_PX},${(ay + by) / 2 + fy * FEATHER_PX}`;\n  });\n  const firstGap = pennantCount > 0 ? 0.5 : fullCount === 0 ? LONE_HALF_GAPS : 0;\n  const feathers = Array.from({ length: fullCount + (hasHalf ? 1 : 0) }, (_, k) => {\n    const along = pennantCount * PENNANT_BASE_PX + (k + firstGap) * FEATHER_GAP_PX;\n    const sx = tipX - ox * along;\n    const sy = tipY - oy * along;\n    const len = k < fullCount ? FEATHER_PX : FEATHER_PX / 2;\n    return [sx, sy, sx + fx * len, sy + fy * len];\n  });\n  return { tipX, tipY, pennants, feathers };\n});\n// One drawing per glyph, centered on (0, 0): the station layer and the key\n// both place these.\nconst glyphMarks = glyphs.map((g, i) =>\n  g == null ? (\n    <circle key={i} r={CALM_R} fill=\"none\" stroke={BRAND} strokeWidth={2.5} />\n  ) : (\n    <g key={i}>\n      <circle r={2.2} fill={BRAND} />\n      <line x2={g.tipX} y2={g.tipY} stroke={BRAND} strokeWidth={3} strokeLinecap=\"round\" />\n      {g.pennants.map((points, k) => (\n        <polygon key={`p${k}`} points={points} fill={BRAND} />\n      ))}\n      {g.feathers.map(([x1, y1, x2, y2], k) => (\n        <line key={`f${k}`} x1={x1} y1={y1} x2={x2} y2={y2} stroke={BRAND} strokeWidth={3.2} strokeLinecap=\"round\" />\n      ))}\n    </g>\n  ),\n);\nconst keyLabels = KEY_SPEEDS.map((s) => (s < CALM_KT ? `Calm (< ${CALM_KT} ${KNOTS})` : `${s} ${KNOTS}`));\nconst SUBTITLE = `${stations.length} stations · each staff points to where the wind blows from · speed rounded to ${HALF_KT} ${KNOTS}`;\n\n// The community package has no barb glyph: a custom layer places each one at\n// its station through the chart's linear scales.\nfunction WindBarbLayer() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n\n  return (\n    <g>\n      {stations.map((d, i) => (\n        <g key={i} transform={`translate(${xScale(d[X])},${yScale(d[Y])})`}>\n          {glyphMarks[i]}\n        </g>\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  // Equal km per pixel on both axes, so distances and directions keep their\n  // shape: the binding axis fills its room, the other one shrinks to the\n  // stations' extent, and the chart and key sit centred on the page\n  const roomW = width - KEY_W - MARGIN.left - MARGIN.right - Y_AXIS_W;\n  const roomH = height - TITLE_H - MARGIN.top - MARGIN.bottom - X_AXIS_H;\n  const kmPerPx = Math.max(xNeed / roomW, yNeed / roomH);\n  const drawW = Math.floor(xNeed / kmPerPx);\n  const drawH = Math.floor(yNeed / kmPerPx);\n  const chartW = drawW + MARGIN.left + MARGIN.right + Y_AXIS_W;\n  const chartH = drawH + MARGIN.top + MARGIN.bottom + X_AXIS_H;\n\n  return (\n    <div style={{ width, height, background: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: TITLE_H, padding: \"18px 36px 0\", boxSizing: \"border-box\" }}>\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.2 }}>{TITLE}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft, lineHeight: 1.2, marginTop: 8 }}>{SUBTITLE}</div>\n      </div>\n      <div style={{ display: \"flex\", justifyContent: \"center\" }}>\n        <ChartsContainer\n          width={chartW}\n          height={chartH}\n          series={[]}\n          skipAnimation\n          margin={MARGIN}\n          xAxis={[\n            {\n              id: \"x\",\n              scaleType: \"linear\",\n              min: xMid - (kmPerPx * drawW) / 2,\n              max: xMid + (kmPerPx * drawW) / 2,\n              tickNumber: Math.ceil(drawW / TICK_PX),\n              height: X_AXIS_H,\n              label: X,\n              labelStyle: { fontSize: 16, fill: t.ink },\n              tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            },\n          ]}\n          yAxis={[\n            {\n              id: \"y\",\n              scaleType: \"linear\",\n              min: yMid - (kmPerPx * drawH) / 2,\n              max: yMid + (kmPerPx * drawH) / 2,\n              tickNumber: Math.ceil(drawH / TICK_PX),\n              width: Y_AXIS_W,\n              label: Y,\n              labelStyle: { fontSize: 16, fill: t.ink },\n              tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            },\n          ]}\n          sx={{\n            \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n            \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          }}\n        >\n          <WindBarbLayer />\n          <ChartsXAxis axisId=\"x\" />\n          <ChartsYAxis axisId=\"y\" />\n        </ChartsContainer>\n        <div style={{ width: KEY_W, paddingTop: MARGIN.top, display: \"flex\", flexDirection: \"column\", gap: 14 }}>\n          <span style={{ fontSize: 16, fontWeight: 600, color: t.ink }}>Wind speed</span>\n          {keyLabels.map((label, j) => (\n            <div key={label} style={{ display: \"flex\", alignItems: \"center\", gap: 12 }}>\n              <svg width={44} height={56} style={{ overflow: \"visible\" }}>\n                <g transform={`translate(30,${glyphs[stations.length + j] == null ? 28 : 50})`}>{glyphMarks[stations.length + j]}</g>\n              </svg>\n              <span style={{ fontSize: 15, color: t.inkSoft }}>{label}</span>\n            </div>\n          ))}\n        </div>\n      </div>\n    </div>\n  );\n}\n"}