Skip to content
Domphy

chartBarMixed

A Charts block/component from shadcn/ui — clean-room reimplemented for Domphy (see methodology). Call chartBarMixed() with no arguments for a working demo, or edit the code below live.

Props

PropTypeDescription
dataChartBarCategoryPoint[]
seriesLabelstring
titlestring
subtitlestring
trendTextstring
trendDirectionChartTrendDirection
captionTextstring
gridChartBarGrid
heightnumber
Implementation notes

Horizontal single-series chart (five browser categories) where every bar gets its own accent color via per-item itemStyle.color (theme-token-derived hex through familyHex/chartBarColorHex), axis labels doubling as the key (no separate legend), numeric axis fully hidden. Uses the same custom horizontal hover overlay as chartBarHorizontal (native tooltip disabled for the same X/Y-scale-mismatch reason documented in chart-bar-shared.ts), configured with showCategoryTitle:true so the tooltip shows both the category name and value. Data/category order reversed for the same bottom-to-top category-axis reason as the horizontal recipe. Direct-source-diff fix (2026-07-05): Per-browser bar colors didn't follow upstream's chart-1..5 assignment order (were scrambled across warning/info/success) — fixed the shared browser-data constant so colors ascend the palette in the same order as upstream.

Status: ported · Reference: shadcn/ui original

// shadcn/ui "chart-bar" (mixed recipe) — clean-room reimplementation.
//
// A horizontal single-series bar chart where every bar carries its own
// distinct accent color instead of sharing one uniform fill, so the chart
// reads like a small color-coded ranking; the axis labels double as the key
// (no separate legend).
//
// Implemented purely from the block's public functional/visual spec — no
// upstream shadcn/ui source was viewed or copied.

import type { ChartOption } from "@domphy/chart";
import type { DomphyElement } from "@domphy/core";
import {
  CHART_BAR_BROWSER_DATA,
  type ChartBarCategoryPoint,
  type ChartBarGrid,
  type ChartTrendDirection,
  chartBarCardShell,
  chartBarCategoryYAxis,
  chartBarColorHex,
  chartBarFrame,
  chartBarHiddenValueXAxis,
  chartBarHorizontalHoverOverlay,
  chartBarSeriesColor,
  chartBarTrendFooter,
  chartBarValueDomain,
} from "./chart-bar-shared.js";

export interface ChartBarMixedProps {
  data?: ChartBarCategoryPoint[];
  seriesLabel?: string;
  title?: string;
  subtitle?: string;
  trendText?: string;
  trendDirection?: ChartTrendDirection;
  captionText?: string;
  grid?: ChartBarGrid;
  height?: number;
}

const DEFAULT_GRID: ChartBarGrid = { left: 64, right: 16, top: 8, bottom: 8 };

/**
 * shadcn/ui "chart-bar" mixed recipe — each row gets its own accent color.
 * Call with no arguments for a working demo.
 */
function chartBarMixed(props: ChartBarMixedProps = {}): DomphyElement<"div"> {
  const {
    data = CHART_BAR_BROWSER_DATA,
    seriesLabel = "Visitors",
    title = "Bar Chart - Mixed",
    subtitle = "January - June 2026",
    trendText = "Trending up by 5.2% this month",
    trendDirection = "up",
    captionText = "Showing total visitors for the last 6 months",
    grid = DEFAULT_GRID,
    height = 64,
  } = props;

  // Category (y) axes render bottom-to-top — reverse so the ranking reads
  // top-to-bottom in the given data order.
  const orderedData = [...data].reverse();
  const categories = orderedData.map((point) => point.category);
  const values = orderedData.map((point) => point.value);
  const colorHexes = orderedData.map((point, index) =>
    chartBarColorHex(point.color ?? chartBarSeriesColor(index).hex),
  );
  const [, domainMax] = chartBarValueDomain(values);

  const option: ChartOption = {
    tooltip: { show: false },
    xAxis: chartBarHiddenValueXAxis({ min: 0, max: domainMax }),
    yAxis: chartBarCategoryYAxis(categories),
    grid,
    series: [
      {
        type: "bar",
        name: seriesLabel,
        itemStyle: { borderRadius: [0, 5, 5, 0] },
        data: orderedData.map((point, index) => ({
          value: point.value,
          itemStyle: { color: colorHexes[index] },
        })),
      },
    ],
  };

  return chartBarCardShell({
    title,
    subtitle,
    content: {
      div: [
        chartBarFrame(option, {
          height,
          overlays: [
            chartBarHorizontalHoverOverlay({
              categories,
              grid,
              valueLabel: (index) => String(values[index] ?? ""),
            }),
          ],
        }),
      ],
    },
    footer: chartBarTrendFooter({
      trendText,
      direction: trendDirection,
      captionText,
    }),
  });
}

export { chartBarMixed };

← Back to shadcn/ui catalog