图表
BarChart
按长度比较的离散分类。
什么时候用它
分类是一个个桶而不是连续区间。如果横轴是时间、读者关心的是趋势,AreaChart 或 LineChart 读起来更快。
示例
default
| month | Desktop | Mobile |
|---|---|---|
| Jan | 186 | 80 |
| Feb | 305 | 200 |
| Mar | 237 | 120 |
| Apr | 273 | 190 |
| May | 209 | 130 |
| Jun | 314 | 240 |
fill variants
| Desktop |
|---|
| 186 |
| 305 |
| 237 |
| 273 |
| 209 |
| 314 |
stacking and orientation
| Desktop | Mobile |
|---|---|
| 186 | 80 |
| 305 | 200 |
| 237 | 120 |
| 173 | 190 |
emphasis
| Desktop |
|---|
| 186 |
| 305 |
| 237 |
| 273 |
| 209 |
| 314 |
| Desktop |
|---|
| 186 |
| 305 |
| 237 |
| 273 |
| 209 |
| 314 |
| Desktop |
|---|
| 186 |
| 305 |
| 237 |
| 273 |
| 209 |
| 314 |
interaction
| Desktop | Mobile |
|---|---|
| 186 | 80 |
| 305 | 200 |
| 237 | 120 |
| 273 | 190 |
brush
| day | Desktop |
|---|---|
| D1 | 90 |
| D2 | 176 |
| D3 | 241 |
| D4 | 270 |
| D5 | 254 |
| D6 | 198 |
| D7 | 115 |
| D8 | 153 |
| D9 | 226 |
| D10 | 266 |
| D11 | 263 |
| D12 | 217 |
| D13 | 140 |
| D14 | 129 |
| D15 | 208 |
| D16 | 259 |
| D17 | 268 |
| D18 | 234 |
| D19 | 164 |
| D20 | 104 |
| D21 | 188 |
| D22 | 248 |
| D23 | 270 |
| D24 | 248 |
| D25 | 187 |
| D26 | 102 |
| D27 | 166 |
| D28 | 235 |
loading
value labels
| Desktop |
|---|
| 1,860 |
| 30,500 |
| 23,700 |
| 27,300 |
| 9,200 |
| 31,400 |
sonify
| month | Revenue | Refunds |
|---|---|---|
| Jan | 18,600 | 1,240 |
| Feb | 30,500 | 980 |
| Mar | 23,700 | 2,310 |
| Apr | 27,300 | 1,150 |
| May | 9,200 | 3,040 |
| Jun | 31,400 | 870 |
toolbar
Arrow keys pan by one point, Page Up and Page Down by a screenful, Home and End jump to the ends. Plus and minus zoom, zero resets. Ctrl and the wheel zoom around the pointer, and dragging across the plot zooms to that span.
Showing Jan 25 to Dec 26, 24 of 24 points
| month | Desktop |
|---|---|
| Jan 25 | 1,200 |
| Feb 25 | 1,534 |
| Mar 25 | 1,837 |
| Apr 25 | 2,077 |
| May 25 | 2,235 |
| Jun 25 | 2,296 |
| Jul 25 | 2,258 |
| Aug 25 | 2,131 |
| Sep 25 | 1,932 |
| Oct 25 | 1,687 |
| Nov 25 | 1,428 |
| Dec 25 | 1,189 |
| Jan 26 | 999 |
| Feb 26 | 884 |
| Mar 26 | 861 |
| Apr 26 | 937 |
| May 26 | 1,108 |
| Jun 26 | 1,360 |
| Jul 26 | 1,669 |
| Aug 26 | 2,005 |
| Sep 26 | 2,337 |
| Oct 26 | 2,631 |
| Nov 26 | 2,861 |
| Dec 26 | 3,004 |
说明
按长度比较的离散分类——用来回答“每一个有多少”。
当横轴是连续的、读者在追一条趋势而不是在比较各个桶时,改用 <AreaChart> 或 <LineChart>。
属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| children必填 | ReactNode | 组合进来的各个部分——坐标轴、网格、tooltip、图例,以及标记本身。 | |
| config必填 | TConfig & ValidateKeys<TData, TConfig> | — | |
| data必填 | TData[] | 图表要画的数据行。每一条对应一个点、一根柱或一个分类。 | |
| title必填 | string | 这张图在讲什么,用一句读者能据此行动的话。必填,而且即使不显示出来也会播报给读屏软件。 | |
| animationType | ChartRevealType | 'forward' | 每个 `<BarChart.Bar>` 继承的生长顺序。 |
| barCategoryGap | number | 各分类之间的间距。 | |
| barGap | number | 同一个分类内部各柱之间的间距。 | |
| barRadius | number | BAR_RADIUS | 每个 `<BarChart.Bar>` 继承的圆角。 |
| chartProps | ComponentProps<typeof RechartsBarChart> | 通往原始 Recharts 图表元素的逃生口。 | |
| className | string | 最后合并到 figure 上,所以调用处可以决定它的尺寸和间距。 | |
| defaultSelectedDataKey | string | null | null | 首次渲染时点亮的序列。选中会让其他序列变暗。 |
| description | ReactNode | 标题下面的一行——单位、时间窗口、需要提醒的地方。 | |
| empty | ChartEmptyProps | false | 图表没东西可画时显示什么。 只要 `data` 是空的、而且图表并不处于加载中,它就会顶替绘图区渲染出来——这是任何一个真实仪表盘一周之内就会遇到的状态,也是「空白的一对坐标轴」和「加载失败」根本分不出来的那一个。传 `false` 保留坐标轴,用于那种「空本身就是结论」的图。 | |
| hideDataTable | boolean | false | 去掉隐藏的表格视图。只有当页面自己已经把数据印出来时,这才是对的。 |
| isLoading | boolean | false | 把标记换成一张动画骨架,并保留已测量的高度,这样数据到位时页面不会跳。 |
| loadingBars | number | 骨架画多少根柱子。 | |
| onSelectionChange | (selectedDataKey: string | null) => void | 选中项变化时触发;被清除时传 null。 | |
| orientation | BarOrientation | 'vertical' | 柱子朝哪个方向排。当分类名长到必须在柱子底下斜着放时,用 `horizontal`。 |
| showTitle | boolean | 把标题印在图上方,而不是藏起来。 | |
| stackType | BarStackType | 'default' | 多个标记如何组合:并排、堆叠,或归一化到 100%。 |
| xDataKey | keyof TData & string | 分类轴对应的数据字段。brush 和表格视图都需要它。 |
类型
export type BarVariant = 'default' | 'hatched' | 'duotone' | 'duotone-reverse' | 'gradient' | 'stripped'
export type BarStackType = 'default' | 'stacked' | 'percent'
export type BarOrientation = 'vertical' | 'horizontal'无障碍
- title 必填;数据行同样会渲染成一份视觉隐藏的表格。
- 每根柱子都带一块看不见的通高命中区,所以刻度底部那根 3px 的柱子和满高的一样好点。
- 可点击的图例项是真正的 button 并带 aria-pressed,不是挂了点击事件的 div。
- 错峰生长动画锚定在图表自身的起始时刻、而不是每根柱子的挂载时刻,所以悬停不会让它重播——减少动态偏好下则整个去掉。