import { hexToRgba as e, niceScale as t, scaleLinear as n } from "../utils.js";
import { t as r } from "../../series-DMc2ZYyj.js";
import { drawXAxis as i, drawYAxis as a } from "../axis.js";
//#region src/charts/scatter/index.ts
var o = 4, s = 2, c = 20, l = class extends r {
	config;
	plottedPoints;
	highlightedPoint;
	constructor(e) {
		super(e), this.config = e, this.plottedPoints = [], this.highlightedPoint = null, this.render();
	}
	computeDataBounds(e) {
		let t = Infinity, n = -Infinity, r = Infinity, i = -Infinity, a = /* @__PURE__ */ new Set();
		for (let o of e) for (let e of o.data) {
			let o = typeof e.x == "number" ? e.x : parseFloat(e.x);
			isNaN(o) || (o < t && (t = o), o > n && (n = o)), a.add(String(e.x)), e.y < r && (r = e.y), e.y > i && (i = e.y);
		}
		return t === Infinity && (t = 0, n = 1), r === Infinity && (r = 0, i = 1), {
			xMin: this.config.xAxis?.min ?? t,
			xMax: this.config.xAxis?.max ?? n,
			yMin: this.config.yAxis?.min ?? r,
			yMax: this.config.yAxis?.max ?? i,
			xLabels: Array.from(a)
		};
	}
	computeTrendLine(e) {
		if (e.length < 2) return null;
		let t = [], n = [];
		for (let r of e) {
			let e = typeof r.x == "number" ? r.x : parseFloat(r.x);
			isNaN(e) || (t.push(e), n.push(r.y));
		}
		let r = t.length;
		if (r < 2) return null;
		let i = 0, a = 0, o = 0, s = 0;
		for (let e = 0; e < r; e++) {
			let r = t[e], c = n[e];
			r === void 0 || c === void 0 || (i += r, a += c, o += r * c, s += r * r);
		}
		let c = r * s - i * i;
		if (c === 0) return null;
		let l = (r * o - i * a) / c;
		return {
			slope: l,
			intercept: (a - l * i) / r
		};
	}
	render() {
		this.clear(), this.plottedPoints = [];
		let r = this.getVisibleSeries(), { xMin: c, xMax: l, yMin: u, yMax: d, xLabels: f } = this.computeDataBounds(r), p = this.plotArea, m = this.config.xAxis?.tickCount ?? 5, h = this.config.yAxis?.tickCount ?? 5, g = t(c, l, m), _ = t(u, d, h), v = f.length > 0 ? f : this.generateNumericLabels(g.min, g.max, g.step);
		i(this.ctx, p, v, this.config.xAxis), a(this.ctx, p, _.min, _.max, this.config.yAxis);
		let y = n([g.min, g.max], [p.x, p.x + p.width]), b = n([_.min, _.max], [p.y + p.height, p.y]), x = this.config.dotRadius ?? o;
		if (this.config.showTrendLine === !0) for (let t of r) {
			let n = this.computeTrendLine(t.data);
			if (n !== null) {
				let r = g.min, i = n.slope * r + n.intercept, a = g.max, o = n.slope * a + n.intercept;
				this.ctx.beginPath(), this.ctx.moveTo(y(r), b(i)), this.ctx.lineTo(y(a), b(o)), this.ctx.strokeStyle = e(t.color, .4), this.ctx.lineWidth = 1.5, this.ctx.setLineDash([6, 4]), this.ctx.stroke(), this.ctx.setLineDash([]);
			}
		}
		for (let t of r) for (let n of t.data) {
			let r = typeof n.x == "number" ? n.x : parseFloat(n.x);
			if (isNaN(r)) continue;
			let i = y(r), a = b(n.y), o = this.highlightedPoint !== null && this.highlightedPoint.px === i && this.highlightedPoint.py === a;
			this.plottedPoints.push({
				px: i,
				py: a,
				dataPoint: n,
				seriesName: t.name,
				color: t.color
			}), this.ctx.beginPath();
			let c = o ? x + s : x;
			this.ctx.arc(i, a, c, 0, Math.PI * 2), this.ctx.fillStyle = o ? e(t.color, 1) : e(t.color, .7), this.ctx.fill(), this.ctx.strokeStyle = t.color, this.ctx.lineWidth = 1.5, this.ctx.stroke();
		}
		this.updateSeriesLegend();
	}
	generateNumericLabels(e, t, n) {
		let r = [];
		for (let i = e; i <= t + n * .001; i += n) r.push(String(Math.round(i * 1e3) / 1e3));
		return r;
	}
	handleMouseMove(e) {
		let t = this.canvas.getBoundingClientRect(), n = e.clientX - t.left, r = e.clientY - t.top, i = null, a = Infinity;
		for (let e of this.plottedPoints) {
			let t = Math.sqrt((n - e.px) ** 2 + (r - e.py) ** 2);
			t < a && (a = t, i = e);
		}
		let o = this.highlightedPoint;
		if (i !== null && a < c) {
			if (this.highlightedPoint = i, this.tooltip !== null) {
				let e;
				e = this.config.tooltip?.format === void 0 ? `${i.seriesName}: (${String(i.dataPoint.x)}, ${String(i.dataPoint.y)})` : this.config.tooltip.format(i.dataPoint.y, String(i.dataPoint.x), i.seriesName), this.tooltip.show(i.px, i.py, e);
			}
		} else this.highlightedPoint = null, this.tooltip !== null && this.tooltip.hide();
		this.highlightedPoint !== o && this.render();
	}
	handleMouseLeave() {
		this.highlightedPoint !== null && (this.highlightedPoint = null, this.render()), this.tooltip !== null && this.tooltip.hide();
	}
	update(e) {
		this.series = this.resolveSeries(e), this.render();
	}
};
//#endregion
export { l as ScatterChart };

//# sourceMappingURL=index.js.map