MCPcopy Create free account
hub / github.com/chartjs/Chart.js / pathArc

Function pathArc

src/elements/element.arc.ts:138–287  ·  view source on GitHub ↗

* Path the arc, respecting border radius by separating into left and right halves. * * Start End * * 1--->a--->2 Outer * / \ * 8 3 * | | * | | * 7 4 * \ / * 6<---b<---5 Inner

(
  ctx: CanvasRenderingContext2D,
  element: ArcElement,
  offset: number,
  spacing: number,
  end: number,
  circular: boolean,
)

Source from the content-addressed store, hash-verified

136 * 6<---b<---5 Inner
137 */
138function pathArc(
139 ctx: CanvasRenderingContext2D,
140 element: ArcElement,
141 offset: number,
142 spacing: number,
143 end: number,
144 circular: boolean,
145) {
146 const {x, y, startAngle: start, pixelMargin, innerRadius: innerR} = element;
147 const {spacingMode = 'angular'} = element.options;
148 const alpha = end - start;
149
150 if (circular && element.options.selfJoin && Math.abs(alpha) >= TAU - 1e-4) {
151 pathFullCircle(ctx, element, offset, spacing);
152 return;
153 }
154
155 const outerRadius = Math.max(element.outerRadius + spacing + offset - pixelMargin, 0);
156 let innerRadius = innerR > 0 ? innerR + spacing + offset + pixelMargin : 0;
157
158 let outerSpacingOffset = 0;
159 let innerSpacingOffset = 0;
160 const beta = outerRadius > 0
161 ? Math.max(0.001, alpha * outerRadius - offset / PI) / outerRadius
162 : 0.001;
163 const angleOffset = (alpha - beta) / 2;
164
165 if (spacing) {
166 // When spacing is present, it is the same for all items
167 // So we adjust the start and end angle of the arc such that
168 // the distance is the same as it would be without the spacing
169 const noSpacingInnerRadius = innerR > 0 ? innerR - spacing : 0;
170 const noSpacingOuterRadius = outerRadius > 0 ? outerRadius - spacing : 0;
171 const avgNoSpacingRadius = (noSpacingInnerRadius + noSpacingOuterRadius) / 2;
172 const proportionalOffset = (() => {
173 const adjustedAngle = avgNoSpacingRadius !== 0 ? (alpha * avgNoSpacingRadius) / (avgNoSpacingRadius + spacing) : alpha;
174 return (alpha - adjustedAngle) / 2;
175 })();
176 const angularOffset = avgNoSpacingRadius > 0 ? Math.asin(Math.min(1, spacing / avgNoSpacingRadius)) : 0;
177
178 // Keep spacing trims below half the available span after base offset trimming.
179 const maxOffset = Math.max(0, beta / 2 - 0.001);
180 const maxOffsetSin = Math.sin(maxOffset);
181
182 if (spacingMode === 'parallel') {
183 if (innerRadius === 0 && maxOffsetSin > 0) {
184 // A root radius of zero cannot realize a non-zero parallel separator width.
185 // Raise the root just enough for the available angular span.
186 const minInnerRadius = spacing / maxOffsetSin;
187 const maxInnerRadius = Math.max(0, outerRadius - 0.001);
188 innerRadius = Math.min(minInnerRadius, maxInnerRadius);
189 }
190
191 // Use one bounded spacing value for both radii so large spacing keeps stable geometry.
192 const maxParallelSpacing = Math.min(
193 outerRadius > 0 ? outerRadius * maxOffsetSin : Number.POSITIVE_INFINITY,
194 innerRadius > 0 ? innerRadius * maxOffsetSin : Number.POSITIVE_INFINITY
195 );

Callers 2

drawArcFunction · 0.85
drawBorderFunction · 0.85

Calls 3

pathFullCircleFunction · 0.85
rThetaToXYFunction · 0.85
parseBorderRadiusFunction · 0.70

Tested by

no test coverage detected