@d3fc/d3fc-annotation

中文文档
3.0.16发布于 2024/07/30MITESM + CJS近 30 天下载 6.2万

A collection of D3 components for rendering plot area annotations to SVG, including lines, crosshairs, gridlines and more

原始使用文档
文档目录22 节
  1. Gridline Annotation
  2. SVG Rendering
  3. Canvas Rendering
  4. Common Properties
  5. SVG Specific Properties
  6. Canvas Specific Properties
  7. Band Annotation
  8. SVG Rendering
  9. Canvas Rendering
  10. Common Properties
  11. Canvas Specific Properties
  12. Line Annotation
  13. SVG Rendering
  14. Canvas Rendering
  15. Shared Components
  16. Canvas Specific Properties
  17. Crosshair Annotation
  18. SVG Rendering
  19. Canvas Rendering
  20. Common Properties
  21. SVG Specific Properties
  22. Canvas Specific Properties

d3fc-annotation

A collection of components for rendering plot area annotations (bands, crosshairs, gridlines and lines) onto Cartesian charts using SVG and canvas.

Main D3FC package

Installation

npm install @d3fc/d3fc-annotation

API

Gridline Annotation

The gridline component renders horizontal and vertical gridlines.

SVG Rendering

# fc.annotationSvgGridline()

Constructs a new SVG gridline annotation component. Once constructed, configure the component with scales and call it on a selection -

const xScale = d3.scaleLinear()
  .range([0, 100]);

const yScale = d3.scaleLinear()
  .range([0, 100]);

const gridline = fc.annotationSvgGridline()
  .xScale(xScale)
  .yScale(yScale);

d3.select('svg')
  .call(gridline);

Canvas Rendering

# fc.annotationCanvasGridline()

Constructs a new canvas gridline annotation component. Once constructed, configure the component with scales and call it -

var ctx = canvas.getContext('2d');

const xScale = d3.scaleLinear()
  .range([0, 100]);

const yScale = d3.scaleLinear()
  .range([0, 100]);

const gridline = fc.annotationCanvasGridline()
  .xScale(xScale)
  .yScale(yScale)
  .context(ctx);

gridline();

Common Properties

# gridline.xScale([scale])

If scale is specified, sets the scale used for the vertical gridline positions (combined with xTicks). Additionally, its range is taken as the bounds of the horizontal gridlines. If scale is not specified, returns the current xScale.

# gridline.yScale([scale])

If scale is specified, sets the scale used for the horizontal gridline positions (combined with yTicks). Additionally, its range is taken as the bounds of the vertical gridlines. If scale is not specified, returns the current yScale.

# gridline.xTicks([count])

# gridline.xTicks([interval])

If count is specified, sets the count passed to ticks when requesting the horizontal gridline positions when xScale is a continuous scale. For other scales, this value is ignored and the domain is used directly. If count is not specified, returns the current count.

When xScale is a time scale this method can be passed a time interval to specify the position of the gridlines. For example to generate gridlines every fifteen minutes:

gridline.xTicks(d3.timeMinute.every(15));

# gridline.xTickValues([values])

Manually specify the vertical gridline positions. Overrides xTicks.

# gridline.yTicks([count])

# gridline.yTicks([interval])

If count is specified, sets the count passed to ticks when requesting the vertical gridline positions when yScale is a continuous scale. For other scales, this value is ignored and the domain is used directly. If count is not specified, returns the current count.

When yScale is a time scale this method can be passed a time interval to specify the position of the gridlines. For example to generate gridlines every fifteen minutes:

gridline.yTicks(d3.timeMinute.every(15));

# gridline.yTickValues([args])

Manually specify the vertical gridline positions. Overrides yTicks.

# gridline.xDecorate([decorateFunc])

If decorateFunc is specified, sets the decorate function used when joining the vertical gridlines to SVG elements or drawing canvas gridlines. If not specified, returns the current decorate function.

# gridline.yDecorate([decorateFunc])

If decorateFunc is specified, sets the decorate function used when joining the horizontal gridlines to SVG elements or drawing canvas gridlines. If not specified, returns the current decorate function.

SVG Specific Properties

# svgGridline.xKey([keyFunc])

If keyFunc is specified, sets the key function used when joining the vertical gridlines to SVG elements. If not specified, returns the current key function.

# svgGridline.yKey([keyFunc])

If keyFunc is specified, sets the key function used when joining the horizontal gridlines to SVG elements. If not specified, returns the current key function.

Canvas Specific Properties

# canvasGridline.context(ctx)

If ctx is specified, sets the canvas context and returns this series. If ctx is not specified, returns the current context.

Band Annotation

The band component renders horizontal and vertical bands.

SVG Rendering

# fc.annotationSvgBand()

Constructs a new band annotation component. Once constructed, configure the component with scales, associate a selection with some data representing the band locations and call it on the selection -

const xScale = d3.scaleLinear()
  .range([0, 100]);

const yScale = d3.scaleLinear()
  .range([0, 100]);

const band = fc.annotationSvgBand()
  .xScale(xScale)
  .yScale(yScale);

d3.select('svg')
  .datum([{ from: 45, to: 55 }])
  .call(band);

Canvas Rendering

# fc.annotationCanvasBand()

Constructs a new band annotation component. Once constructed, configure the component with scales and call with some data representing the band locations -

var ctx = canvas.getContext('2d');

const xScale = d3.scaleLinear()
  .range([0, 100]);

const yScale = d3.scaleLinear()
  .range([0, 100]);

const band = fc.annotationCanvasBand()
  .xScale(xScale)
  .yScale(yScale)
  .context(ctx);

band([{ from: 45, to: 55 }]);

Common Properties

# band.xScale([scale])

If scale is specified, sets the scale used for transforming the fromValue/toValue positions of vertical bands. Additionally, its range is taken as the bounds of the horizontal bands. If scale is not specified, returns the current xScale.

# band.yScale([scale])

If scale is specified, sets the scale used for transforming the fromValue/toValue positions of horizontal bands. Additionally, its range is taken as the bounds of the vertical bands. If scale is not specified, returns the current yScale.

# band.orient([orientation])

If orientation is specified, sets the orientation of the bars to either horizontal or vertical. If orientation is not specified, returns the current orientation.

# band.fromValue([accessorFunc])

If accessorFunc is specified, sets the function used to retrieve the start value for bands. This value will be passed through the appropriate scale. If not specified, returns the current start value.

# band.toValue([accessorFunc])

If accessorFunc is specified, sets the function used to retrieve the end value for bands. This value will be passed through the appropriate scale. If not specified, returns the current end value.

# band.decorate([decorateFunc])

If decorateFunc is specified, sets the decorate function used when joining the bands to SVG elements or drawing the canvas. If not specified, returns the current decorate function.

Canvas Specific Properties

# canvasBand.context(ctx)

If ctx is specified, sets the canvas context and returns this series. If ctx is not specified, returns the current context.

Line Annotation

The line component renders horizontal and vertical lines.

SVG Rendering

# fc.annotationSvgLine()

Constructs a new line annotation component. Once constructed, configure the component with scales, associate a selection with some data representing the line locations and call it on the selection -

const xScale = d3.scaleLinear()
  .range([0, 100]);

const yScale = d3.scaleLinear()
  .range([0, 100]);

const line = fc.annotationSvgLine()
  .xScale(xScale)
  .yScale(yScale);

d3.select('svg')
  .datum([50])
  .call(line);

Canvas Rendering

# fc.annotationCanvasLine()

Constructs a new line annotation component. Once constructed, configure the component with scales and call it with some data representing the line locations -

var ctx = canvas.getContext('2d');

const xScale = d3.scaleLinear()
  .range([0, 100]);

const yScale = d3.scaleLinear()
  .range([0, 100]);

const line = fc.annotationCanvasLine()
  .xScale(xScale)
  .yScale(yScale)
  .context(ctx);

line([50]);

Shared Components

# line.xScale([scale])

If scale is specified, sets the scale used for transforming the value of the line. Additionally, its range is taken as the bounds of the horizontal lines. If scale is not specified, returns the current xScale.

# line.yScale([scale])

If scale is specified, sets the scale used for transforming the value of the line. Additionally, its range is taken as the bounds of the vertical lines. If scale is not specified, returns the current yScale.

# line.orient([orientation])

If orientation is specified, sets the orientation of the lines to either horizontal or vertical. If orientation is not specified, returns the current orientation.

# line.value([accessorFunc])

If accessorFunc is specified, sets the function used to retrieve the value for lines. This value will be passed through the appropriate scale. If not specified, returns the current value.

# line.label([accessorFunc])

If accessorFunc is specified, sets the function used to retrieve the label for lines. If not specified, returns the current label.

# line.decorate([decorateFunc])

If decorateFunc is specified, sets the decorate function used when joining the lines to SVG elements or drawing the lines on the canvas. If not specified, returns the current decorate function.

Canvas Specific Properties

# canvasLine.context(ctx)

If ctx is specified, sets the canvas context and returns this series. If ctx is not specified, returns the current context.

Crosshair Annotation

The crosshair component renders a pair of vertical and horizontal lines with a point at their center.

SVG Rendering

# fc.annotationSvgCrosshair()

Constructs a new crosshair annotation component. Once constructed, configure the component with scales, associate a selection with some data representing the crosshair locations and call it on the selection -

const xScale = d3.scaleLinear()
  .range([0, 100]);

const yScale = d3.scaleLinear()
  .range([0, 100]);

const crosshair = fc.annotationSvgCrosshair()
  .xScale(xScale)
  .yScale(yScale);

d3.select('svg')
  .datum([{x: 50, y: 50}])
  .call(crosshair);

Canvas Rendering

# fc.annotationCanvasCrosshair()

Constructs a new crosshair annotation component. Once constructed, configure the component with scales and call with some data representing the crosshair locations -

var ctx = canvas.getContext('2d');

const xScale = d3.scaleLinear()
  .range([0, 100]);

const yScale = d3.scaleLinear()
  .range([0, 100]);

const crosshair = fc.annotationCanvasCrosshair()
  .xScale(xScale)
  .yScale(yScale)
  .context(ctx);

crosshair([{x: 50, y: 50}]);

Common Properties

# crosshair.xScale([scale])

If scale is specified, sets the scale whose range is taken as the bounds of the horizontal lines. If scale is not specified, returns the current xScale.

# crosshair.yScale([scale])

If scale is specified, sets the scale whose range is taken as the bounds of the vertical lines. If scale is not specified, returns the current yScale.

# crosshair.x([accessorFunc])

If accessorFunc is specified, sets the function used to retrieve the x position of the crosshair. N.B. this value will not be passed through the appropriate scale. If not specified, returns the current value.

# crosshair.y([accessorFunc])

If accessorFunc is specified, sets the function used to retrieve the y position of the crosshair. N.B. this value will not be passed through the appropriate scale. If not specified, returns the current value.

# crosshair.xLabel([accessorFunc])

If accessorFunc is specified, sets the function used to retrieve the label for vertical lines. If not specified, returns the current label.

# crosshair.yLabel([accessorFunc])

If accessorFunc is specified, sets the function used to retrieve the label for horizontal lines. If not specified, returns the current label.

SVG Specific Properties

# svgCrosshair.decorate([decorateFunc])

If decorateFunc is specified, sets the decorate function used when joining the crosshairs to SVG elements. If not specified, returns the current decorate function.

Canvas Specific Properties

# canvasCrosshair.decorate([decorateFunc])

If decorateFunc is specified, sets the decorate function used when drawing the point of the crosshair. If not specified, returns the current decorate function.

# canvasCrosshair.xDecorate([decorateFunc])

If decorateFunc is specified, sets the decorate function used when drawing the vertical line of the crosshair. If not specified, returns the current decorate function.

# canvasCrosshair.yDecorate([decorateFunc])

If decorateFunc is specified, sets the decorate function used when drawing the horizontal line of the crosshair. If not specified, returns the current decorate function.

# canvasCrosshair.context(ctx)

If ctx is specified, sets the canvas context and returns this series. If ctx is not specified, returns the current context.

版本与发布记录

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

版本发布日期距今
3.0.16latest2024/07/302 年前
3.0.152024/06/172 年前
3.0.142024/06/172 年前
3.0.132024/06/132 年前
3.0.122022/05/314 年前
3.0.112020/09/246 年前

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

@d3fc/d3fc-annotation 是什么?

A collection of D3 components for rendering plot area annotations to SVG, including lines, crosshairs, gridlines and more

如何安装 @d3fc/d3fc-annotation?

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

@d3fc/d3fc-annotation 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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