Skip to content

圆弧

🌐 Arcs

弧生成器生成一个圆形或[环形](https://en.wikipedia.org/wiki/Annulus_(mathematics)扇区,就像[饼图](https://observablehq.com/@d3/pie-chart/2)或[甜甜圈图](https://observablehq.com/@d3/donut-chart/2)一样。弧以原点为中心;使用[变换](http://www.w3.org/TR/SVG/coords.html#TransformAttribute)将弧移到不同的位置。

🌐 The arc generator produces a circular or annular sector, as in a pie or donut chart. Arcs are centered at the origin; use a transform to move the arc to a different position.

js
svg.append("path")
    .attr("transform", "translate(100,100)")
    .attr("d", d3.arc()({
      innerRadius: 100,
      outerRadius: 200,
      startAngle: -Math.PI / 2,
      endAngle: Math.PI / 2
    }));

如果startend角度之间的绝对差值(角跨度)大于2π,弧生成器将生成一个完整的圆或环。如果小于2π,弧的角长度将等于两个角度之间的绝对差值(如果有符号差为正,则顺时针;如果为负,则逆时针)。如果绝对差小于2π,弧可能具有圆角角度填充

🌐 If the absolute difference between the start and end angles (the angular span) is greater than 2π, the arc generator will produce a complete circle or annulus. If it is less than 2π, the arc’s angular length will be equal to the absolute difference between the two angles (going clockwise if the signed difference is positive and anticlockwise if it is negative). If the absolute difference is less than 2π, the arc may have rounded corners and angular padding.

另请参见 饼图生成器,它计算将一组数据表示为饼图或环形图所需的角度;这些角度随后可以传递给弧生成器。

🌐 See also the pie generator, which computes the necessary angles to represent an array of data as a pie or donut chart; these angles can then be passed to an arc generator.

arc()

来源 · 使用默认设置构建一个新的弧生成器。使用默认设置:

js
const arc = d3.arc();

或者,将半径和角度配置为常量:

🌐 Or, with the radii and angles configured as constants:

js
const arc = d3.arc()
    .innerRadius(0)
    .outerRadius(100)
    .startAngle(0)
    .endAngle(Math.PI / 2);

arc(...arguments)

来源 · 为给定的参数生成弧线。参数是任意的;它们会与this对象一起传递给弧线生成器的访问器函数。例如,使用默认设置时,预期的对象包含半径和角度:

js
const arc = d3.arc();

arc({
  innerRadius: 0,
  outerRadius: 100,
  startAngle: 0,
  endAngle: Math.PI / 2
}); // "M0,-100A100,100,0,0,1,100,0L0,0Z"

如果半径和角度定义为常量,则可以生成不带任何参数的圆弧:

🌐 If the radii and angles are instead defined as constants, you can generate an arc without any arguments:

js
d3.arc()
    .innerRadius(0)
    .outerRadius(100)
    .startAngle(0)
    .endAngle(Math.PI / 2)
  (); // "M0,-100A100,100,0,0,1,100,0L0,0Z"

如果弧线生成器具有上下文,则弧线将作为一系列path 方法调用渲染到该上下文中,并且该函数返回空值。否则,将返回path 数据字符串。

🌐 If the arc generator has a context, then the arc is rendered to this context as a sequence of path method calls and this function returns void. Otherwise, a path data string is returned.

arc.centroid(...arguments)

示例 · 来源 · 计算给定参数生成的弧线的中心线的中点[x, y]。

参数 是任意的;它们会与 this 对象一起传递给弧生成器的访问器函数。为了与生成的弧保持一致,访问器函数必须是确定性的,即在相同参数下返回相同的值。中点定义为 (startAngle + endAngle) / 2 以及 (innerRadius + outerRadius) / 2。例如:

🌐 The arguments are arbitrary; they are propagated to the arc generator’s accessor functions along with the this object. To be consistent with the generated arc, the accessors must be deterministic, i.e., return the same value given the same arguments. The midpoint is defined as (startAngle + endAngle) / 2 and (innerRadius + outerRadius) / 2. For example:

请注意,这不是弧的几何中心,几何中心可能位于弧外;这种方法仅仅是为了方便放置标签。

🌐 Note that this is not the geometric center of the arc, which may be outside the arc; this method is merely a convenience for positioning labels.

arc.innerRadius(radius)

来源 · 如果指定了radius,则将内半径设置为指定的函数或数值,并返回此弧生成器。

js
const arc = d3.arc().innerRadius(40);

如果未指定 radius,则返回当前的内半径访问器。

🌐 If radius is not specified, returns the current inner radius accessor.

js
arc.innerRadius() // () => 40

内半径访问器默认为:

🌐 The inner radius accessor defaults to:

js
function innerRadius(d) {
  return d.innerRadius;
}

将内半径指定为函数在构建堆叠极坐标柱状图时非常有用,通常与sqrt尺度一起使用。更常见的是,对于甜甜圈图或饼图使用恒定的内半径。如果外半径小于内半径,则交换内半径和外半径。负值被视为零。

🌐 Specifying the inner radius as a function is useful for constructing a stacked polar bar chart, often in conjunction with a sqrt scale. More commonly, a constant inner radius is used for a donut or pie chart. If the outer radius is smaller than the inner radius, the inner and outer radii are swapped. A negative value is treated as zero.

arc.outerRadius(radius)

来源 · 如果指定了radius,则将外半径设置为指定的函数或数值,并返回此弧生成器。

js
const arc = d3.arc().outerRadius(240);

如果未指定 radius,则返回当前的外半径访问器。

🌐 If radius is not specified, returns the current outer radius accessor.

js
arc.outerRadius() // () => 240

外部半径访问器默认为:

🌐 The outer radius accessor defaults to:

js
function outerRadius(d) {
  return d.outerRadius;
}

将外半径指定为函数在构建鸡冠图或极坐标条形图时非常有用,通常与sqrt尺度结合使用。更常见的是,在饼图或甜甜圈图中使用恒定的外半径。如果外半径小于内半径,则会交换内半径和外半径。负值被视为零。

🌐 Specifying the outer radius as a function is useful for constructing a coxcomb or polar bar chart, often in conjunction with a sqrt scale. More commonly, a constant outer radius is used for a pie or donut chart. If the outer radius is smaller than the inner radius, the inner and outer radii are swapped. A negative value is treated as zero.

arc.cornerRadius(radius)

示例 · 来源 · 如果指定了radius,则将角半径设置为指定的函数或数字,并返回此弧生成器。

js
const arc = d3.arc().cornerRadius(18);

如果未指定 radius,则返回当前的圆角半径访问器。

🌐 If radius is not specified, returns the current corner radius accessor.

js
arc.cornerRadius() // () => 18

圆角半径访问器默认为:

🌐 The corner radius accessor defaults to:

js
function cornerRadius() {
  return 0;
}

如果角半径大于零,圆弧的角将使用给定半径的圆进行圆角处理。对于圆形扇形,两个外角会被圆角处理;对于环形扇形,所有四个角都会被圆角处理。

🌐 If the corner radius is greater than zero, the corners of the arc are rounded using circles of the given radius. For a circular sector, the two outer corners are rounded; for an annular sector, all four corners are rounded.

圆角半径不得大于 (outerRadius - innerRadius) / 2。此外,对于角度跨度小于 π 的弧,当两个相邻的圆角相交时,圆角半径可能会缩小。这种情况在内角处更为常见。请参见 弧角动画 以获取示例说明。

🌐 The corner radius may not be larger than (outerRadius - innerRadius) / 2. In addition, for arcs whose angular span is less than π, the corner radius may be reduced as two adjacent rounded corners intersect. This occurs more often with the inner corners. See the arc corners animation for illustration.

arc.startAngle(angle)

来源 · 如果指定了angle,则将起始角度设置为指定的函数或数字,并返回此弧生成器。

js
const arc = d3.arc().startAngle(Math.PI / 4);

如果未指定 angle,则返回当前起始角度访问器。

🌐 If angle is not specified, returns the current start angle accessor.

js
arc.startAngle() // () => 0.7853981633974483

起始角度访问器默认为:

🌐 The start angle accessor defaults to:

js
function startAngle(d) {
  return d.startAngle;
}

角度以弧度为单位指定,0 位于 -y(12 点钟方向),正角度顺时针增加。如果 |endAngle - startAngle| ≥ 2π,将生成完整的圆或圆环,而不是扇形。

🌐 The angle is specified in radians, with 0 at -y (12 o’clock) and positive angles proceeding clockwise. If |endAngle - startAngle| ≥ 2π, a complete circle or annulus is generated rather than a sector.

arc.endAngle(angle)

来源 · 如果指定了angle,则将结束角设置为指定的函数或数字,并返回此弧生成器。

js
const arc = d3.arc().endAngle(Math.PI);

如果未指定 angle,则返回当前的结束角度访问器。

🌐 If angle is not specified, returns the current end angle accessor.

js
arc.endAngle() // () => 3.141592653589793

结束角度访问器默认为:

🌐 The end angle accessor defaults to:

js
function endAngle(d) {
  return d.endAngle;
}

角度以弧度为单位指定,0 位于 -y(12 点钟方向),正角度顺时针增加。如果 |endAngle - startAngle| ≥ 2π,将生成完整的圆或圆环,而不是扇形。

🌐 The angle is specified in radians, with 0 at -y (12 o’clock) and positive angles proceeding clockwise. If |endAngle - startAngle| ≥ 2π, a complete circle or annulus is generated rather than a sector.

arc.padAngle(angle)

示例 · 来源 · 如果指定了angle,则将填充角设置为指定的函数或数字,并返回此弧生成器。

js
const arc = d3.arc().padAngle(0);

如果未指定 angle,则返回当前填充角度访问器。

🌐 If angle is not specified, returns the current pad angle accessor.

js
arc.padAngle() // () => 0

填充角度访问器默认为:

🌐 The pad angle accessor defaults to:

js
function padAngle() {
  return d && d.padAngle;
}

衬垫角度被转换为分隔相邻弧的固定线性距离,定义为 padRadius × padAngle。此距离从弧的 startend 等量减去。如果弧形成完整的圆或环,如 |endAngle - startAngle| ≥ 2π 时,则忽略衬垫角度。

🌐 The pad angle is converted to a fixed linear distance separating adjacent arcs, defined as padRadius × padAngle. This distance is subtracted equally from the start and end of the arc. If the arc forms a complete circle or annulus, as when |endAngle - startAngle| ≥ 2π, the pad angle is ignored.

如果内半径或角跨度相对于垫片角度较小,可能无法保持相邻弧线之间的平行边。在这种情况下,弧线的内边可能会塌缩为一个点,类似于圆形扇形。因此,填充通常只应用于环形扇区(,当内半径为正时),如图所示:

🌐 If the inner radius or angular span is small relative to the pad angle, it may not be possible to maintain parallel edges between adjacent arcs. In this case, the inner edge of the arc may collapse to a point, similar to a circular sector. For this reason, padding is typically only applied to annular sectors (i.e., when innerRadius is positive), as shown in this diagram:

使用填充时推荐的最小内半径为 outerRadius * padAngle / sin(θ),其中 θ 是填充前最小弧的角跨度。例如,如果外半径为 200 像素,填充角为 0.02 弧度,一个合理的 θ 为 0.04 弧度,合理的内半径为 100 像素。请参见 弧填充动画 以获取说明。

🌐 The recommended minimum inner radius when using padding is outerRadius * padAngle / sin(θ), where θ is the angular span of the smallest arc before padding. For example, if the outer radius is 200 pixels and the pad angle is 0.02 radians, a reasonable θ is 0.04 radians, and a reasonable inner radius is 100 pixels. See the arc padding animation for illustration.

通常,填充角度不是直接在弧生成器上设置的,而是由饼图生成器计算,以确保填充弧的面积与其值成比例;参见pie.padAngle。有关说明,请参见饼图填充动画。如果你直接对弧生成器应用固定的填充角度,它往往会对较小的弧产生不成比例的减量,从而引入失真。

🌐 Often, the pad angle is not set directly on the arc generator, but is instead computed by the pie generator so as to ensure that the area of padded arcs is proportional to their value; see pie.padAngle. See the pie padding animation for illustration. If you apply a constant pad angle to the arc generator directly, it tends to subtract disproportionately from smaller arcs, introducing distortion.

arc.padRadius(radius)

来源 · 如果指定了radius,则将填充半径设置为指定的函数或数值,并返回此弧生成器。如果未指定radius,则返回当前的填充半径访问器,默认为 null,表示填充半径应自动计算为 sqrt(innerRadius × innerRadius + outerRadius × outerRadius)。填充半径决定相邻弧之间的固定线性距离,该距离定义为 padRadius × padAngle

arc.context(context)

来源 · 如果指定了 context,则设置该上下文并返回此弧生成器。

js
const context = canvas.getContext("2d");
const arc = d3.arc().context(context);

如果未指定 context,则返回当前上下文,默认值为 null。

🌐 If context is not specified, returns the current context, which defaults to null.

js
arc.context() // context

如果上下文不为 null,则生成的弧线作为一系列path 方法调用呈现到该上下文中。否则,返回表示生成弧线的path 数据字符串。

🌐 If the context is not null, then the generated arc is rendered to this context as a sequence of path method calls. Otherwise, a path data string representing the generated arc is returned.

arc.digits(digits)

来源 · 如果指定了 digits,则设置小数点后的最大位数,并返回此弧生成器。

js
const arc = d3.arc().digits(3);

如果未指定 digits,则返回当前的最大小数位数,默认值为3。

🌐 If digits is not specified, returns the current maximum fraction digits, which defaults to 3.

js
arc.digits() // 3

此选项仅在关联的上下文为 null 时适用,例如当此弧生成器用于生成路径数据时。

🌐 This option only applies when the associated context is null, as when this arc generator is used to produce path data.