@data-ui/radial-chart

中文文档
0.0.84发布于 2019/11/20MITESM + CJS近 30 天下载 1.6万

A package for radial-axis visualizations https://williaster.github.io/data-ui

原始使用文档
文档目录5 节
  1. Overview
  2. Usage
  3. Tooltips
  4. Roadmap
  5. NOTE ‼️

@data-ui/radial-chart

demo at williaster.github.io/data-ui

Overview

This package exports declarative react <RadialChart />s implemented with @vx which can be used to render both donut and pie charts depending on props. As demonstrated in the demo, in combination with @vx/legend and @vx/scale these can be used to create re-usable radial charts.

Usage

See the demo at williaster.github.io/data-ui for more example outputs.

Donut chart
import { scaleOrdinal } from '@vx/scale';
import { LegendOrdinal } from '@vx/legend';

import { color as colors } from '@data-ui/theme';
import { RadialChart, ArcSeries, ArcLabel } from '@data-ui/radial-chart';

const colorScale = scaleOrdinal({ range: colors.categories });
const data = [{ label: 'a', value: 200 }, { label: 'c', value: 150 }, { label: 'c', value: 21 }];

export default () => (
  <div style={{ display: 'flex', alignItems: 'center' }}>
    <RadialChart
      ariaLabel="This is a radial-chart chart of..."
      width={width}
      height={height}
      margin={{ top, right, bottom, left }}
      renderTooltip={({ event, datum, data, fraction }) => (
        <div>
          <strong>{datum.label}</strong>
          {datum.value} ({(fraction * 100).toFixed(2)}%)
        </div>
      )}
    >
      <ArcSeries
        data={data}
        pieValue={d => d.value}
        fill={arc => colorScale(arc.data.label)}
        stroke="#fff"
        strokeWidth={1}
        label{arc => `${(arc.data.value).toFixed(1)}%`}
        labelComponent={<ArcLabel />}
        innerRadius={radius => 0.35 * radius}
        outerRadius={radius => 0.6 * radius}
        labelRadius={radius => 0.75 * radius}
      />
    </RadialChart>
    <LegendOrdinal
      direction="column"
      scale={colorScale}
      shape="rect"
      fill={({ datum }) => colorScale(datum)}
      labelFormat={label => label}
    />
  </div>
);

Tooltips

The easiest way to use tooltips out of the box is by passing a renderTooltip function to <RadialChart />. This function takes an object with the shape { event, datum, data, fraction } as input and should return the inner contents of the tooltip (not the tooltip container!) as shown above. If this function returns a falsy value, a tooltip will not be rendered.

Under the covers this will wrap the <RadialChart /> component in the exported <WithTooltip /> HOC, which wraps the svg in a <div /> and handles the positioning and rendering of an HTML-based tooltip with the contents returned by renderTooltip(). This tooltip is aware of the bounds of its container and should position itself "smartly".

If you'd like more customizability over tooltip rendering you can do either of the following:

  1. Roll your own tooltip positioning logic and pass onMouseMove and onMouseLeave functions to RadialChart. These functions are passed to the <ArcSeries /> children and are called with the signature onMouseMove({ datum, event }) and onMouseLeave() upon appropriate trigger.

  2. Wrap <RadialChart /> in <WithTooltip /> yourself, which accepts props for additional customization:

NameTypeDefaultDescription
childrenPropTypes.func or PropTypes.object-Child function (to call) or element (to clone) with onMouseMove, onMouseLeave, and tooltipData props/keys
classNamePropTypes.string-Class name to add to the <div> container wrapper
renderTooltipPropTypes.func.isRequired-Renders the contents of the tooltip, signature of ({ event, data, datum, fraction }) => node. If this function returns a falsy value, a tooltip will not be rendered.
stylesPropTypes.object{}Styles to add to the <div> container wrapper
TooltipComponentPropTypes.func or PropTypes.object@vx's TooltipWithBoundsComponent (not instance) to use as the tooltip container component. It is passed top and left numbers for positioning
tooltipPropsPropTypes.object-Props that are passed to TooltipComponent
tooltipTimeoutPropTypes.number200Timeout in ms for the tooltip to hide upon calling onMouseLeave

Note that currently this is implemented with @vx/tooltips's withTooltip HOC, which adds an additional div wrapper.

Roadmap

  • more types of radial series
  • animations / transitions

NOTE ‼️

Although pie 🍰 and donut 🍩 charts are frequently encountered, they are not the most effective visualization for conveying quantitative information. With that caveat, when used well they can effectively give an overview of population makeup which is an entirely reasonable use of these charts. We don't recommend using >7 slices for user readability.

版本与发布记录

@data-ui/radial-chart 共发布 40 个版本,首次发布于 2017/06/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.84latest2019/11/206 年前
0.0.832019/11/206 年前
0.0.822019/11/057 年前
0.0.812019/09/247 年前
0.0.802019/07/257 年前
0.0.792019/06/157 年前

关于 @data-ui/radial-chart 的常见问题

@data-ui/radial-chart 是什么?

A package for radial-axis visualizations https://williaster.github.io/data-ui

如何安装 @data-ui/radial-chart?

在项目目录运行 npm install @data-ui/radial-chart,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^15.0.0-0 || ^16.0.0-0)、react-dom(^15.0.0-0 || ^16.0.0-0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@data-ui/radial-chart 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/data-ui__radial-chart 是否存在。

@data-ui/radial-chart 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@data-ui/radial-chart 可以商用吗?

@data-ui/radial-chart 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@data-ui/radial-chart 的下载量有多少?

根据 npm 官方下载统计,@data-ui/radial-chart 在 2026/10/12 – 2026/11/10 这 30 天内被下载 15,685 次,日均约 523 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@data-ui/radial-chart 还在维护吗?

最新版本 0.0.84 发布于 2019/11/20(6 年前);至今共发布 40 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/06/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。