{"spec_id":"bar-3d-categorical","library":"muix","language":"javascript","code":"// anyplot.ai\n// bar-3d-categorical: 3D Bar Chart for Categorical Comparison\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { useXScale, useYScale, useDrawingArea } from \"@mui/x-charts/hooks\";\nimport { lighten, darken } from \"@mui/material/styles\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst ISO_DX = 20;\nconst ISO_DY = -14;\nconst STAGGER_DX = ISO_DX * 1.35;\nconst STAGGER_DY = ISO_DY * 1.35;\nconst Y_PAD_SHARE = 0.08;\nconst VALUE_LABEL_H = 22; // px a value label needs above a bar's top face\nconst LABEL_CHAR_PX = 8.5; // px advance of one 14 px label character\nconst ROW_LABEL_GAP = 12;\nconst EDGE_PAD = 16;\n\n// Columns: set these to your data's column names\nconst X_CATEGORY = \"Alloy\"; // role: x_category\nconst Y_CATEGORY = \"Heat treatment\"; // role: y_category\nconst VALUE = \"Tensile strength (MPa)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\nconst data = [\n  { [X_CATEGORY]: \"Al 6061\", [Y_CATEGORY]: \"Annealed\", [VALUE]: 124 },\n  { [X_CATEGORY]: \"Ti-6Al-4V\", [Y_CATEGORY]: \"Annealed\", [VALUE]: 830 },\n  { [X_CATEGORY]: \"Steel 4140\", [Y_CATEGORY]: \"Annealed\", [VALUE]: 655 },\n  { [X_CATEGORY]: \"Inconel 718\", [Y_CATEGORY]: \"Annealed\", [VALUE]: 965 },\n  { [X_CATEGORY]: \"Mg AZ31\", [Y_CATEGORY]: \"Annealed\", [VALUE]: 240 },\n  { [X_CATEGORY]: \"Al 6061\", [Y_CATEGORY]: \"Tempered\", [VALUE]: 200 },\n  { [X_CATEGORY]: \"Ti-6Al-4V\", [Y_CATEGORY]: \"Tempered\", [VALUE]: 950 },\n  { [X_CATEGORY]: \"Steel 4140\", [Y_CATEGORY]: \"Tempered\", [VALUE]: 780 },\n  { [X_CATEGORY]: \"Inconel 718\", [Y_CATEGORY]: \"Tempered\", [VALUE]: 1100 },\n  { [X_CATEGORY]: \"Mg AZ31\", [Y_CATEGORY]: \"Tempered\", [VALUE]: 250 },\n  { [X_CATEGORY]: \"Al 6061\", [Y_CATEGORY]: \"Quenched\", [VALUE]: 275 },\n  { [X_CATEGORY]: \"Ti-6Al-4V\", [Y_CATEGORY]: \"Quenched\", [VALUE]: 1100 },\n  { [X_CATEGORY]: \"Steel 4140\", [Y_CATEGORY]: \"Quenched\", [VALUE]: 900 },\n  { [X_CATEGORY]: \"Inconel 718\", [Y_CATEGORY]: \"Quenched\", [VALUE]: 1250 },\n  { [X_CATEGORY]: \"Mg AZ31\", [Y_CATEGORY]: \"Quenched\", [VALUE]: 265 },\n  { [X_CATEGORY]: \"Al 6061\", [Y_CATEGORY]: \"Aged\", [VALUE]: 310 },\n  { [X_CATEGORY]: \"Ti-6Al-4V\", [Y_CATEGORY]: \"Aged\", [VALUE]: 1170 },\n  { [X_CATEGORY]: \"Steel 4140\", [Y_CATEGORY]: \"Aged\", [VALUE]: 1080 },\n  { [X_CATEGORY]: \"Inconel 718\", [Y_CATEGORY]: \"Aged\", [VALUE]: 1400 },\n  { [X_CATEGORY]: \"Mg AZ31\", [Y_CATEGORY]: \"Aged\", [VALUE]: 290 },\n];\n\n// Plot: reads only data and the constants above\nconst alloys = [...new Set(data.map((d) => d[X_CATEGORY]))];\nconst treatments = [...new Set(data.map((d) => d[Y_CATEGORY]))];\nconst values = data.map((d) => d[VALUE]);\nconst maxVal = Math.max(...values);\nconst minVal = Math.min(...values);\nconst yMax = maxVal * (1 + Y_PAD_SHARE);\nconst formatValue = (v: number) => v.toLocaleString(\"en-US\");\nconst marginTop =\n  (treatments.length - 1) * Math.abs(STAGGER_DY) + Math.abs(ISO_DY) + VALUE_LABEL_H;\nconst rowLabelW = Math.max(...treatments.map((tr) => tr.length)) * LABEL_CHAR_PX;\nconst marginRight =\n  (treatments.length - 1) * STAGGER_DX + ISO_DX + ROW_LABEL_GAP + rowLabelW + EDGE_PAD;\n\n// Value levels: one nice step shared by the y axis, the back wall and the side wall\nconst rawStep = yMax / 8;\nconst stepMag = 10 ** Math.floor(Math.log10(rawStep));\nconst tickStep =\n  [1, 2, 2.5, 5, 10].find((m) => m * stepMag >= rawStep)! * stepMag;\nconst valueTicks = Array.from(\n  { length: Math.floor(yMax / tickStep) + 1 },\n  (_, i) => i * tickStep,\n);\n// Back wall sits behind the rear row's back faces\nconst wallDx = (treatments.length - 1) * STAGGER_DX + ISO_DX;\nconst wallDy = (treatments.length - 1) * STAGGER_DY + ISO_DY;\n\nconst rowColors = treatments.map((_, i) => t.palette[i]);\nconst topColors = rowColors.map((hex) => lighten(hex, 0.34));\nconst sideColors = rowColors.map((hex) => darken(hex, 0.3));\n\nconst paintOrder = [...treatments.keys()].sort((a, b) => b - a);\n\nfunction Bars3D() {\n  const xScale = useXScale() as any;\n  const yScale = useYScale() as any;\n  const { left, width } = useDrawingArea();\n  const bandwidth = xScale.bandwidth();\n  const barWidth = bandwidth * 0.52;\n  const baseline = yScale(0);\n\n  return (\n    <g data-drawing-container>\n      <g>\n        {valueTicks.map((v) => (\n          <line\n            key={`wall-level-${v}`}\n            x1={left}\n            y1={yScale(v)}\n            x2={left + wallDx}\n            y2={yScale(v) + wallDy}\n            stroke={t.grid}\n            strokeWidth={1}\n          />\n        ))}\n        <line\n          x1={left + wallDx}\n          y1={baseline + wallDy}\n          x2={left + wallDx}\n          y2={yScale(yMax) + wallDy}\n          stroke={t.inkSoft}\n          strokeWidth={1}\n          opacity={0.35}\n        />\n        {treatments.map((treatment, row) => {\n          const dx = row * STAGGER_DX;\n          const dy = row * STAGGER_DY;\n          return (\n            <g key={`floor-row-${treatment}`}>\n              <line\n                x1={left + dx}\n                y1={baseline + dy}\n                x2={left + width + dx}\n                y2={baseline + dy}\n                stroke={t.inkSoft}\n                strokeWidth={1.5}\n                opacity={0.35}\n              />\n              <text\n                x={left + width + dx + ISO_DX + ROW_LABEL_GAP}\n                y={baseline + dy + 4}\n                fontSize={14}\n                fontWeight={500}\n                fill={t.inkSoft}\n              >\n                {treatment}\n              </text>\n            </g>\n          );\n        })}\n        {alloys.map((alloy) => {\n          const cx = xScale(alloy) + bandwidth / 2;\n          const points = treatments\n            .map(\n              (_, row) =>\n                `${cx + row * STAGGER_DX},${baseline + row * STAGGER_DY}`,\n            )\n            .join(\" \");\n          return (\n            <polyline\n              key={`floor-col-${alloy}`}\n              points={points}\n              fill=\"none\"\n              stroke={t.inkSoft}\n              strokeWidth={1.5}\n              opacity={0.35}\n            />\n          );\n        })}\n      </g>\n      {paintOrder.map((row) => {\n        const treatment = treatments[row];\n        const dx = row * STAGGER_DX;\n        const dy = row * STAGGER_DY;\n        const color = rowColors[row];\n        const topColor = topColors[row];\n        const sideColor = sideColors[row];\n        return (\n          <g key={treatment} transform={`translate(${dx}, ${dy})`}>\n            {alloys.map((alloy) => {\n              const rowObj = data.find(\n                (d) => d[X_CATEGORY] === alloy && d[Y_CATEGORY] === treatment,\n              );\n              if (!rowObj || rowObj[VALUE] === null) return null;\n              const value = rowObj[VALUE];\n              const x = xScale(alloy) + (bandwidth - barWidth) / 2;\n              const y = yScale(value);\n              const barHeight = baseline - y;\n              const topFace = [\n                `${x},${y}`,\n                `${x + barWidth},${y}`,\n                `${x + barWidth + ISO_DX},${y + ISO_DY}`,\n                `${x + ISO_DX},${y + ISO_DY}`,\n              ].join(\" \");\n              const sideFace = [\n                `${x + barWidth},${y}`,\n                `${x + barWidth},${y + barHeight}`,\n                `${x + barWidth + ISO_DX},${y + barHeight + ISO_DY}`,\n                `${x + barWidth + ISO_DX},${y + ISO_DY}`,\n              ].join(\" \");\n              const labelCx = x + barWidth / 2 + ISO_DX / 2;\n              const labelCy = y + ISO_DY - 7;\n              const labelStr = formatValue(value);\n              const pillW = labelStr.length * LABEL_CHAR_PX + 10;\n              return (\n                <g key={alloy}>\n                  <rect\n                    x={x}\n                    y={y}\n                    width={barWidth}\n                    height={barHeight}\n                    fill={color}\n                  />\n                  <polygon points={sideFace} fill={sideColor} />\n                  <polygon points={topFace} fill={topColor} />\n                  {row < treatments.length - 1 && (\n                    <rect\n                      x={labelCx - pillW / 2}\n                      y={labelCy - 14}\n                      width={pillW}\n                      height={18}\n                      rx={3}\n                      fill={t.pageBg}\n                      fillOpacity={0.85}\n                    />\n                  )}\n                  <text\n                    x={labelCx}\n                    y={labelCy}\n                    textAnchor=\"middle\"\n                    fontSize={14}\n                    fontWeight={600}\n                    fill={t.ink}\n                  >\n                    {labelStr}\n                  </text>\n                </g>\n              );\n            })}\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nconst TITLE = \"bar-3d-categorical · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${X_CATEGORY}: ${alloys.length} · ${Y_CATEGORY}: ${treatments.length} · ${VALUE}: ${formatValue(minVal)}–${formatValue(maxVal)}`;\nconst HEADER_H = 104;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily:\n          '-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif',\n      }}\n    >\n      <div\n        style={{\n          height: HEADER_H,\n          padding: \"14px 48px 0 48px\",\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          boxSizing: \"border-box\",\n        }}\n      >\n        <div\n          style={{\n            fontSize: 22,\n            fontWeight: 600,\n            color: t.ink,\n            lineHeight: 1.25,\n          }}\n        >\n          {TITLE}\n        </div>\n        <div\n          style={{\n            fontSize: 15,\n            color: t.inkSoft,\n            marginTop: 4,\n          }}\n        >\n          {SUBTITLE}\n        </div>\n        <div\n          style={{\n            display: \"flex\",\n            alignItems: \"center\",\n            gap: 20,\n            marginTop: 6,\n            fontSize: 14,\n            color: t.inkSoft,\n          }}\n        >\n          <span style={{ fontWeight: 600, color: t.ink }}>{Y_CATEGORY}:</span>\n          {treatments.map((tr, i) => (\n            <span\n              key={tr}\n              style={{ display: \"inline-flex\", alignItems: \"center\", gap: 6 }}\n            >\n              <span\n                style={{\n                  width: 12,\n                  height: 12,\n                  borderRadius: 3,\n                  backgroundColor: rowColors[i],\n                  display: \"inline-block\",\n                }}\n              />\n              <span>{tr}</span>\n            </span>\n          ))}\n        </div>\n      </div>\n      <ChartsContainer\n        width={width}\n        height={height - HEADER_H}\n        margin={{ top: marginTop, right: marginRight, bottom: 24, left: 40 }}\n        skipAnimation\n        series={[]}\n        xAxis={[\n          {\n            scaleType: \"band\",\n            data: alloys,\n            label: X_CATEGORY,\n            height: 68,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"linear\",\n            min: 0,\n            max: yMax,\n            tickInterval: valueTicks,\n            valueFormatter: formatValue,\n            label: VALUE,\n            width: 96,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        sx={{\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      >\n        <g transform={`translate(${wallDx}, ${wallDy})`}>\n          <ChartsGrid horizontal />\n        </g>\n        <Bars3D />\n        <ChartsXAxis />\n        <ChartsYAxis />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}