@d3fc/d3fc-shape

中文文档
6.0.1发布于 2020/07/14MITESM + CJS零依赖近 30 天下载 6.9万

A collection of SVG/canvas path generators for creating chart series

原始使用文档
文档目录8 节
  1. Installing
  2. API Reference
  3. Example usage
  4. Bar
  5. Boxplot
  6. Candlestick
  7. Error Bar
  8. OHLC

d3fc-shape

A collection of SVG/canvas path generators for creating chart series

Main D3FC package

Installing

npm install @d3fc/d3fc-shape

API Reference

All of the exported functions have the same signature, (context) => generator. The context supplied must be an implementation of the subset of Context2D methods as implemented by d3-path (or indeed a Context2D!).

You can then configure the generator by invoking the relevant methods (e.g. generator.x(/* ... */)) as described below. Once suitably configured invoke the generator function itself with the required data (e.g. generator([/* ... */])).

Example usage

SVG


import { path } from 'd3-path';
import { shapeCandlestick } from 'd3fc-shape';

const ctx = path();

const candlestick = shapeCandlestick()
  .x((d, i) => i)
  .open((d) => d.open)
  .high((d) => d.high)
  .low((d) => d.low)
  .close((d) => d.close);

d3.select('path')
  .datum([
    { open: 4, high: 5, low: 3, close: 3 }
  ])
  .attr('d', candlestick);

Canvas


import { shapeCandlestick } from 'd3fc-shape';

const ctx = document.querySelector('canvas').getContext('2d');

const candlestick = shapeCandlestick()
  .context(ctx)
  .x((d, i) => i)
  .open((d) => d.open)
  .high((d) => d.high)
  .low((d) => d.low)
  .close((d) => d.close);

candlestick([
  { open: 4, high: 5, low: 3, close: 3 }
]);

ctx.stroke();

Bar

# fc.shapeBar(context)

# shapeBar.x(accessorFunc)
# shapeBar.y(accessorFunc)
# shapeBar.width(accessorFunc)
# shapeBar.height(accessorFunc)

The attribute accessor methods available to set the way the bar chart accesses the data. The accessorFunc(datum, index) function is called on each item of the data, and returns the relevant value for the relevant attribute for that item.

# shapeBar.horizontalAlign(alignment)

alignment is one of: left, right or center (default)

# shapeBar.verticalAlign(alignment)

alignment is one of: bottom, top or center (default)

Boxplot

# fc.shapeBoxPlot(context)

# shapeBoxPlot.value(accessorFunc)
# shapeBoxPlot.median(accessorFunc)
# shapeBoxPlot.upperQuartile(accessorFunc)
# shapeBoxPlot.lowerQuartile(accessorFunc)
# shapeBoxPlot.high(accessorFunc)
# shapeBoxPlot.low(accessorFunc)
# shapeBoxPlot.width(accessorFunc)

The attribute accessor methods available to set the way the bar chart accesses the data. The accessorFunc(datum, index) function is called on each item of the data, and returns the relevant value for the relevant attribute for that item.

# shapeBoxPlot.cap(accessorFunc)

The accessorFunc(item, index) function is called on each item of the data, and returns the proprtion of the box width that the caps width should be.

# shapeBoxPlot.orient(orientation)

Orientation of the chart. Either horizontal (default) or vertical

Candlestick

# fc.shapeCandlestick(context)

# shapeCandlestick.x(accessorFunc)
# shapeCandlestick.open(accessorFunc)
# shapeCandlestick.high(accessorFunc)
# shapeCandlestick.low(accessorFunc)
# shapeCandlestick.close(accessorFunc)
# shapeCandlestick.width(accessorFunc)

The attribute accessor methods available to set the way the bar chart accesses the data. The accessorFunc(datum, index) function is called on each item of the data, and returns the relevant value for the relevant attribute for that item.

Error Bar

# fc.shapeErrorBar(context)

# shapeErrorBar.value(accessorFunc)
# shapeErrorBar.high(accessorFunc)
# shapeErrorBar.low(accessorFunc)
# shapeErrorBar.width(accessorFunc)

The attribute accessor methods available to set the way the bar chart accesses the data. The accessorFunc(datum, index) function is called on each item of the data, and returns the relevant value for the relevant attribute for that item.

# shapeErrorBar.orient(orientation)

Orientation of the chart. Either horizontal (default) or vertical

OHLC

# fc.shapeOhlc(context)

# shapeOhlc.x(accessorFunc)
# shapeOhlc.open(accessorFunc)
# shapeOhlc.high(accessorFunc)
# shapeOhlc.low(accessorFunc)
# shapeOhlc.close(accessorFunc)
# shapeOhlc.width(accessorFunc)

The attribute accessor methods available to set the way the bar chart accesses the data. The accessorFunc(datum, index) function is called on each item of the data, and returns the relevant value for the relevant attribute for that item.

# shapeOhlc.orient(orientation)

Orientation of the chart. Either horizontal (default) or vertical

版本与发布记录

@d3fc/d3fc-shape 共发布 23 个版本,首次发布于 2018/08/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.0.1latest2020/07/146 年前
6.0.02020/04/166 年前
5.1.02020/03/196 年前
5.0.192019/11/286 年前
5.0.182019/09/047 年前
5.0.172019/08/147 年前

关于 @d3fc/d3fc-shape 的常见问题

@d3fc/d3fc-shape 是什么?

A collection of SVG/canvas path generators for creating chart series

如何安装 @d3fc/d3fc-shape?

在项目目录运行 npm install @d3fc/d3fc-shape,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:d3-path(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@d3fc/d3fc-shape 支持 TypeScript 吗?

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

@d3fc/d3fc-shape 支持 ESM 还是 CommonJS?

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

@d3fc/d3fc-shape 可以商用吗?

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

@d3fc/d3fc-shape 的下载量有多少?

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

@d3fc/d3fc-shape 还在维护吗?

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