@oliasoft-open-source/charts-library 5.9.0-beta-2 → 5.9.0-beta-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/assets/{Color-6BZIO3FS-BdmyW5fF.js → Color-6BZIO3FS-DhiCnlR7.js} +1 -1
  2. package/dist/assets/{DocsRenderer-LL677BLK-Dmh2MbD-.js → DocsRenderer-LL677BLK-CLTp7LBe.js} +4 -4
  3. package/dist/assets/{WithTooltip-65CFNBJE-B1vNinkY.js → WithTooltip-65CFNBJE-nlhXDghh.js} +1 -1
  4. package/dist/assets/{bar-chart.stories-Ch0i7N0G.js → bar-chart.stories-Be0sO_B7.js} +1 -1
  5. package/dist/assets/{chunk-YKABRMAI-nN4IfCIz.js → chunk-YKABRMAI-CLqhon1S.js} +1 -1
  6. package/dist/assets/{components-s5a1V3CD.js → components-DbZbxf23.js} +4 -4
  7. package/dist/assets/formatter-EIJCOSYU-Bl_ix4lj.js +1 -0
  8. package/dist/assets/{get-draggableData-mCkevOrC.js → get-draggableData-D9mEzSRR.js} +1 -1
  9. package/dist/assets/{iframe-B7buKd6W.js → iframe-BRmN2xGD.js} +3 -3
  10. package/dist/assets/{legend-Cem_14IV.js → legend-Dp1wGMqq.js} +1 -1
  11. package/dist/assets/legend-context-CsgeM7Ef.js +10 -0
  12. package/dist/assets/{line-chart-BDauhYk8.js → line-chart-CmggAPJL.js} +1 -1
  13. package/dist/assets/{line-chart.stories-T04m8x5t.js → line-chart.stories-DKASrDEu.js} +1 -1
  14. package/dist/assets/{line-chart.test-case.stories-BcA_QlE1.js → line-chart.test-case.stories-Vi5KSfuS.js} +174 -126
  15. package/dist/assets/{pie-chart.stories-D2G6WnUw.js → pie-chart.stories-quF2SShg.js} +1 -1
  16. package/dist/assets/{react-18-DQODWq5X.js → react-18-LnLgmPpm.js} +1 -1
  17. package/dist/assets/{react-CwUuogrH.js → react-B3OBiEIY.js} +1 -1
  18. package/dist/assets/{react-dom-O1LTTn3X.js → react-dom-7mcTV0j1.js} +1 -1
  19. package/dist/assets/{scatter-chart.stories-DJ3Z3dsB.js → scatter-chart.stories-27nhXtcG.js} +1 -1
  20. package/dist/assets/{syntaxhighlighter-ED5Y7EFY-DYFKPQ-I.js → syntaxhighlighter-ED5Y7EFY-CU6vABwV.js} +1 -1
  21. package/dist/assets/{theming-DpsViFyv.js → theming-CZ2tB5Pm.js} +1 -1
  22. package/dist/common/helpers/callout-helpers/callout-helpers.d.ts +1 -0
  23. package/dist/iframe.html +1 -1
  24. package/dist/index.js +612 -434
  25. package/dist/index.json +1 -1
  26. package/dist/line-chart/plugins/callout-plugin/helpers.d.ts +11 -0
  27. package/dist/project.json +1 -1
  28. package/dist/src/components/common/helpers/callout-helpers/callout-helpers.d.ts +1 -0
  29. package/dist/src/components/line-chart/plugins/callout-plugin/helpers.d.ts +11 -0
  30. package/package.json +1 -1
  31. package/dist/assets/formatter-EIJCOSYU-Bo0DMj3p.js +0 -1
  32. package/dist/assets/legend-context-D1oEf-Th.js +0 -9
  33. package/dist/common/helpers/callout-helpers/callout-overlap-helpers.d.ts +0 -9
  34. package/dist/src/components/common/helpers/callout-helpers/callout-overlap-helpers.d.ts +0 -9
package/dist/index.js CHANGED
@@ -802,7 +802,7 @@ function _factorize(value) {
802
802
  function isNonPrimitive(n) {
803
803
  return typeof n === "symbol" || typeof n === "object" && n !== null && !(Symbol.toPrimitive in n || "toString" in n || "valueOf" in n);
804
804
  }
805
- function isNumber$2(n) {
805
+ function isNumber$1(n) {
806
806
  return !isNonPrimitive(n) && !isNaN(parseFloat(n)) && isFinite(n);
807
807
  }
808
808
  function almostWhole(x, epsilon) {
@@ -4352,7 +4352,7 @@ var LineController = class extends DatasetController {
4352
4352
  const iAxis = iScale.axis;
4353
4353
  const vAxis = vScale.axis;
4354
4354
  const { spanGaps, segment } = this.options;
4355
- const maxGapLength = isNumber$2(spanGaps) ? spanGaps : Number.POSITIVE_INFINITY;
4355
+ const maxGapLength = isNumber$1(spanGaps) ? spanGaps : Number.POSITIVE_INFINITY;
4356
4356
  const directUpdate = this.chart._animationsDisabled || reset || mode === "none";
4357
4357
  const end = start + count;
4358
4358
  const pointsCount = points.length;
@@ -4475,7 +4475,7 @@ var ScatterController = class extends DatasetController {
4475
4475
  const iAxis = iScale.axis;
4476
4476
  const vAxis = vScale.axis;
4477
4477
  const { spanGaps, segment } = this.options;
4478
- const maxGapLength = isNumber$2(spanGaps) ? spanGaps : Number.POSITIVE_INFINITY;
4478
+ const maxGapLength = isNumber$1(spanGaps) ? spanGaps : Number.POSITIVE_INFINITY;
4479
4479
  const directUpdate = this.chart._animationsDisabled || reset || mode === "none";
4480
4480
  let prevParsed = start > 0 && this.getParsed(start - 1);
4481
4481
  for (let i = start; i < start + count; ++i) {
@@ -5281,7 +5281,7 @@ var Element = class {
5281
5281
  };
5282
5282
  }
5283
5283
  hasValue() {
5284
- return isNumber$2(this.x) && isNumber$2(this.y);
5284
+ return isNumber$1(this.x) && isNumber$1(this.y);
5285
5285
  }
5286
5286
  getProps(props, final) {
5287
5287
  const anims = this.$animations;
@@ -11390,7 +11390,7 @@ function parse(scale, input) {
11390
11390
  if (typeof parser === "function") value = parser(value);
11391
11391
  if (!isNumberFinite(value)) value = typeof parser === "string" ? adapter.parse(value, parser) : adapter.parse(value);
11392
11392
  if (value === null) return null;
11393
- if (round) value = round === "week" && (isNumber$2(isoWeekday) || isoWeekday === true) ? adapter.startOf(value, "isoWeek", isoWeekday) : adapter.startOf(value, round);
11393
+ if (round) value = round === "week" && (isNumber$1(isoWeekday) || isoWeekday === true) ? adapter.startOf(value, "isoWeek", isoWeekday) : adapter.startOf(value, round);
11394
11394
  return +value;
11395
11395
  }
11396
11396
  function determineUnitForAutoTicks(minUnit, min, max, capacity) {
@@ -11585,7 +11585,7 @@ var TimeScale = class extends Scale {
11585
11585
  const minor = timeOpts.unit || determineUnitForAutoTicks(timeOpts.minUnit, min, max, this._getLabelCapacity(min));
11586
11586
  const stepSize = valueOrDefault(options.ticks.stepSize, 1);
11587
11587
  const weekday = minor === "week" ? timeOpts.isoWeekday : false;
11588
- const hasWeekday = isNumber$2(weekday) || weekday === true;
11588
+ const hasWeekday = isNumber$1(weekday) || weekday === true;
11589
11589
  const ticks = {};
11590
11590
  let first = min;
11591
11591
  let time, count;
@@ -13597,7 +13597,7 @@ function isZoomingOrPanning(chart) {
13597
13597
  const state = getState(chart);
13598
13598
  return state.panning || state.dragging;
13599
13599
  }
13600
- var clamp$2 = (x, from, to) => Math.min(to, Math.max(from, x));
13600
+ var clamp$3 = (x, from, to) => Math.min(to, Math.max(from, x));
13601
13601
  function removeHandler(chart, type) {
13602
13602
  const { handlers } = getState(chart);
13603
13603
  const handler = handlers[type];
@@ -13683,8 +13683,8 @@ function applyAspectRatio({ begin, end }, aspectRatio) {
13683
13683
  end.y = begin.y + height;
13684
13684
  }
13685
13685
  function applyMinMaxProps(rect, chartArea, points, { min, max, prop }) {
13686
- rect[min] = clamp$2(Math.min(points.begin[prop], points.end[prop]), chartArea[min], chartArea[max]);
13687
- rect[max] = clamp$2(Math.max(points.begin[prop], points.end[prop]), chartArea[min], chartArea[max]);
13686
+ rect[min] = clamp$3(Math.min(points.begin[prop], points.end[prop]), chartArea[min], chartArea[max]);
13687
+ rect[max] = clamp$3(Math.max(points.begin[prop], points.end[prop]), chartArea[min], chartArea[max]);
13688
13688
  }
13689
13689
  function getRelativePoints(chart, pointEvents, maintainAspectRatio) {
13690
13690
  const points = {
@@ -15073,10 +15073,10 @@ function rotated(point, center, angle) {
15073
15073
  }
15074
15074
  var isOlderPart = (act, req) => req > act || act.length > req.length && act.slice(0, req.length) === req;
15075
15075
  var EPSILON = .001;
15076
- var clamp$1 = (x, from, to) => Math.min(to, Math.max(from, x));
15076
+ var clamp$2 = (x, from, to) => Math.min(to, Math.max(from, x));
15077
15077
  var inLimit = (limit, hitSize) => limit.value >= limit.start - hitSize && limit.value <= limit.end + hitSize;
15078
15078
  function clampAll(obj, from, to) {
15079
- for (const key of Object.keys(obj)) obj[key] = clamp$1(obj[key], from, to);
15079
+ for (const key of Object.keys(obj)) obj[key] = clamp$2(obj[key], from, to);
15080
15080
  return obj;
15081
15081
  }
15082
15082
  function inPointRange(point, center, radius, hitSize) {
@@ -15117,7 +15117,7 @@ function requireVersion(pkg, min, ver, strict = true) {
15117
15117
  }
15118
15118
  var isPercentString = (s) => typeof s === "string" && s.endsWith("%");
15119
15119
  var toPercent = (s) => parseFloat(s) / 100;
15120
- var toPositivePercent = (s) => clamp$1(toPercent(s), 0, 1);
15120
+ var toPositivePercent = (s) => clamp$2(toPercent(s), 0, 1);
15121
15121
  var boxAppering = (x, y) => ({
15122
15122
  x,
15123
15123
  y,
@@ -15499,8 +15499,8 @@ function applyLabelContent(ctx, { x, y }, labels, { fonts, colors }) {
15499
15499
  });
15500
15500
  }
15501
15501
  function getOpacity(value, elementValue) {
15502
- const opacity = isNumber$2(value) ? value : elementValue;
15503
- return isNumber$2(opacity) ? clamp$1(opacity, 0, 1) : 1;
15502
+ const opacity = isNumber$1(value) ? value : elementValue;
15503
+ return isNumber$1(opacity) ? clamp$2(opacity, 0, 1) : 1;
15504
15504
  }
15505
15505
  var positions = [
15506
15506
  "left",
@@ -16834,7 +16834,7 @@ function calculateTAdjust(lineSize, sizes, label, space) {
16834
16834
  const lineH = lineSize.h * space.dy;
16835
16835
  const x = lineW > 0 && (labelSize.w / 2 + padding.left - space.x) / lineW;
16836
16836
  const y = lineH > 0 && (labelSize.h / 2 + padding.top - space.y) / lineH;
16837
- return clamp$1(Math.max(x, y), 0, .25);
16837
+ return clamp$2(Math.max(x, y), 0, .25);
16838
16838
  }
16839
16839
  function spaceAround(properties, chartArea) {
16840
16840
  const { x, x2, y, y2 } = properties;
@@ -17595,7 +17595,6 @@ const BORDER_COLOR = "rgba(0,0,0,0.1)";
17595
17595
  const ANNOTATION_DASH = [10, 2];
17596
17596
  const DEFAULT_FONT_FAMILY = "\"Roobert\", \"Noto Sans\", sans-serif";
17597
17597
  const DEFAULT_COLOR = "hsl(60, 10.34482759%, 12.5%)";
17598
- const TRANSPARENT = "transparent";
17599
17598
  const LOGARITHMIC_STEPS = [
17600
17599
  1,
17601
17600
  10,
@@ -19393,329 +19392,478 @@ function addTransparency(input, alpha, out = "rgb") {
19393
19392
  const c = color.alpha(a);
19394
19393
  return out === "hex" ? c.toHex() : c.toRgbString();
19395
19394
  }
19395
+ var DEFAULT_FONT_SIZE = 12;
19396
+ var DEFAULT_PADDING = 5;
19397
+ var DEFAULT_MARGIN = 8;
19398
+ var DEFAULT_LABEL_GAP = 8;
19399
+ var DEFAULT_MIN_CONNECTOR_LENGTH = 14;
19400
+ var DEFAULT_HORIZONTAL_OFFSET = 18;
19401
+ var DEFAULT_VERTICAL_OFFSET = -18;
19402
+ var DEFAULT_ROW_GAP = 22;
19403
+ var DEFAULT_ITEM_GAP = 8;
19404
+ var DEFAULT_SIDE_SPREAD_STEP = 12;
19405
+ var DEFAULT_SIDE_SPREAD_MAX = 48;
19406
+ var clamp$1 = (value, min, max) => {
19407
+ if (Number.isNaN(value)) return min;
19408
+ if (min > max) return min;
19409
+ return Math.min(Math.max(value, min), max);
19410
+ };
19411
+ var getChartFromCtx$1 = (ctx) => {
19412
+ return ctx?.chart?.chart ?? ctx?.chart;
19413
+ };
19396
19414
  var getXScale = (chart, axisId) => {
19397
19415
  if (axisId && chart?.scales?.[axisId]) return chart.scales[axisId];
19398
19416
  if (chart?.scales?.x) return chart.scales.x;
19399
- const key = Object.keys(chart?.scales ?? {})?.find((k) => k?.toLowerCase()?.includes("x"));
19417
+ const key = Object.keys(chart?.scales ?? {})?.find((item) => {
19418
+ return item?.toLowerCase()?.includes("x");
19419
+ });
19400
19420
  return key ? chart?.scales?.[key] : void 0;
19401
19421
  };
19402
19422
  var getYScale = (chart, axisId) => {
19403
19423
  if (axisId && chart?.scales?.[axisId]) return chart.scales[axisId];
19404
19424
  if (chart?.scales?.y) return chart.scales.y;
19405
- const key = Object.keys(chart?.scales ?? {})?.find((k) => k?.toLowerCase()?.includes("y"));
19425
+ const key = Object.keys(chart?.scales ?? {})?.find((item) => {
19426
+ return item?.toLowerCase()?.includes("y");
19427
+ });
19406
19428
  return key ? chart?.scales?.[key] : void 0;
19407
19429
  };
19408
- var isNumber$1 = (value) => {
19409
- return typeof value === "number" && Number.isFinite(value);
19430
+ var resolveFontSize = (font) => {
19431
+ if (typeof font === "string") {
19432
+ const match = font.match(/(\d+(?:\.\d+)?)px/);
19433
+ return match ? Number(match[1]) : DEFAULT_FONT_SIZE;
19434
+ }
19435
+ if (font && typeof font === "object" && "size" in font) {
19436
+ const size = Number(font.size);
19437
+ return Number.isFinite(size) ? size : DEFAULT_FONT_SIZE;
19438
+ }
19439
+ return DEFAULT_FONT_SIZE;
19410
19440
  };
19411
- var looksLikeXAxis = (axisId) => {
19412
- return !!axisId && axisId.toLowerCase().includes("x");
19441
+ var resolvePadding = (padding) => {
19442
+ if (typeof padding === "number") return {
19443
+ top: padding,
19444
+ right: padding,
19445
+ bottom: padding,
19446
+ left: padding
19447
+ };
19448
+ if (padding && typeof padding === "object") {
19449
+ const typedPadding = padding;
19450
+ return {
19451
+ top: Number(typedPadding.top ?? DEFAULT_PADDING),
19452
+ right: Number(typedPadding.right ?? DEFAULT_PADDING),
19453
+ bottom: Number(typedPadding.bottom ?? DEFAULT_PADDING),
19454
+ left: Number(typedPadding.left ?? DEFAULT_PADDING)
19455
+ };
19456
+ }
19457
+ return {
19458
+ top: DEFAULT_PADDING,
19459
+ right: DEFAULT_PADDING,
19460
+ bottom: DEFAULT_PADDING,
19461
+ left: DEFAULT_PADDING
19462
+ };
19463
+ };
19464
+ var estimateLabelSize = (chart, annotation, calloutCfg) => {
19465
+ const content = `${annotation?.label ?? ""}`;
19466
+ const fontSize = resolveFontSize(calloutCfg?.font);
19467
+ const padding = resolvePadding(calloutCfg?.padding);
19468
+ const lines = content.split("\n");
19469
+ const fallbackWidth = Math.max(...lines.map((line) => line.length), 0) * fontSize * .62;
19470
+ let measuredWidth = fallbackWidth;
19471
+ if (chart?.ctx) {
19472
+ chart.ctx.save();
19473
+ chart.ctx.font = typeof calloutCfg?.font === "string" ? calloutCfg.font : `${fontSize}px sans-serif`;
19474
+ measuredWidth = Math.max(...lines.map((line) => chart.ctx.measureText(line).width), fallbackWidth);
19475
+ chart.ctx.restore();
19476
+ }
19477
+ return {
19478
+ width: measuredWidth + padding.left + padding.right,
19479
+ height: lines.length * fontSize + padding.top + padding.bottom
19480
+ };
19481
+ };
19482
+ var getAxisId = (annotation, axis) => {
19483
+ const axisScaleId = axis === "x" ? annotation?.xScaleID : annotation?.yScaleID;
19484
+ if (axisScaleId) return axisScaleId;
19485
+ if (annotation?.annotationAxis?.startsWith(axis)) return annotation.annotationAxis;
19486
+ };
19487
+ var getAnchorPixel = (chart, annotation, axis, area) => {
19488
+ const scale = axis === "x" ? getXScale(chart, getAxisId(annotation, "x")) : getYScale(chart, getAxisId(annotation, "y"));
19489
+ const directValue = axis === "x" ? annotation?.xValue : annotation?.yValue;
19490
+ if (typeof directValue === "number" && scale) return scale.getPixelForValue(directValue);
19491
+ const minKey = axis === "x" ? "xMin" : "yMin";
19492
+ const maxKey = axis === "x" ? "xMax" : "yMax";
19493
+ const minValue = annotation?.[minKey];
19494
+ const maxValue = annotation?.[maxKey];
19495
+ if (typeof minValue === "number" && typeof maxValue === "number" && scale) return (scale.getPixelForValue(minValue) + scale.getPixelForValue(maxValue)) / 2;
19496
+ if (typeof minValue === "number" && scale) return scale.getPixelForValue(minValue);
19497
+ if (typeof maxValue === "number" && scale) return scale.getPixelForValue(maxValue);
19498
+ return axis === "x" ? (area.left + area.right) / 2 : (area.top + area.bottom) / 2;
19499
+ };
19500
+ var hasAxisAnchor = (annotation, axis) => {
19501
+ if (axis === "x") return typeof annotation?.xValue === "number" || typeof annotation?.xMin === "number" || typeof annotation?.xMax === "number" || annotation?.type === "box" || annotation?.type === "label" || annotation?.type === "ellipse";
19502
+ return typeof annotation?.yValue === "number" || typeof annotation?.yMin === "number" || typeof annotation?.yMax === "number" || annotation?.type === "box" || annotation?.type === "label" || annotation?.type === "ellipse";
19503
+ };
19504
+ var getBoxHalfWidth = (chart, area, annotation) => {
19505
+ const xScale = getXScale(chart, getAxisId(annotation, "x"));
19506
+ if (xScale && typeof annotation?.xMin === "number" && typeof annotation?.xMax === "number") return Math.abs(xScale.getPixelForValue(annotation.xMax) - xScale.getPixelForValue(annotation.xMin)) / 2;
19507
+ if (annotation?.type === "box") return (area.right - area.left) / 2;
19508
+ return annotation?.radius ?? 0;
19509
+ };
19510
+ var getBoxHalfHeight = (chart, annotation) => {
19511
+ const yScale = getYScale(chart, getAxisId(annotation, "y"));
19512
+ if (yScale && typeof annotation?.yMin === "number" && typeof annotation?.yMax === "number") return Math.abs(yScale.getPixelForValue(annotation.yMax) - yScale.getPixelForValue(annotation.yMin)) / 2;
19513
+ return annotation?.radius ?? 0;
19514
+ };
19515
+ var getReferencePoint$1 = (chart, area, annotation) => {
19516
+ const anchorX = getAnchorPixel(chart, annotation, "x", area);
19517
+ const anchorY = getAnchorPixel(chart, annotation, "y", area);
19518
+ const halfWidth = getBoxHalfWidth(chart, area, annotation);
19519
+ const halfHeight = getBoxHalfHeight(chart, annotation);
19520
+ const labelOffsetPx = annotation?.labelOffsetPx ?? 14;
19521
+ const xAdjust = annotation?.labelConfig?.xAdjust ?? 0;
19522
+ const yAdjust = annotation?.labelConfig?.yAdjust ?? 0;
19523
+ const position = annotation?.labelConfig?.position;
19524
+ let x = anchorX;
19525
+ let y = anchorY;
19526
+ let side = anchorX >= (area.left + area.right) / 2 ? "right" : "left";
19527
+ switch (position) {
19528
+ case Position.Left:
19529
+ x -= Math.max(halfWidth, annotation?.radius ?? 0) + labelOffsetPx;
19530
+ side = "left";
19531
+ break;
19532
+ case Position.Right:
19533
+ x += Math.max(halfWidth, annotation?.radius ?? 0) + labelOffsetPx;
19534
+ side = "right";
19535
+ break;
19536
+ case Position.TopLeft:
19537
+ x -= halfWidth;
19538
+ y -= halfHeight;
19539
+ side = "left";
19540
+ break;
19541
+ case Position.TopRight:
19542
+ x += halfWidth;
19543
+ y -= halfHeight;
19544
+ side = "right";
19545
+ break;
19546
+ case Position.BottomLeft:
19547
+ x -= halfWidth;
19548
+ y += halfHeight;
19549
+ side = "left";
19550
+ break;
19551
+ case Position.BottomRight:
19552
+ x += halfWidth;
19553
+ y += halfHeight;
19554
+ side = "right";
19555
+ break;
19556
+ case Position.Bottom:
19557
+ y += Math.max(halfHeight, annotation?.radius ?? 0) + labelOffsetPx;
19558
+ break;
19559
+ case Position.Top:
19560
+ default:
19561
+ y -= Math.max(halfHeight, annotation?.radius ?? 0) + labelOffsetPx;
19562
+ break;
19563
+ }
19564
+ return {
19565
+ x: x + xAdjust,
19566
+ y: y + yAdjust,
19567
+ side
19568
+ };
19413
19569
  };
19414
- var looksLikeYAxis = (axisId) => {
19415
- return !!axisId && axisId.toLowerCase().includes("y");
19570
+ var getOriginalLabelRect = (chart, area, annotation, index) => {
19571
+ const size = estimateLabelSize(chart, annotation, annotation?.labelConfig?.callout ?? {});
19572
+ const reference = getReferencePoint$1(chart, area, annotation);
19573
+ const direction = reference.side === "right" ? 1 : -1;
19574
+ const centerX = reference.x + direction * (size.width / 2);
19575
+ const centerY = reference.y;
19576
+ return {
19577
+ id: `callout-annotation-${index}`,
19578
+ side: reference.side,
19579
+ left: centerX - size.width / 2,
19580
+ right: centerX + size.width / 2,
19581
+ top: centerY - size.height / 2,
19582
+ bottom: centerY + size.height / 2
19583
+ };
19416
19584
  };
19417
- var resolveXScale = (chart, refAnnotation) => {
19418
- return getXScale(chart, refAnnotation?.xScaleID) ?? getXScale(chart, looksLikeXAxis(refAnnotation?.annotationAxis) ? refAnnotation?.annotationAxis : void 0) ?? getXScale(chart, refAnnotation?.annotationAxis);
19585
+ var isRectOverlapping = (left, right) => {
19586
+ return !(left.right <= right.left || right.right <= left.left || left.bottom <= right.top || right.bottom <= left.top);
19419
19587
  };
19420
- var resolveYScale = (chart, refAnnotation) => {
19421
- return getYScale(chart, refAnnotation?.yScaleID) ?? getYScale(chart, looksLikeYAxis(refAnnotation?.annotationAxis) ? refAnnotation?.annotationAxis : void 0) ?? getYScale(chart, refAnnotation?.annotationAxis);
19588
+ var getOverlappingCalloutIds = (chart) => {
19589
+ const area = chart?.chartArea;
19590
+ const annotationsData = (chart?.options)?.annotations?.annotationsData ?? [];
19591
+ if (!chart || !area) return /* @__PURE__ */ new Set();
19592
+ const rects = annotationsData.flatMap((annotation, index) => {
19593
+ const calloutCfg = annotation?.labelConfig?.callout ?? {};
19594
+ if (!calloutCfg?.enabled || !calloutCfg?.onlyWhenOverlapping) return [];
19595
+ return [getOriginalLabelRect(chart, area, annotation, index)];
19596
+ });
19597
+ const overlappingIds = /* @__PURE__ */ new Set();
19598
+ rects.forEach((rect, index) => {
19599
+ rects.slice(index + 1).forEach((otherRect) => {
19600
+ if (rect.side !== otherRect.side) return;
19601
+ if (!isRectOverlapping(rect, otherRect)) return;
19602
+ overlappingIds.add(rect.id);
19603
+ overlappingIds.add(otherRect.id);
19604
+ });
19605
+ });
19606
+ return overlappingIds;
19607
+ };
19608
+ var getOccupiedOriginalLabelRects = (chart, area, overlappingIds) => {
19609
+ return ((chart?.options)?.annotations?.annotationsData ?? []).flatMap((annotation, index) => {
19610
+ const calloutCfg = annotation?.labelConfig?.callout ?? {};
19611
+ const id = `callout-annotation-${index}`;
19612
+ if (!calloutCfg?.enabled || !calloutCfg?.onlyWhenOverlapping) return [];
19613
+ if (overlappingIds.has(id)) return [];
19614
+ return [getOriginalLabelRect(chart, area, annotation, index)];
19615
+ });
19422
19616
  };
19423
- var getAxisAlignedValue = (refAnnotation, axis) => {
19424
- if (!isNumber$1(refAnnotation?.value)) return;
19425
- const annAxis = refAnnotation?.annotationAxis;
19426
- if (axis === "x" && looksLikeXAxis(annAxis)) return refAnnotation.value;
19427
- if (axis === "y" && looksLikeYAxis(annAxis)) return refAnnotation.value;
19617
+ const isCalloutOverlapping = (ctx, refAnnotation, index) => {
19618
+ const chart = getChartFromCtx$1(ctx);
19619
+ if (!(refAnnotation?.labelConfig?.callout ?? {})?.onlyWhenOverlapping) return true;
19620
+ if (!chart?.chartArea) return false;
19621
+ return getOverlappingCalloutIds(chart).has(`callout-annotation-${index}`);
19622
+ };
19623
+ var getPreferredX = (chart, area, annotation, calloutCfg, width) => {
19624
+ const xScale = getXScale(chart, getAxisId(annotation, "x"));
19625
+ const explicitXValue = typeof calloutCfg?.xValue === "number" ? calloutCfg.xValue : void 0;
19626
+ if (typeof explicitXValue === "number" && xScale) return xScale.getPixelForValue(explicitXValue);
19627
+ const margin = calloutCfg?.margin ?? DEFAULT_MARGIN;
19628
+ const halfWidth = width / 2;
19629
+ const reference = getReferencePoint$1(chart, area, annotation);
19630
+ const direction = reference.side === "right" ? 1 : -1;
19631
+ const gap = calloutCfg?.gapPx ?? DEFAULT_LABEL_GAP;
19632
+ const minConnectorLength = calloutCfg?.minConnectorLengthPx ?? DEFAULT_MIN_CONNECTOR_LENGTH;
19633
+ return clamp$1(reference.x + direction * (halfWidth + gap + minConnectorLength), area.left + margin + halfWidth, area.right - margin - halfWidth);
19634
+ };
19635
+ var getPreferredY = (chart, area, annotation, calloutCfg, index) => {
19636
+ const yScale = getYScale(chart, getAxisId(annotation, "y"));
19637
+ const explicitYValue = typeof calloutCfg?.yValue === "number" ? calloutCfg.yValue : void 0;
19638
+ if (typeof explicitYValue === "number" && yScale) return yScale.getPixelForValue(explicitYValue);
19639
+ const reference = getReferencePoint$1(chart, area, annotation);
19640
+ const hasCustomOffset = typeof calloutCfg?.yAdjust === "number";
19641
+ const row = Math.floor(index / 3);
19642
+ const dy = hasCustomOffset ? 0 : DEFAULT_VERTICAL_OFFSET - row * DEFAULT_ROW_GAP;
19643
+ return reference.y + dy;
19644
+ };
19645
+ var getCalloutItems = (chart, area) => {
19646
+ const overlappingIds = getOverlappingCalloutIds(chart);
19647
+ return ((chart?.options)?.annotations?.annotationsData ?? []).flatMap((annotation, index) => {
19648
+ const calloutCfg = annotation?.labelConfig?.callout ?? {};
19649
+ const id = `callout-annotation-${index}`;
19650
+ if (!calloutCfg?.enabled) return [];
19651
+ if (calloutCfg?.onlyWhenOverlapping && !overlappingIds.has(id)) return [];
19652
+ const size = estimateLabelSize(chart, annotation, calloutCfg);
19653
+ const xScale = getXScale(chart, getAxisId(annotation, "x"));
19654
+ const yScale = getYScale(chart, getAxisId(annotation, "y"));
19655
+ const reference = getReferencePoint$1(chart, area, annotation);
19656
+ const preferredX = getPreferredX(chart, area, annotation, calloutCfg, size.width);
19657
+ const preferredY = getPreferredY(chart, area, annotation, calloutCfg, index);
19658
+ const hasStoredX = typeof calloutCfg?.xValue === "number" || !xScale;
19659
+ const hasStoredY = typeof calloutCfg?.yValue === "number" || !yScale;
19660
+ const side = reference.side;
19661
+ const margin = calloutCfg?.margin ?? DEFAULT_MARGIN;
19662
+ return [{
19663
+ annotation,
19664
+ calloutCfg,
19665
+ id,
19666
+ index,
19667
+ preferredX,
19668
+ preferredY,
19669
+ width: size.width,
19670
+ height: size.height,
19671
+ isFixed: hasStoredX && hasStoredY,
19672
+ overlapOnly: Boolean(calloutCfg?.onlyWhenOverlapping),
19673
+ minX: area.left + margin + size.width / 2,
19674
+ maxX: area.right - margin - size.width / 2,
19675
+ minY: area.top + margin + size.height / 2,
19676
+ maxY: area.bottom - margin - size.height / 2,
19677
+ side
19678
+ }];
19679
+ });
19428
19680
  };
19429
- var resolveAnchorX = (chart, refAnnotation) => {
19430
- if (isNumber$1(refAnnotation?.xValue)) return refAnnotation.xValue;
19431
- if (isNumber$1(refAnnotation?.xMin) && isNumber$1(refAnnotation?.xMax)) return (refAnnotation.xMin + refAnnotation.xMax) / 2;
19432
- const axisValue = getAxisAlignedValue(refAnnotation, "x");
19433
- if (isNumber$1(axisValue)) return axisValue;
19434
- if (!chart) return 0;
19435
- const xScale = resolveXScale(chart, refAnnotation);
19436
- const area = chart.chartArea;
19437
- if (!xScale || !area) return 0;
19438
- return xScale.getValueForPixel((area.left + area.right) / 2);
19439
- };
19440
- var resolveAnchorY = (chart, refAnnotation) => {
19441
- if (isNumber$1(refAnnotation?.yValue)) return refAnnotation.yValue;
19442
- if (isNumber$1(refAnnotation?.yMin) && isNumber$1(refAnnotation?.yMax)) return (refAnnotation.yMin + refAnnotation.yMax) / 2;
19443
- const axisValue = getAxisAlignedValue(refAnnotation, "y");
19444
- if (isNumber$1(axisValue)) return axisValue;
19445
- if (!chart) return 0;
19446
- const yScale = resolveYScale(chart, refAnnotation);
19447
- const area = chart.chartArea;
19448
- if (!yScale || !area) return 0;
19449
- return yScale.getValueForPixel((area.top + area.bottom) / 2);
19681
+ var hasVerticalOverlap = (items) => {
19682
+ const sorted = [...items].sort((left, right) => left.preferredY - right.preferredY);
19683
+ for (let index = 1; index < sorted.length; index += 1) {
19684
+ const previous = sorted[index - 1];
19685
+ const current = sorted[index];
19686
+ const gap = previous.height / 2 + current.height / 2 + DEFAULT_ITEM_GAP;
19687
+ if (current.preferredY - previous.preferredY < gap) return true;
19688
+ }
19689
+ return false;
19450
19690
  };
19451
- var getDefaultXDirection = (refAnnotation, index) => {
19452
- switch (refAnnotation?.labelConfig?.position) {
19453
- case "left":
19454
- case "top-left":
19455
- case "bottom-left": return -1;
19456
- case "right":
19457
- case "top-right":
19458
- case "bottom-right": return 1;
19459
- default: return index % 2 === 0 ? 1 : -1;
19691
+ var overlapsOccupiedRect = (centerY, height, occupiedRects) => {
19692
+ const top = centerY - height / 2;
19693
+ const bottom = centerY + height / 2;
19694
+ return occupiedRects.find((rect) => !(bottom <= rect.top || top >= rect.bottom));
19695
+ };
19696
+ var layoutSide = (items, occupiedRects = []) => {
19697
+ const sorted = [...items].sort((left, right) => left.preferredY - right.preferredY);
19698
+ const result = /* @__PURE__ */ new Map();
19699
+ if (!sorted.filter((item) => !item.isFixed).length) {
19700
+ sorted.forEach((item) => {
19701
+ result.set(item.id, {
19702
+ xValue: item.preferredX,
19703
+ yValue: item.preferredY
19704
+ });
19705
+ });
19706
+ return result;
19460
19707
  }
19708
+ const shouldReflow = sorted.some((item) => !item.overlapOnly) || hasVerticalOverlap(sorted);
19709
+ sorted.forEach((item) => {
19710
+ result.set(item.id, {
19711
+ xValue: item.preferredX,
19712
+ yValue: clamp$1(item.preferredY, item.minY, item.maxY)
19713
+ });
19714
+ });
19715
+ if (!shouldReflow) return result;
19716
+ let previousBottom = Number.NEGATIVE_INFINITY;
19717
+ sorted.forEach((item) => {
19718
+ if (item.isFixed) {
19719
+ const yValue = clamp$1(item.preferredY, item.minY, item.maxY);
19720
+ result.set(item.id, {
19721
+ xValue: item.preferredX,
19722
+ yValue
19723
+ });
19724
+ previousBottom = yValue + item.height / 2;
19725
+ return;
19726
+ }
19727
+ const minCenter = Math.max(item.minY, previousBottom + item.height / 2 + DEFAULT_ITEM_GAP);
19728
+ let yValue = clamp$1(item.preferredY, minCenter, item.maxY);
19729
+ let overlappingRect = overlapsOccupiedRect(yValue, item.height, occupiedRects);
19730
+ while (overlappingRect) {
19731
+ yValue = clamp$1(overlappingRect.bottom + DEFAULT_ITEM_GAP + item.height / 2, item.minY, item.maxY);
19732
+ overlappingRect = overlapsOccupiedRect(yValue, item.height, occupiedRects);
19733
+ }
19734
+ result.set(item.id, {
19735
+ xValue: item.preferredX,
19736
+ yValue
19737
+ });
19738
+ previousBottom = yValue + item.height / 2;
19739
+ });
19740
+ let nextTop = Number.POSITIVE_INFINITY;
19741
+ [...sorted].reverse().forEach((item) => {
19742
+ const current = result.get(item.id);
19743
+ if (!current) return;
19744
+ if (item.isFixed) {
19745
+ nextTop = current.yValue - item.height / 2;
19746
+ return;
19747
+ }
19748
+ const maxCenter = Math.min(item.maxY, nextTop - item.height / 2 - DEFAULT_ITEM_GAP);
19749
+ const yValue = clamp$1(current.yValue, item.minY, maxCenter);
19750
+ result.set(item.id, {
19751
+ ...current,
19752
+ yValue
19753
+ });
19754
+ nextTop = yValue - item.height / 2;
19755
+ });
19756
+ sorted.forEach((item, index) => {
19757
+ const current = result.get(item.id);
19758
+ if (!current || item.isFixed) return;
19759
+ const spreadOffset = Math.min(index * DEFAULT_SIDE_SPREAD_STEP, DEFAULT_SIDE_SPREAD_MAX);
19760
+ const direction = item.side === "right" ? -1 : 1;
19761
+ const xValue = clamp$1(current.xValue + direction * spreadOffset, item.minX, item.maxX);
19762
+ result.set(item.id, {
19763
+ ...current,
19764
+ xValue
19765
+ });
19766
+ });
19767
+ return result;
19461
19768
  };
19462
- var getCalloutLayoutState = (chart, index) => {
19463
- const c = chart;
19464
- if (!c.__calloutLayoutState || index === 0) c.__calloutLayoutState = {
19465
- byIndex: {},
19466
- leftLastPx: Number.NEGATIVE_INFINITY,
19467
- rightLastPx: Number.NEGATIVE_INFINITY
19769
+ var getCalloutLayoutPixels = (chart) => {
19770
+ const area = chart?.chartArea;
19771
+ if (!chart || !area) return /* @__PURE__ */ new Map();
19772
+ const occupiedRects = getOccupiedOriginalLabelRects(chart, area, getOverlappingCalloutIds(chart));
19773
+ const grouped = getCalloutItems(chart, area).map((item) => {
19774
+ const xValue = clamp$1(item.preferredX, item.minX, item.maxX);
19775
+ return {
19776
+ ...item,
19777
+ preferredX: xValue
19778
+ };
19779
+ }).reduce((accumulator, item) => {
19780
+ accumulator[item.side].push(item);
19781
+ return accumulator;
19782
+ }, {
19783
+ left: [],
19784
+ right: []
19785
+ });
19786
+ const leftLayout = layoutSide(grouped.left, occupiedRects.filter((rect) => rect.side === "left").map((rect) => ({
19787
+ top: rect.top,
19788
+ bottom: rect.bottom
19789
+ })));
19790
+ const rightLayout = layoutSide(grouped.right, occupiedRects.filter((rect) => rect.side === "right").map((rect) => ({
19791
+ top: rect.top,
19792
+ bottom: rect.bottom
19793
+ })));
19794
+ return new Map([...leftLayout.entries(), ...rightLayout.entries()]);
19795
+ };
19796
+ var getCalloutLayout = (ctx, refAnnotation, index) => {
19797
+ const chart = getChartFromCtx$1(ctx);
19798
+ const xScale = chart ? getXScale(chart, getAxisId(refAnnotation, "x")) : void 0;
19799
+ const yScale = chart ? getYScale(chart, getAxisId(refAnnotation, "y")) : void 0;
19800
+ if (!chart || !yScale || !chart.chartArea) return null;
19801
+ if (!xScale && hasAxisAnchor(refAnnotation, "x")) return null;
19802
+ if (!xScale) return null;
19803
+ const layout = getCalloutLayoutPixels(chart).get(`callout-annotation-${index}`);
19804
+ if (!layout) return null;
19805
+ return {
19806
+ xValue: xScale.getValueForPixel(layout.xValue),
19807
+ yValue: yScale.getValueForPixel(layout.yValue)
19468
19808
  };
19469
- return c.__calloutLayoutState;
19470
19809
  };
19471
19810
  const resolveCalloutXValue = (ctx, refAnnotation, calloutCfg, index) => {
19472
- const chart = ctx?.chart;
19473
- const xVal = resolveAnchorX(chart, refAnnotation);
19811
+ const layout = getCalloutLayout(ctx, refAnnotation, index);
19812
+ if (layout) return layout.xValue;
19813
+ const chart = getChartFromCtx$1(ctx);
19814
+ const xVal = refAnnotation?.xValue;
19815
+ if (typeof xVal !== "number") return 0;
19474
19816
  if (!chart) return xVal;
19475
- const scale = resolveXScale(chart, refAnnotation);
19817
+ const scale = getXScale(chart, getAxisId(refAnnotation, "x"));
19476
19818
  if (!scale) return xVal;
19477
19819
  const hasCustomOffset = typeof calloutCfg?.xAdjust === "number";
19478
- const side = getDefaultXDirection(refAnnotation, index);
19479
- const dx = hasCustomOffset ? calloutCfg?.xAdjust : side * 60;
19480
- if (!isNumber$1(xVal)) return xVal;
19820
+ const side = index % 2 === 0 ? 1 : -1;
19821
+ const dx = hasCustomOffset ? calloutCfg?.xAdjust : side * DEFAULT_HORIZONTAL_OFFSET;
19481
19822
  const basePx = scale.getPixelForValue(xVal);
19482
- const cxBase = basePx + dx;
19483
19823
  const area = chart.chartArea;
19484
- if (!area) return scale.getValueForPixel(cxBase);
19485
- const margin = calloutCfg?.margin ?? 8;
19486
- const halfW = 160 / 2;
19487
- let cx = cxBase;
19488
- let adjDx = dx;
19489
- if (cx - halfW < area.left + margin) {
19490
- adjDx += area.left + margin + halfW - cx;
19491
- cx = basePx + adjDx;
19492
- } else if (cx + halfW > area.right - margin) {
19493
- adjDx += area.right - margin - halfW - cx;
19494
- cx = basePx + adjDx;
19495
- }
19496
- return scale.getValueForPixel(cx);
19824
+ const element = ctx?.element;
19825
+ if (!area || !element) return scale.getValueForPixel(basePx + dx);
19826
+ const margin = calloutCfg?.margin ?? DEFAULT_MARGIN;
19827
+ const halfWidth = (element?.width ?? 0) / 2;
19828
+ const clampedPx = clamp$1(basePx + dx, area.left + margin + halfWidth, area.right - margin - halfWidth);
19829
+ return scale.getValueForPixel(clampedPx);
19497
19830
  };
19498
19831
  const resolveCalloutYValue = (ctx, refAnnotation, calloutCfg, index) => {
19499
- const chart = ctx?.chart;
19500
- const yVal = resolveAnchorY(chart, refAnnotation);
19832
+ const layout = getCalloutLayout(ctx, refAnnotation, index);
19833
+ if (layout) return layout.yValue;
19834
+ const chart = getChartFromCtx$1(ctx);
19835
+ const yVal = refAnnotation?.yValue;
19836
+ if (typeof yVal !== "number") return 0;
19501
19837
  if (!chart) return yVal;
19502
- const scale = resolveYScale(chart, refAnnotation);
19838
+ const scale = getYScale(chart, getAxisId(refAnnotation, "y"));
19503
19839
  if (!scale) return yVal;
19504
19840
  const hasCustomOffset = typeof calloutCfg?.yAdjust === "number";
19505
- const layoutIndex = typeof calloutCfg?.layoutIndex === "number" ? calloutCfg.layoutIndex : index;
19506
- const row = Math.floor(layoutIndex / 2);
19507
- const dy = hasCustomOffset ? calloutCfg?.yAdjust : -40 - row * 22;
19508
- if (!isNumber$1(yVal)) return yVal;
19841
+ const row = Math.floor(index / 2);
19842
+ const dy = hasCustomOffset ? calloutCfg?.yAdjust : DEFAULT_VERTICAL_OFFSET - row * DEFAULT_ROW_GAP;
19509
19843
  const basePx = scale.getPixelForValue(yVal);
19510
- const laneBaseOffset = -24;
19511
- let cyBase = !hasCustomOffset ? basePx + laneBaseOffset : basePx + dy;
19512
19844
  const area = chart.chartArea;
19513
- if (!area) return scale.getValueForPixel(cyBase);
19514
- const margin = calloutCfg?.margin ?? 8;
19515
- const halfH = 18 / 2;
19516
- const minGapPx = calloutCfg?.minGapPx ?? halfH * 2 + 4;
19517
- let cy = cyBase;
19518
- let adjDy = !hasCustomOffset ? laneBaseOffset : dy;
19519
- if (!hasCustomOffset) {
19520
- const side = getDefaultXDirection(refAnnotation, layoutIndex);
19521
- const state = getCalloutLayoutState(chart, layoutIndex);
19522
- const cachedCy = state.byIndex[layoutIndex];
19523
- if (isNumber$1(cachedCy)) cy = cachedCy;
19524
- else {
19525
- if (side < 0) {
19526
- cy = Math.max(cy, state.leftLastPx + minGapPx);
19527
- state.leftLastPx = cy;
19528
- } else {
19529
- cy = Math.max(cy, state.rightLastPx + minGapPx);
19530
- state.rightLastPx = cy;
19531
- }
19532
- state.byIndex[layoutIndex] = cy;
19533
- }
19534
- }
19535
- if (cy - halfH < area.top + margin) {
19536
- adjDy += area.top + margin + halfH - cy;
19537
- cy = basePx + adjDy;
19538
- } else if (cy + halfH > area.bottom - margin) {
19539
- adjDy += area.bottom - margin - halfH - cy;
19540
- cy = basePx + adjDy;
19541
- }
19542
- return scale.getValueForPixel(cy);
19543
- };
19544
- var isInRange = (v, min, max) => v >= min && v <= max;
19845
+ const element = ctx?.element;
19846
+ if (!area || !element) return scale.getValueForPixel(basePx + dy);
19847
+ const margin = calloutCfg?.margin ?? DEFAULT_MARGIN;
19848
+ const halfHeight = (element?.height ?? 0) / 2;
19849
+ const clampedPx = clamp$1(basePx + dy, area.top + margin + halfHeight, area.bottom - margin - halfHeight);
19850
+ return scale.getValueForPixel(clampedPx);
19851
+ };
19852
+ var isInRange = (value, min, max) => value >= min && value <= max;
19545
19853
  const isCalloutAnchorInChartArea = (ctx, refAnnotation, margin = 0) => {
19546
- const chart = ctx?.chart;
19854
+ const chart = getChartFromCtx$1(ctx);
19547
19855
  const area = chart?.chartArea;
19548
19856
  if (!chart || !area) return true;
19549
- const xScale = resolveXScale(chart, refAnnotation);
19550
- const yScale = resolveYScale(chart, refAnnotation);
19551
- const xVal = resolveAnchorX(chart, refAnnotation);
19552
- const yVal = resolveAnchorY(chart, refAnnotation);
19553
- if (!xScale || !yScale) return false;
19554
- if (!isNumber$1(xVal) || !isNumber$1(yVal)) return false;
19555
- const x = xScale.getPixelForValue(xVal);
19556
- const y = yScale.getPixelForValue(yVal);
19857
+ const xScale = getXScale(chart, getAxisId(refAnnotation, "x"));
19858
+ const yScale = getYScale(chart, getAxisId(refAnnotation, "y"));
19859
+ const hasXValues = hasAxisAnchor(refAnnotation, "x");
19860
+ const hasYValues = hasAxisAnchor(refAnnotation, "y");
19861
+ if (hasXValues && !xScale || hasYValues && !yScale) return false;
19862
+ if (!hasXValues || !hasYValues) return false;
19863
+ const x = getAnchorPixel(chart, refAnnotation, "x", area);
19864
+ const y = getAnchorPixel(chart, refAnnotation, "y", area);
19557
19865
  return isInRange(x, area.left + margin, area.right - margin) && isInRange(y, area.top + margin, area.bottom - margin);
19558
19866
  };
19559
- const getChartFromCtx = (ctx) => {
19560
- if (!ctx) return;
19561
- if (ctx.chart?.persistenceId !== void 0) return ctx.chart;
19562
- if (ctx.chart?.chart?.persistenceId !== void 0) return ctx.chart.chart;
19563
- if (ctx.$context?.chart?.persistenceId !== void 0) return ctx.$context.chart;
19564
- return ctx.chart?.chart ?? ctx.chart ?? ctx.$context?.chart;
19565
- };
19566
- var isFiniteNumber$1 = (value) => {
19567
- return typeof value === "number" && Number.isFinite(value);
19568
- };
19569
- var parseFontSize = (font) => {
19570
- if (!font) return 12;
19571
- const match = font.match(/(\d+(?:\.\d+)?)px/);
19572
- return match ? Number(match[1]) : 12;
19573
- };
19574
- const getScaleByAxis = (chart, axis, ann) => {
19575
- const preferred = axis === "x" ? [ann?.xScaleID, ann?.annotationAxis] : [ann?.yScaleID, ann?.annotationAxis];
19576
- for (const key of preferred) if (key && chart?.scales?.[key]) return chart.scales[key];
19577
- if (axis === "x" && chart?.scales?.x) return chart.scales.x;
19578
- if (axis === "y" && chart?.scales?.y) return chart.scales.y;
19579
- const contains = axis === "x" ? "x" : "y";
19580
- const key = Object.keys(chart?.scales ?? {}).find((k) => k.toLowerCase().includes(contains));
19581
- return key ? chart.scales[key] : void 0;
19582
- };
19583
- const getAnchorValueFromData = (ann, axis) => {
19584
- const value = axis === "x" ? ann?.xValue : ann?.yValue;
19585
- if (isFiniteNumber$1(value)) return value;
19586
- const min = axis === "x" ? ann?.xMin : ann?.yMin;
19587
- const max = axis === "x" ? ann?.xMax : ann?.yMax;
19588
- if (isFiniteNumber$1(min) && isFiniteNumber$1(max)) return (min + max) / 2;
19589
- const annAxis = `${ann?.annotationAxis ?? ""}`.toLowerCase();
19590
- if (axis === "x" && annAxis.includes("x") && isFiniteNumber$1(ann?.value)) return ann.value;
19591
- if (axis === "y" && annAxis.includes("y") && isFiniteNumber$1(ann?.value)) return ann.value;
19592
- };
19593
- const getLabelSide = (ann) => {
19594
- const p = `${ann?.labelConfig?.position ?? ""}`.toLowerCase();
19595
- if (p.includes("left")) return "left";
19596
- if (p.includes("right")) return "right";
19597
- return "center";
19598
- };
19599
- const buildLikelyOverlapSet = (annotationsData) => {
19600
- const points = annotationsData.map((ann, idx) => ({
19601
- idx,
19602
- side: getLabelSide(ann),
19603
- x: getAnchorValueFromData(ann, "x"),
19604
- y: getAnchorValueFromData(ann, "y"),
19605
- label: ann?.label,
19606
- visible: ann?.display !== false
19607
- })).filter((p) => p.visible && !!p.label && isFiniteNumber$1(p.y));
19608
- if (!points.length) return /* @__PURE__ */ new Set();
19609
- const ys = points.map((p) => p.y);
19610
- const rangeY = Math.max(...ys) - Math.min(...ys);
19611
- const yThreshold = Math.max(120, rangeY * .06);
19612
- const xThreshold = .35;
19613
- const overlapSet = /* @__PURE__ */ new Set();
19614
- for (let i = 0; i < points.length; i += 1) for (let j = i + 1; j < points.length; j += 1) {
19615
- const a = points[i];
19616
- const b = points[j];
19617
- if (!(a.side === "center" || b.side === "center" || a.side === b.side)) continue;
19618
- const xClose = !isFiniteNumber$1(a.x) || !isFiniteNumber$1(b.x) ? true : Math.abs(a.x - b.x) <= xThreshold;
19619
- if (Math.abs(a.y - b.y) <= yThreshold && xClose) {
19620
- overlapSet.add(a.idx);
19621
- overlapSet.add(b.idx);
19622
- }
19623
- }
19624
- return overlapSet;
19625
- };
19626
- const buildOverlapCalloutYMap = (annotationsData, overlapSet) => {
19627
- const points = annotationsData.map((ann, idx) => ({
19628
- idx,
19629
- side: getLabelSide(ann),
19630
- y: getAnchorValueFromData(ann, "y")
19631
- })).filter((p) => overlapSet.has(p.idx) && typeof p.y === "number");
19632
- const yMap = /* @__PURE__ */ new Map();
19633
- if (!points.length) return yMap;
19634
- const ys = points.map((p) => p.y);
19635
- const minY = Math.min(...ys);
19636
- const maxY = Math.max(...ys);
19637
- const yRange = Math.max(maxY - minY, 1);
19638
- const minGap = Math.max(130, yRange * .03);
19639
- const minBound = minY - minGap * .5;
19640
- const maxBound = maxY + minGap * .5;
19641
- const groups = {
19642
- left: [],
19643
- right: []
19644
- };
19645
- points.forEach((p) => {
19646
- if (p.side === "right") {
19647
- groups.right.push({
19648
- idx: p.idx,
19649
- y: p.y
19650
- });
19651
- return;
19652
- }
19653
- groups.left.push({
19654
- idx: p.idx,
19655
- y: p.y
19656
- });
19657
- });
19658
- Object.keys(groups).forEach((side) => {
19659
- const group = groups[side].sort((a, b) => a.y - b.y);
19660
- if (!group.length) return;
19661
- const packed = group.map((item, i) => {
19662
- return i === 0 ? item.y : Math.max(item.y, group[i - 1].y + minGap);
19663
- });
19664
- const overflowBottom = packed[packed.length - 1] - maxBound;
19665
- if (overflowBottom > 0) for (let i = 0; i < packed.length; i += 1) packed[i] -= overflowBottom;
19666
- const overflowTop = minBound - packed[0];
19667
- if (overflowTop > 0) for (let i = 0; i < packed.length; i += 1) packed[i] += overflowTop;
19668
- group.forEach((item, i) => {
19669
- yMap.set(item.idx, packed[i]);
19670
- });
19671
- });
19672
- return yMap;
19673
- };
19674
- const getOverlapCalloutXAdjust = (ann) => {
19675
- const side = getLabelSide(ann);
19676
- if (side === "right") return -56;
19677
- if (side === "left") return 56;
19678
- return 0;
19679
- };
19680
- const hasOverlappingLabel = (ctx, refAnnotation, index, annotationsData) => {
19681
- const chart = getChartFromCtx(ctx);
19682
- if (!chart) return false;
19683
- const yScale = getScaleByAxis(chart, "y", refAnnotation);
19684
- const xScale = getScaleByAxis(chart, "x", refAnnotation);
19685
- if (!yScale || !xScale) return false;
19686
- const area = chart.chartArea;
19687
- if (!area) return false;
19688
- const yVal = getAnchorValueFromData(refAnnotation, "y");
19689
- const xVal = getAnchorValueFromData(refAnnotation, "x");
19690
- if (!isFiniteNumber$1(yVal)) return false;
19691
- const currentYPx = yScale.getPixelForValue(yVal);
19692
- const currentXPx = isFiniteNumber$1(xVal) ? xScale.getPixelForValue(xVal) : void 0;
19693
- const currentSide = getLabelSide(refAnnotation);
19694
- const currentFont = refAnnotation?.labelConfig?.font;
19695
- const currentHeight = parseFontSize(currentFont) + 6;
19696
- const nearTop = currentYPx - currentHeight <= area.top + 2;
19697
- const nearBottom = currentYPx + currentHeight >= area.bottom - 2;
19698
- if (nearTop || nearBottom) return true;
19699
- for (let i = 0; i < annotationsData.length; i += 1) {
19700
- if (i === index) continue;
19701
- const ann = annotationsData[i];
19702
- if (!ann?.label || ann?.display === false) continue;
19703
- const otherYScale = getScaleByAxis(chart, "y", ann);
19704
- const otherXScale = getScaleByAxis(chart, "x", ann);
19705
- if (!otherYScale || !otherXScale) continue;
19706
- const otherYVal = getAnchorValueFromData(ann, "y");
19707
- const otherXVal = getAnchorValueFromData(ann, "x");
19708
- if (!isFiniteNumber$1(otherYVal)) continue;
19709
- const otherYPx = otherYScale.getPixelForValue(otherYVal);
19710
- const otherXPx = isFiniteNumber$1(otherXVal) ? otherXScale.getPixelForValue(otherXVal) : void 0;
19711
- const otherSide = getLabelSide(ann);
19712
- const otherFont = ann?.labelConfig?.font;
19713
- const yThreshold = (currentHeight + (parseFontSize(otherFont) + 6)) / 2;
19714
- const xClose = !isFiniteNumber$1(currentXPx) || !isFiniteNumber$1(otherXPx) ? true : Math.abs(currentXPx - otherXPx) < 160;
19715
- if ((currentSide === "center" || otherSide === "center" || currentSide === otherSide) && Math.abs(currentYPx - otherYPx) < yThreshold && xClose) return true;
19716
- }
19717
- return false;
19718
- };
19719
19867
  var STORAGE_PREFIX = "chart_annotations";
19720
19868
  var getStorageKey = (persistenceId) => {
19721
19869
  return `${STORAGE_PREFIX}:${persistenceId}`;
@@ -19750,9 +19898,12 @@ const getAnnotationPosition = (persistenceId, annotationId) => {
19750
19898
  if (!persistenceId) return void 0;
19751
19899
  return readStorage(persistenceId)[annotationId];
19752
19900
  };
19753
- var isDragActive = (chart, annotation) => {
19754
- const pluginEnabled = Boolean(chart?.options?.plugins?.annotationDraggerPlugin?.enabled);
19755
- return Boolean(annotation?.enableDrag && pluginEnabled);
19901
+ const getChartFromCtx = (ctx) => {
19902
+ if (!ctx) return;
19903
+ if (ctx.chart?.persistenceId !== void 0) return ctx.chart;
19904
+ if (ctx.chart?.chart?.persistenceId !== void 0) return ctx.chart.chart;
19905
+ if (ctx.$context?.chart?.persistenceId !== void 0) return ctx.$context.chart;
19906
+ return ctx.chart?.chart ?? ctx.chart ?? ctx.$context?.chart;
19756
19907
  };
19757
19908
  var mapToBoxLikeLabelPosition = (position) => {
19758
19909
  switch (position) {
@@ -19792,8 +19943,7 @@ var mapToBoxLikeLabelPosition = (position) => {
19792
19943
  }
19793
19944
  };
19794
19945
  var handleLineEnter = (element, chart, annotation) => {
19795
- const canDrag = isDragActive(chart, annotation);
19796
- chart.canvas.style.cursor = canDrag ? CursorStyle.Pointer : CursorStyle.Initial;
19946
+ chart.canvas.style.cursor = CursorStyle.Pointer;
19797
19947
  if (element.options.scaleID?.includes(AxisType.X)) {
19798
19948
  if (element.options.label) element.options.label.xAdjust = chart.chartArea.left;
19799
19949
  }
@@ -19802,7 +19952,7 @@ var handleLineEnter = (element, chart, annotation) => {
19802
19952
  element.label.options.display = true;
19803
19953
  element.options.label.enabled = true;
19804
19954
  }
19805
- if (canDrag) {
19955
+ if (annotation?.enableDrag) {
19806
19956
  chart.hoveredAnnotationId = element.options.id || null;
19807
19957
  chart.update("none");
19808
19958
  }
@@ -19816,14 +19966,14 @@ var handleLineLeave = (element, chart, annotation) => {
19816
19966
  element.label.options.display = false;
19817
19967
  element.options.label.enabled = false;
19818
19968
  }
19819
- if (isDragActive(chart, annotation)) {
19969
+ if (annotation?.enableDrag) {
19820
19970
  chart.hoveredAnnotationId = null;
19821
19971
  chart.update("none");
19822
19972
  }
19823
19973
  chart.draw();
19824
19974
  };
19825
19975
  var handleBoxEnter = (element, chart, annotation) => {
19826
- if (isDragActive(chart, annotation)) {
19976
+ if (annotation?.enableDrag) {
19827
19977
  chart.hoveredAnnotationId = element.options.id || null;
19828
19978
  chart.update("none");
19829
19979
  element.options.borderWidth = BORDER_WIDTH.HOVERED;
@@ -19832,7 +19982,7 @@ var handleBoxEnter = (element, chart, annotation) => {
19832
19982
  chart.draw();
19833
19983
  };
19834
19984
  var handleBoxLeave = (element, chart, annotation) => {
19835
- if (isDragActive(chart, annotation)) {
19985
+ if (annotation?.enableDrag) {
19836
19986
  chart.hoveredAnnotationId = null;
19837
19987
  element.options.borderWidth = BORDER_WIDTH.INITIAL;
19838
19988
  chart.canvas.style.cursor = CursorStyle.Initial;
@@ -19841,7 +19991,7 @@ var handleBoxLeave = (element, chart, annotation) => {
19841
19991
  chart.draw();
19842
19992
  };
19843
19993
  var handlePointEnter = (element, chart, annotation) => {
19844
- if (isDragActive(chart, annotation)) {
19994
+ if (annotation?.enableDrag) {
19845
19995
  chart.hoveredAnnotationId = element.options.id || null;
19846
19996
  chart.update("none");
19847
19997
  element.options.borderWidth = BORDER_WIDTH.POINT_HOVERED;
@@ -19850,7 +20000,7 @@ var handlePointEnter = (element, chart, annotation) => {
19850
20000
  chart.draw();
19851
20001
  };
19852
20002
  var handlePointLeave = (element, chart, annotation) => {
19853
- if (isDragActive(chart, annotation)) {
20003
+ if (annotation?.enableDrag) {
19854
20004
  chart.hoveredAnnotationId = null;
19855
20005
  element.options.borderWidth = BORDER_WIDTH.INITIAL;
19856
20006
  chart.canvas.style.cursor = CursorStyle.Initial;
@@ -19859,7 +20009,7 @@ var handlePointLeave = (element, chart, annotation) => {
19859
20009
  chart.draw();
19860
20010
  };
19861
20011
  var handleLabelEnter = (element, chart, annotation) => {
19862
- if (isDragActive(chart, annotation)) {
20012
+ if (annotation?.enableDrag) {
19863
20013
  chart.hoveredAnnotationId = element.options.id || null;
19864
20014
  chart.update("none");
19865
20015
  chart.canvas.style.cursor = CursorStyle.Pointer;
@@ -19867,7 +20017,7 @@ var handleLabelEnter = (element, chart, annotation) => {
19867
20017
  chart.draw();
19868
20018
  };
19869
20019
  var handleLabelLeave = (_element, chart, annotation) => {
19870
- if (isDragActive(chart, annotation)) {
20020
+ if (annotation?.enableDrag) {
19871
20021
  chart.hoveredAnnotationId = null;
19872
20022
  chart.canvas.style.cursor = CursorStyle.Initial;
19873
20023
  chart.update("none");
@@ -19998,41 +20148,32 @@ const generateAnn = (ann, index) => {
19998
20148
  displayDragCoordinates: ann?.displayDragCoordinates ?? true
19999
20149
  };
20000
20150
  };
20001
- var getCalloutAnnotation = (refAnnotation, index, annotationsData, likelyOverlapSet, overlapCalloutYMap, layoutIndex) => {
20151
+ var getCalloutAnnotation = (refAnnotation, index) => {
20002
20152
  const generated = generateAnn(refAnnotation, index);
20003
20153
  if (!generated) return [];
20004
20154
  const { label, ...baseRest } = generated;
20005
20155
  const baseId = baseRest.id;
20006
20156
  const calloutCfg = refAnnotation?.labelConfig?.callout ?? {};
20007
- const onlyWhenOverlapping = calloutCfg?.onlyWhenOverlapping ?? false;
20008
- const hideBoxGlyphInOverlapMode = onlyWhenOverlapping && refAnnotation?.type === AnnotationType$1.BOX;
20009
- const staticShouldShowCallout = likelyOverlapSet.has(index);
20010
- const shouldShowCallout = (ctx) => {
20011
- return !onlyWhenOverlapping || staticShouldShowCallout || calloutCfg?.useRuntimeOverlap === true && hasOverlappingLabel(ctx, refAnnotation, index, annotationsData);
20012
- };
20013
- const canDisplayCallout = (ctx) => {
20014
- return (refAnnotation.display ?? true) && shouldShowCallout(ctx) && isCalloutAnchorInChartArea(ctx, refAnnotation, calloutCfg.margin ?? 0);
20015
- };
20016
20157
  const baseAnnotation = {
20017
20158
  ...baseRest,
20018
- backgroundColor: hideBoxGlyphInOverlapMode ? TRANSPARENT : baseRest?.backgroundColor,
20019
- borderWidth: hideBoxGlyphInOverlapMode ? BORDER_WIDTH.ZERO : baseRest?.borderWidth,
20020
- borderColor: hideBoxGlyphInOverlapMode ? TRANSPARENT : baseRest?.borderColor,
20021
20159
  label: {
20022
20160
  ...label,
20023
- display: (ctx) => (refAnnotation?.display ?? true) && (!calloutCfg?.enabled || !canDisplayCallout(ctx))
20161
+ display: (ctx) => {
20162
+ if (!(refAnnotation.display ?? true)) return false;
20163
+ if (!calloutCfg?.onlyWhenOverlapping) return false;
20164
+ return !isCalloutOverlapping(ctx, refAnnotation, index);
20165
+ }
20024
20166
  }
20025
20167
  };
20026
20168
  const color = calloutCfg?.color ?? "hsl(60, 10.34482759%, 12.5%)";
20027
20169
  const font = calloutCfg?.font ?? `12px "Roobert", "Noto Sans", sans-serif`;
20028
20170
  const borderColor = calloutCfg?.borderColor ?? "transparent";
20029
- const connectorStrokeStyle = calloutCfg.strokeStyle ?? (hideBoxGlyphInOverlapMode ? refAnnotation?.color ?? "hsl(60, 10.34482759%, 12.5%)" : baseAnnotation.borderColor);
20171
+ const strokeStyle = calloutCfg.strokeStyle ?? "rgba(120, 126, 138, 0.9)";
20030
20172
  const lineWidth = typeof calloutCfg.lineWidth === "number" ? calloutCfg.lineWidth : 1;
20031
- const startOffset = typeof calloutCfg.startOffset === "number" ? calloutCfg.startOffset : 6;
20032
- const endOffset = typeof calloutCfg.endOffset === "number" ? calloutCfg.endOffset : 1;
20173
+ const startOffset = typeof calloutCfg.startOffset === "number" ? calloutCfg.startOffset : 2;
20174
+ const endOffset = typeof calloutCfg.endOffset === "number" ? calloutCfg.endOffset : .5;
20033
20175
  const calloutId = `callout-annotation-${index}`;
20034
- const isCalloutDraggable = refAnnotation?.enableDrag !== false;
20035
- const calloutAnnotation = {
20176
+ return [baseAnnotation, {
20036
20177
  id: calloutId,
20037
20178
  type: "label",
20038
20179
  backgroundColor: "transparent",
@@ -20040,47 +20181,38 @@ var getCalloutAnnotation = (refAnnotation, index, annotationsData, likelyOverlap
20040
20181
  color: (ctx) => {
20041
20182
  const chart = getChartFromCtx(ctx);
20042
20183
  if (!chart) return color;
20043
- const isDragModeEnabled = Boolean(chart?.options?.plugins?.annotationDraggerPlugin?.enabled);
20044
- if (!isCalloutDraggable || !isDragModeEnabled) return color;
20045
- if (chart.hoveredAnnotationId === calloutId) return "#DB5B00";
20184
+ if (Boolean(chart?.options?.plugins?.annotationDraggerPlugin?.enabled) && chart.hoveredAnnotationId === calloutId) return "#DB5B00";
20046
20185
  return color;
20047
20186
  },
20048
20187
  font,
20049
20188
  borderColor,
20050
20189
  borderWidth: BORDER_WIDTH.INITIAL,
20051
20190
  padding: 5,
20052
- display: canDisplayCallout,
20191
+ display: (ctx) => {
20192
+ if (!(refAnnotation.display ?? true)) return false;
20193
+ if (!isCalloutAnchorInChartArea(ctx, refAnnotation, calloutCfg.margin ?? 0)) return false;
20194
+ return isCalloutOverlapping(ctx, refAnnotation, index);
20195
+ },
20053
20196
  xValue: (ctx) => {
20054
20197
  const persistenceId = getChartFromCtx(ctx)?.options?.persistenceId;
20055
20198
  const stored = getAnnotationPosition(persistenceId, `callout-annotation-${index}`);
20056
20199
  const explicit = typeof calloutCfg.xValue === "number" ? calloutCfg.xValue : void 0;
20057
- const overlapCalloutAdjust = onlyWhenOverlapping && !calloutCfg?.xAdjust ? getOverlapCalloutXAdjust(refAnnotation) : calloutCfg?.xAdjust;
20058
- return stored?.x ?? explicit ?? resolveCalloutXValue(ctx, refAnnotation, {
20059
- ...calloutCfg,
20060
- xAdjust: overlapCalloutAdjust,
20061
- layoutIndex
20062
- }, index);
20200
+ return stored?.x ?? explicit ?? resolveCalloutXValue(ctx, refAnnotation, calloutCfg, index);
20063
20201
  },
20064
20202
  yValue: (ctx) => {
20065
20203
  const persistenceId = getChartFromCtx(ctx)?.options?.persistenceId;
20066
20204
  const stored = getAnnotationPosition(persistenceId, `callout-annotation-${index}`);
20067
20205
  const explicit = typeof calloutCfg.yValue === "number" ? calloutCfg.yValue : void 0;
20068
- const overlapPackedY = onlyWhenOverlapping ? overlapCalloutYMap.get(index) : void 0;
20069
- const overlapCalloutYAdjust = onlyWhenOverlapping && !calloutCfg?.yAdjust ? 0 : calloutCfg?.yAdjust;
20070
- return stored?.y ?? explicit ?? overlapPackedY ?? resolveCalloutYValue(ctx, refAnnotation, {
20071
- ...calloutCfg,
20072
- yAdjust: overlapCalloutYAdjust,
20073
- layoutIndex
20074
- }, index);
20206
+ return stored?.y ?? explicit ?? resolveCalloutYValue(ctx, refAnnotation, calloutCfg, index);
20075
20207
  },
20076
20208
  resizable: false,
20077
20209
  displayDragCoordinates: false,
20078
- enableDrag: isCalloutDraggable,
20210
+ enableDrag: true,
20079
20211
  enter: ({ element }, { chart }) => {
20080
- return handleLabelEnter(element, chart, { enableDrag: isCalloutDraggable });
20212
+ return handleLabelEnter(element, chart, { enableDrag: Boolean(chart?.options?.plugins?.annotationDraggerPlugin?.enabled) });
20081
20213
  },
20082
20214
  leave: ({ element }, { chart }) => {
20083
- return handleLabelLeave(element, chart, { enableDrag: isCalloutDraggable });
20215
+ return handleLabelLeave(element, chart, { enableDrag: Boolean(chart?.options?.plugins?.annotationDraggerPlugin?.enabled) });
20084
20216
  },
20085
20217
  onDragStart: calloutCfg?.onDragStart ? () => (coords) => {
20086
20218
  return calloutCfg.onDragStart?.(coords, refAnnotation);
@@ -20094,58 +20226,22 @@ var getCalloutAnnotation = (refAnnotation, index, annotationsData, likelyOverlap
20094
20226
  calloutConnector: {
20095
20227
  enabled: true,
20096
20228
  fromId: baseId,
20097
- strokeStyle: connectorStrokeStyle,
20229
+ strokeStyle,
20098
20230
  lineWidth,
20099
20231
  startOffset,
20100
- endOffset
20232
+ endOffset,
20233
+ anchor: {
20234
+ position: refAnnotation?.labelConfig?.position,
20235
+ xAdjust: refAnnotation?.labelConfig?.xAdjust,
20236
+ yAdjust: refAnnotation?.labelConfig?.yAdjust,
20237
+ labelOffsetPx: refAnnotation?.labelOffsetPx
20238
+ }
20101
20239
  }
20102
- };
20103
- const anchorX = getAnchorValueFromData(refAnnotation, "x");
20104
- const anchorY = getAnchorValueFromData(refAnnotation, "y");
20105
- const originTickAnnotation = typeof anchorX === "number" && typeof anchorY === "number" ? {
20106
- id: `callout-origin-tick-${index}`,
20107
- type: "line",
20108
- xMin: anchorX,
20109
- xMax: (ctx) => {
20110
- const chart = getChartFromCtx(ctx);
20111
- const xScale = chart ? getScaleByAxis(chart, "x", refAnnotation) : null;
20112
- if (!xScale) return anchorX;
20113
- const px = xScale.getPixelForValue(anchorX);
20114
- const tickLength = calloutCfg?.originTickLengthPx ?? 8;
20115
- return xScale.getValueForPixel(px + tickLength);
20116
- },
20117
- yMin: anchorY,
20118
- yMax: anchorY,
20119
- borderColor: calloutCfg?.originTickColor ?? connectorStrokeStyle,
20120
- borderWidth: calloutCfg?.originTickWidth ?? 1,
20121
- borderDash: [],
20122
- adjustScaleRange: false,
20123
- label: {
20124
- display: false,
20125
- content: ""
20126
- },
20127
- display: (ctx) => onlyWhenOverlapping && (refAnnotation.display ?? true) && shouldShowCallout(ctx),
20128
- resizable: false,
20129
- displayDragCoordinates: false,
20130
- enableDrag: false
20131
- } : null;
20132
- if (!onlyWhenOverlapping) return [baseAnnotation, calloutAnnotation];
20133
- return originTickAnnotation ? [
20134
- baseAnnotation,
20135
- originTickAnnotation,
20136
- calloutAnnotation
20137
- ] : [baseAnnotation, calloutAnnotation];
20240
+ }];
20138
20241
  };
20139
20242
  var generateAnnotations = (annotationsData) => {
20140
- const likelyOverlapSet = buildLikelyOverlapSet(annotationsData);
20141
- const overlapCalloutYMap = buildOverlapCalloutYMap(annotationsData, likelyOverlapSet);
20142
- let calloutLayoutIndex = 0;
20143
20243
  return annotationsData?.flatMap((ann, idx) => {
20144
- if (ann?.labelConfig?.callout?.enabled ?? false) {
20145
- const annotation = getCalloutAnnotation(ann, idx, annotationsData, likelyOverlapSet, overlapCalloutYMap, calloutLayoutIndex);
20146
- calloutLayoutIndex += 1;
20147
- return annotation;
20148
- }
20244
+ if (ann?.labelConfig?.callout?.enabled ?? false) return getCalloutAnnotation(ann, idx);
20149
20245
  return generateAnn(ann, idx);
20150
20246
  });
20151
20247
  };
@@ -20844,52 +20940,127 @@ const annotationDraggerPlugin = {
20844
20940
  }
20845
20941
  }
20846
20942
  };
20847
- var getRadius = (el) => {
20848
- if (typeof el.options?.radius === "number") return el.options.radius;
20849
- if (typeof el.width === "number" && typeof el.height === "number") return Math.min(el.width, el.height) / 2;
20850
- return 0;
20943
+ var getRectangleEdgePoint = (el, targetX, targetY, offset) => {
20944
+ const width = el?.width ?? 0;
20945
+ const height = el?.height ?? 0;
20946
+ if (width <= 0 || height <= 0) return {
20947
+ x: el.centerX,
20948
+ y: el.centerY
20949
+ };
20950
+ const halfWidth = width / 2;
20951
+ const halfHeight = height / 2;
20952
+ const dx = targetX - el.centerX;
20953
+ const dy = targetY - el.centerY;
20954
+ const length = Math.sqrt(dx * dx + dy * dy) || 1;
20955
+ const ux = dx / length;
20956
+ const uy = dy / length;
20957
+ const tx = Math.abs(ux) > 1e-6 ? halfWidth / Math.abs(ux) : Number.POSITIVE_INFINITY;
20958
+ const ty = Math.abs(uy) > 1e-6 ? halfHeight / Math.abs(uy) : Number.POSITIVE_INFINITY;
20959
+ const distance = Math.min(tx, ty) + offset;
20960
+ return {
20961
+ x: el.centerX + ux * distance,
20962
+ y: el.centerY + uy * distance
20963
+ };
20964
+ };
20965
+ var getLabelEdgePoint = (labelEl, startX, endOffset) => {
20966
+ const width = labelEl?.width ?? 0;
20967
+ const height = labelEl?.height ?? 0;
20968
+ if (width <= 0 || height <= 0) return {
20969
+ x: labelEl.centerX,
20970
+ y: labelEl.centerY
20971
+ };
20972
+ const halfWidth = width / 2;
20973
+ const direction = startX <= labelEl.centerX ? -1 : 1;
20974
+ return {
20975
+ x: labelEl.centerX + direction * (halfWidth + endOffset),
20976
+ y: labelEl.centerY
20977
+ };
20978
+ };
20979
+ var getReferencePoint = (fromEl, connector) => {
20980
+ const { anchor } = connector;
20981
+ if (!anchor) return {
20982
+ x: fromEl.centerX,
20983
+ y: fromEl.centerY
20984
+ };
20985
+ const width = fromEl?.width ?? 0;
20986
+ const height = fromEl?.height ?? 0;
20987
+ const radius = fromEl?.options?.radius ?? 0;
20988
+ const halfWidth = width / 2;
20989
+ const halfHeight = height / 2;
20990
+ const labelOffset = anchor?.labelOffsetPx ?? 14;
20991
+ const xAdjust = anchor?.xAdjust ?? 0;
20992
+ const yAdjust = anchor?.yAdjust ?? 0;
20993
+ const position = anchor?.position ?? "top";
20994
+ let x = fromEl.centerX;
20995
+ let y = fromEl.centerY;
20996
+ switch (position) {
20997
+ case "left":
20998
+ x -= Math.max(halfWidth, radius) + labelOffset;
20999
+ break;
21000
+ case "right":
21001
+ x += Math.max(halfWidth, radius) + labelOffset;
21002
+ break;
21003
+ case "bottom":
21004
+ y += Math.max(halfHeight, radius) + labelOffset;
21005
+ break;
21006
+ case "top-left":
21007
+ x -= halfWidth;
21008
+ y -= halfHeight;
21009
+ break;
21010
+ case "top-right":
21011
+ x += halfWidth;
21012
+ y -= halfHeight;
21013
+ break;
21014
+ case "bottom-left":
21015
+ x -= halfWidth;
21016
+ y += halfHeight;
21017
+ break;
21018
+ case "bottom-right":
21019
+ x += halfWidth;
21020
+ y += halfHeight;
21021
+ break;
21022
+ default:
21023
+ y -= Math.max(halfHeight, radius) + labelOffset;
21024
+ break;
21025
+ }
21026
+ return {
21027
+ x: x + xAdjust,
21028
+ y: y + yAdjust
21029
+ };
20851
21030
  };
20852
21031
  const computeConnectorPoints = (fromEl, labelEl, connector) => {
20853
21032
  const x1c = fromEl?.centerX;
20854
21033
  const y1c = fromEl?.centerY;
20855
21034
  const cx = labelEl?.centerX;
20856
21035
  const cy = labelEl?.centerY;
20857
- const labelWidth = labelEl?.width ?? 0;
20858
- const labelHeight = labelEl?.height ?? 0;
20859
21036
  const ux = cx - x1c;
20860
21037
  const uy = cy - y1c;
20861
21038
  const len = Math.sqrt(ux * ux + uy * uy) || 1;
20862
21039
  const dx = ux / len;
20863
21040
  const dy = uy / len;
20864
- const fromRadius = getRadius(fromEl);
20865
- const startOffset = (connector?.startOffset ?? 6) + fromRadius;
20866
- const startX = x1c + dx * startOffset;
20867
- const startY = y1c + dy * startOffset;
20868
- let endX = cx;
20869
- let endY = cy;
20870
- if (labelWidth > 0 && labelHeight > 0) {
20871
- const hw = labelWidth / 2;
20872
- const hh = labelHeight / 2;
20873
- const vx = x1c - cx;
20874
- const vy = y1c - cy;
20875
- const vlen = Math.sqrt(vx * vx + vy * vy) || 1;
20876
- const vxn = vx / vlen;
20877
- const vyn = vy / vlen;
20878
- const tx = Math.abs(vxn) > 1e-6 ? hw / Math.abs(vxn) : Number.POSITIVE_INFINITY;
20879
- const ty = Math.abs(vyn) > 1e-6 ? hh / Math.abs(vyn) : Number.POSITIVE_INFINITY;
20880
- const total = Math.min(tx, ty) + (connector.endOffset ?? 1);
20881
- endX = cx + vxn * total;
20882
- endY = cy + vyn * total;
20883
- } else {
20884
- const endOffset = connector.endOffset ?? 1;
20885
- endX = cx - dx * endOffset;
20886
- endY = cy - dy * endOffset;
20887
- }
21041
+ const startOffset = connector?.startOffset ?? 6;
21042
+ const pointRadius = fromEl?.options?.radius;
21043
+ const isPointAnchor = typeof pointRadius === "number";
21044
+ const referencePoint = connector?.anchor && !isPointAnchor ? getReferencePoint(fromEl, connector) : isPointAnchor ? {
21045
+ x: x1c + dx * (pointRadius + startOffset),
21046
+ y: y1c + dy * (pointRadius + startOffset)
21047
+ } : getRectangleEdgePoint(fromEl, cx, cy, startOffset);
21048
+ const endOffset = connector?.endOffset ?? 1;
21049
+ const end = typeof labelEl?.width === "number" && typeof labelEl?.height === "number" ? getLabelEdgePoint(labelEl, referencePoint.x, endOffset) : {
21050
+ x: cx - dx * endOffset,
21051
+ y: cy - dy * endOffset
21052
+ };
21053
+ const tickHalf = isPointAnchor ? 0 : (connector?.anchor?.tickSize ?? 8) / 2;
21054
+ const lineDirection = end.x >= referencePoint.x ? 1 : -1;
20888
21055
  return {
20889
- startX,
20890
- startY,
20891
- endX,
20892
- endY
21056
+ startX: referencePoint.x + lineDirection * tickHalf,
21057
+ startY: referencePoint.y,
21058
+ endX: end.x,
21059
+ endY: end.y,
21060
+ tickStartX: referencePoint.x - tickHalf,
21061
+ tickStartY: referencePoint.y,
21062
+ tickEndX: referencePoint.x + tickHalf,
21063
+ tickEndY: referencePoint.y
20893
21064
  };
20894
21065
  };
20895
21066
  const calloutConnectorPlugin = {
@@ -20910,17 +21081,24 @@ const calloutConnectorPlugin = {
20910
21081
  const fromEl = elements.find((el) => el.options && el.options.id === connector.fromId);
20911
21082
  if (!labelEl || !fromEl) return;
20912
21083
  if (labelEl.options?.display === false) return;
20913
- const { startX, startY, endX, endY } = computeConnectorPoints(fromEl, labelEl, connector) ?? {};
21084
+ const { startX, startY, endX, endY, tickStartX, tickStartY, tickEndX, tickEndY } = computeConnectorPoints(fromEl, labelEl, connector) ?? {};
20914
21085
  const ctx = chart?.ctx;
21086
+ const strokeStyle = connector?.strokeStyle ?? "rgba(120, 126, 138, 0.9)";
20915
21087
  ctx?.save?.();
20916
21088
  ctx?.beginPath?.();
20917
21089
  ctx?.moveTo?.(startX, startY);
20918
21090
  ctx?.lineTo?.(endX, endY);
20919
21091
  ctx.lineWidth = connector?.lineWidth ?? 1;
20920
- ctx.strokeStyle = connector?.strokeStyle ?? "rgba(120,120,120,0.9)";
21092
+ ctx.strokeStyle = strokeStyle;
20921
21093
  if (connector?.lineDash && connector?.lineDash?.length) ctx?.setLineDash?.(connector.lineDash);
20922
21094
  ctx.lineCap = "round";
20923
21095
  ctx?.stroke?.();
21096
+ ctx?.beginPath?.();
21097
+ ctx?.moveTo?.(tickStartX, tickStartY);
21098
+ ctx?.lineTo?.(tickEndX, tickEndY);
21099
+ ctx.lineWidth = Math.max(connector?.lineWidth ?? 1, 1);
21100
+ ctx.strokeStyle = strokeStyle;
21101
+ ctx?.stroke?.();
20924
21102
  ctx?.restore?.();
20925
21103
  });
20926
21104
  }