{"spec_id":"bar-feature-importance","library":"muix","language":"javascript","code":"// anyplot.ai\n// bar-feature-importance: Feature Importance Bar Chart\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/100 | Updated: 2026-11-08\nimport { BarChart } from \"@mui/x-charts/BarChart\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"Wine Quality Random Forest · bar-feature-importance · javascript · muix · anyplot.ai\";\nconst TITLE_H = 84;\nconst PAD_SHARE = 0.1; // room beyond the longest bar for its value label\nconst DECIMALS = 3;\n\n// Columns: set these to your data's column names\nconst FEATURE = \"Feature\"; // role: feature\nconst IMPORTANCE = \"Importance (mean decrease in impurity)\"; // role: importance\n\n// Data: replace this block with your own records, one object per row\n// Random forest regressor predicting wine quality (UCI Wine Quality dataset); the\n// impurity-based importances of all 11 features sum to 1\nconst data = [\n  { [FEATURE]: \"Alcohol\", [IMPORTANCE]: 0.2 },\n  { [FEATURE]: \"Sulphates\", [IMPORTANCE]: 0.14 },\n  { [FEATURE]: \"Volatile Acidity\", [IMPORTANCE]: 0.126 },\n  { [FEATURE]: \"Total Sulfur Dioxide\", [IMPORTANCE]: 0.099 },\n  { [FEATURE]: \"Density\", [IMPORTANCE]: 0.087 },\n  { [FEATURE]: \"Chlorides\", [IMPORTANCE]: 0.072 },\n  { [FEATURE]: \"Citric Acid\", [IMPORTANCE]: 0.066 },\n  { [FEATURE]: \"Fixed Acidity\", [IMPORTANCE]: 0.06 },\n  { [FEATURE]: \"pH\", [IMPORTANCE]: 0.056 },\n  { [FEATURE]: \"Free Sulfur Dioxide\", [IMPORTANCE]: 0.051 },\n  { [FEATURE]: \"Residual Sugar\", [IMPORTANCE]: 0.043 },\n];\n\n// Plot: reads only data and the constants above\n// Highest importance first, so the strongest feature sits at the top\nconst rows = data.filter((d) => d[IMPORTANCE] != null).sort((a, b) => b[IMPORTANCE] - a[IMPORTANCE]);\nconst features = rows.map((d) => d[FEATURE]);\nconst importance = rows.map((d) => d[IMPORTANCE]);\nconst maxImportance = Math.max(...importance);\nconst minImportance = Math.min(...importance);\nconst fmt = (v: number) => v.toFixed(DECIMALS);\n\nconst lead = rows[0];\nconst runnerUp = rows[1];\nconst SUBTITLE = [\n  `${rows.length} features, sorted by importance`,\n  runnerUp && runnerUp[IMPORTANCE] > 0\n    ? `${lead[FEATURE]} leads at ${fmt(lead[IMPORTANCE])}, ${(lead[IMPORTANCE] / runnerUp[IMPORTANCE]).toFixed(1)}× ${runnerUp[FEATURE]}`\n    : null,\n]\n  .filter(Boolean)\n  .join(\" · \");\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      <BarChart\n        width={width}\n        height={height - TITLE_H}\n        layout=\"horizontal\"\n        skipAnimation\n        hideLegend\n        borderRadius={4}\n        grid={{ vertical: true }}\n        series={[\n          {\n            id: \"importance\",\n            data: importance,\n            label: IMPORTANCE,\n            valueFormatter: (v: number | null) => (v == null ? null : fmt(v)),\n            barLabel: (item: { value: number | null }) => (item.value == null ? null : fmt(item.value)),\n            barLabelPlacement: \"outside\",\n          },\n        ]}\n        xAxis={[\n          {\n            id: \"value\",\n            min: 0,\n            max: maxImportance * (1 + PAD_SHARE),\n            label: IMPORTANCE,\n            height: 64,\n            tickNumber: 8,\n            // The sequential Imprint ramp maps the same importance the bar length shows\n            colorMap: { type: \"continuous\", min: minImportance, max: maxImportance, color: [t.seq[0], t.seq[1]] },\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"band\",\n            data: features,\n            label: FEATURE,\n            width: 210,\n            categoryGapRatio: 0.3,\n            disableTicks: true,\n            tickLabelStyle: { fontSize: 15, fill: t.ink },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        margin={{ top: 16, right: 48, bottom: 8, left: 24 }}\n        sx={{\n          \"& .MuiBarChart-label\": { fill: t.ink, fontSize: 15, fontWeight: 600 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      />\n    </Box>\n  );\n}\n"}