d3-shape
可视化可以通过离散的图形标记表示,例如符号、弧、线和区域。虽然柱状图的矩形有时可能很简单,但其他形状则比较复杂,例如圆形环扇区和Catmull–Rom样条曲线。d3-shape模块为你提供了各种形状生成器以方便使用。
🌐 Visualizations can be represented by discrete graphical marks such as symbols, arcs, lines, and areas. While the rectangles of a bar chart may sometimes be simple, other shapes are complex, such as rounded annular sectors and Catmull–Rom splines. The d3-shape module provides a variety of shape generators for your convenience.
与 D3 的其他方面一样,这些形状由数据驱动:每个形状生成器都提供访问器,用于控制输入数据如何映射到可视化表示。例如,你可以为时间序列定义一个线条生成器,通过缩放数据字段以适合图表:
🌐 As with other aspects of D3, these shapes are driven by data: each shape generator exposes accessors that control how the input data are mapped to a visual representation. For example, you might define a line generator for a time series by scaling fields of your data to fit the chart:
const line = d3.line()
.x((d) => x(d.date))
.y((d) => y(d.value));然后可以使用此线生成器来计算 SVG 路径元素的 d 属性:
🌐 This line generator can then be used to compute the d attribute of an SVG path element:
path.datum(data).attr("d", line);或者,你可以使用它来渲染到 Canvas 2D 上下文:
🌐 Or you can use it to render to a Canvas 2D context:
line.context(context)(data);请参阅以下之一:
🌐 See one of: