MCPcopy
hub / github.com/chartjs/Chart.js / drawPointLegend

Function drawPointLegend

src/helpers/helpers.canvas.ts:167–308  ·  view source on GitHub ↗
(
  ctx: CanvasRenderingContext2D,
  options: DrawPointOptions,
  x: number,
  y: number,
  w: number
)

Source from the content-addressed store, hash-verified

165
166// eslint-disable-next-line complexity
167export function drawPointLegend(
168 ctx: CanvasRenderingContext2D,
169 options: DrawPointOptions,
170 x: number,
171 y: number,
172 w: number
173) {
174 let type: string, xOffset: number, yOffset: number, size: number, cornerRadius: number, width: number, xOffsetW: number, yOffsetW: number;
175 const style = options.pointStyle;
176 const rotation = options.rotation;
177 const radius = options.radius;
178 let rad = (rotation || 0) * RAD_PER_DEG;
179
180 if (style && typeof style === 'object') {
181 type = style.toString();
182 if (type === '[object HTMLImageElement]' || type === '[object HTMLCanvasElement]') {
183 ctx.save();
184 ctx.translate(x, y);
185 ctx.rotate(rad);
186 ctx.drawImage(style, -style.width / 2, -style.height / 2, style.width, style.height);
187 ctx.restore();
188 return;
189 }
190 }
191
192 if (isNaN(radius) || radius <= 0) {
193 return;
194 }
195
196 ctx.beginPath();
197
198 switch (style) {
199 // Default includes circle
200 default:
201 if (w) {
202 ctx.ellipse(x, y, w / 2, radius, 0, 0, TAU);
203 } else {
204 ctx.arc(x, y, radius, 0, TAU);
205 }
206 ctx.closePath();
207 break;
208 case 'triangle':
209 width = w ? w / 2 : radius;
210 ctx.moveTo(x + Math.sin(rad) * width, y - Math.cos(rad) * radius);
211 rad += TWO_THIRDS_PI;
212 ctx.lineTo(x + Math.sin(rad) * width, y - Math.cos(rad) * radius);
213 rad += TWO_THIRDS_PI;
214 ctx.lineTo(x + Math.sin(rad) * width, y - Math.cos(rad) * radius);
215 ctx.closePath();
216 break;
217 case 'rectRounded':
218 // NOTE: the rounded rect implementation changed to use `arc` instead of
219 // `quadraticCurveTo` since it generates better results when rect is
220 // almost a circle. 0.516 (instead of 0.5) produces results with visually
221 // closer proportion to the previous impl and it is inscribed in the
222 // circle with `radius`. For more details, see the following PRs:
223 // https://github.com/chartjs/Chart.js/issues/5597
224 // https://github.com/chartjs/Chart.js/issues/5858

Callers 2

drawPointFunction · 0.85
drawLegendBoxMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected