chartjs / chartjs-plugin-annotation

Annotation plugin for Chart.js
MIT License
609 stars 329 forks source link

Invalid center coordinates when a point/polygon annotation is configured using x/yMin and y/xMax #850

Closed stockiNail closed 1 year ago

stockiNail commented 1 year ago

When a point/polygon annotation is configured using x/yMin and y/xMax, the calculated centerX and centerY are wrong because the center point of result box is used. This is correct only if the box is a square.

https://github.com/chartjs/chartjs-plugin-annotation/blob/76ba0e9e265c14b52d59ba13737ce106eb6d9c15/src/helpers/helpers.chart.js#L140-L149

To use the center point of the box, it should be:

    const adjustCenterX = box.centerX + options.xAdjust;
    const adjustCenterY = box.centerY + options.yAdjust;
    return {
      x: adjustCenterX - radius,
      y: adjustCenterY - radius,
      x2: adjustCenterX + radius,
      y2: adjustCenterY + radius,
      centerX: adjustCenterX,
      centerY: adjustCenterY,
      width: size,
      height: size
    };