{"spec_id":"line-styled","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-styled: Styled Line Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-23\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants: the standard line styles, handed out in order (solid, dashed,\n// dotted, dash-dot) so the chart stays legible when printed in monochrome.\n// The dotted pattern is a touch dense so it keeps reading as dots at crossings.\nconst DASHES = [\"none\", \"11 7\", \"3 5\", \"13 5 2 5\", \"18 6\", \"2 4 8 4\"];\nconst LINE_WIDTH = 3;\n\n// Columns: set these to your data's column names\nconst MINUTE = \"Time (minutes)\"; // role: x\nconst LATENCY = [\"p50\", \"p90\", \"p99\", \"p99.9\"]; // role: y\nconst Y_LABEL = \"Response Latency (ms)\";\n\n// Data: replace this block with your own records, one object per row\nfunction mulberry32(seed: number) {\n  return () => {\n    seed |= 0;\n    seed = (seed + 0x6d2b79f5) | 0;\n    let x = Math.imul(seed ^ (seed >>> 15), 1 | seed);\n    x = (x + Math.imul(x ^ (x >>> 7), 61 | x)) ^ x;\n    return ((x ^ (x >>> 14)) >>> 0) / 4294967296;\n  };\n}\nconst rand = mulberry32(42);\nconst data: Record<string, number>[] = [];\nlet baseline = 42;\nfor (let i = 0; i < 60; i += 1) {\n  baseline += (rand() - 0.5) * 4;\n  baseline = Math.min(58, Math.max(32, baseline));\n  const tailGap = 14 + rand() * 8;\n  const deepTailGap = 30 + rand() * 14 + Math.sin(i / 6) * 6;\n  const spike = i % 17 === 0 ? 55 + rand() * 35 : rand() * 10;\n  const round = (v: number) => Math.round(v * 10) / 10;\n  data.push({\n    [MINUTE]: i,\n    p50: round(baseline),\n    p90: round(baseline + tailGap),\n    p99: round(baseline + tailGap + deepTailGap),\n    \"p99.9\": round(baseline + tailGap + deepTailGap + spike),\n  });\n}\n\n// Plot: reads only data and the constants above\n// Focal point: the single largest value of the deepest-tail (last) series.\nconst tail = LATENCY[LATENCY.length - 1];\nconst peak = data.reduce((best, d) => ((d[tail] ?? -Infinity) > (best[tail] ?? -Infinity) ? d : best));\n\n// Legend swatch drawn by the native legend: a line sample in the series' own\n// dash pattern, long enough to show the full pattern (MUI X's built-in \"line\"\n// mark is solid only).\nfunction dashMark(dash: string) {\n  return function DashMark({ color }: { color?: string }) {\n    return (\n      <svg viewBox=\"0 0 44 14\" width={44} height={14} style={{ display: \"block\" }}>\n        <line x1={0} y1={7} x2={44} y2={7} stroke={color} strokeWidth={LINE_WIDTH} strokeDasharray={dash} />\n      </svg>\n    );\n  };\n}\n\n// One stroke width for every style; the dash pattern is set per series via\n// the v9 `data-series` attribute on each line path.\nconst lineSx = {\n  \"& .MuiLineChart-line\": { strokeWidth: LINE_WIDTH },\n  ...Object.fromEntries(\n    LATENCY.map((key, i) => [`& .MuiLineChart-line[data-series=\"${key}\"]`, { strokeDasharray: DASHES[i] }]),\n  ),\n};\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const titleHeight = 56;\n  const legendHeight = 48;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div\n        style={{\n          height: titleHeight,\n          display: \"flex\",\n          alignItems: \"center\",\n          paddingLeft: 8,\n          fontSize: 22,\n          fontWeight: 500,\n          color: t.ink,\n          fontFamily: \"system-ui, sans-serif\",\n        }}\n      >\n        line-styled · javascript · muix · anyplot.ai\n      </div>\n      <LineChart\n        width={width}\n        height={height - titleHeight - legendHeight}\n        skipAnimation\n        dataset={data}\n        xAxis={[\n          {\n            dataKey: MINUTE,\n            label: MINUTE,\n            scaleType: \"linear\",\n            height: 64,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            label: Y_LABEL,\n            width: 84,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        series={LATENCY.map((key, i) => ({\n          id: key,\n          dataKey: key,\n          label: `${key} latency`,\n          color: t.palette[i],\n          curve: \"monotoneX\",\n          showMark: false,\n          labelMarkType: dashMark(DASHES[i]),\n        }))}\n        grid={{ horizontal: true }}\n        margin={{ top: 20, right: 32, bottom: 8, left: 12 }}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"bottom\", horizontal: \"center\" },\n            sx: {\n              fontSize: 14,\n              color: t.inkSoft,\n              gap: 4,\n              columnGap: 4,\n              pt: 1.5,\n              // MUI X clamps legend marks to a 14 px square; widen it so the\n              // swatch shows the whole dash pattern.\n              \"& .MuiChartsLegend-mark\": { width: 44, height: 14 },\n            },\n          },\n        }}\n        sx={{\n          ...lineSx,\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      >\n        <ChartsReferenceLine\n          x={peak[MINUTE]}\n          label={`peak ${tail} · ${peak[tail]}ms`}\n          labelAlign=\"start\"\n          spacing={{ x: -150, y: 10 }}\n          lineStyle={{ stroke: t.inkSoft, strokeDasharray: \"4 4\" }}\n          labelStyle={{ fontSize: 13, fill: t.inkSoft }}\n        />\n      </LineChart>\n    </div>\n  );\n}\n"}