# BulletChart

> A bullet chart preset over Chart with a feature bar per item, qualitative bands, one or more targets, both orientations and value labels.

Canonical: https://salmonui.dev/docs/data-visualization/bullet-chart

`BulletChart` (`@salmonui/charts/chart/bullet`) is a preset over `Chart`
with one `bullet` series: one feature bar per data item. It is a
subpath of the chart family so a page with only `Chart` does not carry it;
its styles are in the chart stylesheet.

```tsx
import { BulletChart } from '@salmonui/charts/chart/bullet';
import '@salmonui/charts/chart/styles.css';

<BulletChart
  title="KPIs"
  name="Sales"
  data={[{ k: 'Revenue', v: 270, t: 250 }]}
  categoryField="k"
  valueField="v"
  targetField="t"
  max={300}
  bands={[{ to: 150 }, { to: 225 }, { to: 300 }]}
/>;
```

### Live demo: Bullet chart KPIs

Live demo: https://salmonui.dev/demos/data-visualization/bullet-chart-kpis

Quarterly KPIs as a BulletChart with three qualitative bands, a target and a stretch target, value labels, a legend, a custom tooltip and click events; switch orientation, feature glyph, axis side and animation.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { BulletChart } from '@salmonui/charts/chart/bullet';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Select } from '@salmonui/dropdowns/select';

const q3 = [
  { kpi: 'Revenue', actual: 78, target: 72, stretch: 88 },
  { kpi: 'Gross margin', actual: 61, target: 65, stretch: 75 },
  { kpi: 'Retention', actual: 84, target: 80, stretch: 90 },
  { kpi: 'NPS', actual: 46, target: 55, stretch: 70 },
];
const q4Change = [6, 5, -3, 12];
const q4 = q3.map((d, i) => ({ ...d, actual: d.actual + (q4Change[i] ?? 0) }));

const bands = [
  { to: 50, color: '#dc2626', label: 'Poor' },
  { to: 75, color: '#a16207', label: 'Satisfactory' },
  { to: 100, color: '#15803d', label: 'Good' },
];

const percent: Intl.NumberFormatOptions = { style: 'unit', unit: 'percent', maximumFractionDigits: 0 };

/** Quarterly KPIs: value, target and stretch target over three bands. */
export default function BulletChartKpisDemo() {
  const [orientation, setOrientation] = useState<'horizontal' | 'vertical'>('horizontal');
  const [valueShape, setValueShape] = useState<'bar' | 'dot'>('bar');
  const [axisEnd, setAxisEnd] = useState(false);
  const [animated, setAnimated] = useState(true);
  const [quarter, setQuarter] = useState<'Q3' | 'Q4'>('Q3');
  const [clicked, setClicked] = useState('none');
  return (
    <div className="demo-stack">
      <div className="demo-toolbar" role="group" aria-label="Bullet chart options">
        <label>
          Orientation
          <Select aria-label="Orientation" data={[{ value: 'horizontal', label: 'Horizontal' }, { value: 'vertical', label: 'Vertical' }]} value={orientation} onValueChange={(value) => value != null && setOrientation(value === 'vertical' ? 'vertical' : 'horizontal')} />
        </label>
        <label>
          Value glyph
          <Select aria-label="Value glyph" data={[{ value: 'bar', label: 'Bar' }, { value: 'dot', label: 'Dot' }]} value={valueShape} onValueChange={(value) => value != null && setValueShape(value === 'dot' ? 'dot' : 'bar')} />
        </label>
        <Checkbox checked={axisEnd} onCheckedChange={(checked) => setAxisEnd(checked)}>
          Axis on the opposite side
        </Checkbox>
        <Checkbox checked={animated} onCheckedChange={(checked) => setAnimated(checked)}>
          Animate
        </Checkbox>
        <Button variant="outline" type="button" onClick={() => setQuarter((q) => (q === 'Q3' ? 'Q4' : 'Q3'))}>
          Show {quarter === 'Q3' ? 'Q4' : 'Q3'}
        </Button>
      </div>
      <BulletChart
        title={`KPI scorecard, ${quarter}`}
        subtitle="Percent of plan"
        data={quarter === 'Q3' ? q3 : q4}
        categoryField="kpi"
        valueField="actual"
        name="Actual"
        targetField={['target', 'stretch']}
        targetShapes={['line', 'cross']}
        bands={bands}
        valueShape={valueShape}
        orientation={orientation}
        axisPosition={axisEnd ? 'end' : 'start'}
        min={0}
        max={100}
        interval={25}
        minorTicks={4}
        format={percent}
        labelPosition="outside"
        tickPosition="inside"
        labels={{ format: percent }}
        legend={{ position: 'bottom', title: 'Legend' }}
        animated={animated}
        tooltip={{
          render: ({ points }) => {
            const p = points[0];
            if (!p) return null;
            const onTarget = p.y >= Number(p.datum.target);
            return (
              <span>
                <strong>{String(p.x)}</strong>: {p.yText} ({onTarget ? 'on' : 'below'} target)
              </span>
            );
          },
        }}
        onItemClick={(ctx) => setClicked(`${String(ctx.x)} ${ctx.yText}`)}
        height={orientation === 'horizontal' ? 260 : 380}
      />
      <p className="demo-status" aria-live="polite">
        Last clicked: <code>{clicked}</code>
      </p>
    </div>
  );
}
```

## Props

- `name` names the value in the legend and the announcements (default
  `valueField`).
- `targetField` takes one field or several, drawn with `targetShapes`, one
  of `'line'` (default), `'circle'` or `'cross'` per target.
- `bands: [{ to, color, label }]` are the qualitative ranges; without
  colors they take neutral tones from the theme.
- `orientation: 'horizontal' | 'vertical'` and `axisPosition: 'start' |
  'end'`.
- `min`, `max`, `interval`, `minorTicks`, `format`, `labelPosition` and
  `tickPosition` (`'inside' | 'outside'`, default following
  `labelPosition`) scale the value axis.
- `title` and `subtitle` as in `Chart`; category labels sit at the start
  of the category axis.
- `labels` (`true` or `{ format }`) prints the value on the feature bar.
- `valueShape: 'dot'` draws the feature as a dot at the value instead of a
  bar from zero.
- `tooltip` (default on) and keyboard focus come from `Chart`.
- `legend` (`true` or `{ position: 'top' | 'bottom', title }`, default off)
  lists the value, each target (with its glyph) and each band (its color
  and `label`, else "Band 1", "Band 2", ...) as a static list next to the
  figure; it is server rendered.
- `valueColor` and `targetColor` are data colors; bar and target
  widths are system-owned.
- `max` defaults to the last band's `to`, `min` to 0.

`animated` (default `true`) plays the chart entrance on the feature bars
after hydration and transitions later values; `prefers-reduced-motion`
turns it off.

## Events, keyboard and accessibility

`onItemClick(context, event)` fires on a pointer click on an item (it is
`Chart`'s `onPointClick`). `BulletChart` has no controlled state. The plot
is one tab stop; arrow keys move between items and the polite live region
announces the item, its value, every target and the band the value falls
in, for example "Sales, Revenue: 270.00, Target 250.00, Band 3. 1 of 2."
(`name="Sales"`). The same text is the tooltip and the accessible
table cell. `text: { target, band, legend }` localizes the words. `ref`
receives the `Chart` instance (`export`, `toSVGString`, `print`, `focus`,
...).

## Server rendering

The figure, bands, targets, labels and legend are server-rendered SVG and
HTML; the tooltip and the entrance animation start after hydration. Without
`height` a horizontal chart is 56 px plus 36 px per item tall (at least
120 px), so the server markup already has its final size.

Fonts, border widths and other visual tokens are system-owned, not props
(see [Rendering and colors](https://salmonui.dev/docs/data-visualization/rendering-and-colors)).
Every prop is listed in the [API reference](https://salmonui.dev/docs/reference/charts/chart-bullet).
