import { bindGlobalStyle, getGlobalStylesId, HtmlVar, RefProps } from 'lupine.components';
import { chartCommonCss, getChartColor, BasicChartProps } from './chart-utils';
import { Tooltip } from '../tooltip';
export type ScatterChartProps = BasicChartProps & {
xAxisFormatter?: (value: number) => string;
yAxisFormatter?: (value: number) => string;
};
export const ScatterChart = (props: ScatterChartProps) => {
const globalCssId = getGlobalStylesId(chartCommonCss);
bindGlobalStyle(globalCssId, chartCommonCss);
const showLegend = props.showLegend !== false;
const series = props.data.series;
// Scatter chart might use numeric labels or categorical labels for X.
// Assuming equal spacing for labels if provided. If not, data could be `{x, y}` but to keep ChartData simple we use `data: number[]` mapped to `labels` index.
const labels = props.data.labels;
if (!series || series.length === 0 || labels.length === 0) {
return
No data
;
}
// Find max value for Y axis scale
let maxVal = 0;
series.forEach((s) => {
s.data.forEach((val) => {
if (val > maxVal) maxVal = val;
});
});
const tickCount = 5;
const order = Math.floor(Math.log10(maxVal || 1));
const magnitude = Math.pow(10, order);
const niceStep = Math.ceil(maxVal / magnitude / tickCount) * magnitude;
const niceMax = niceStep * tickCount;
const yTicks = Array.from({ length: tickCount + 1 }, (_, i) => parseFloat((i * niceStep).toPrecision(12)));
const renderChart = (viewBoxWidth: number, viewBoxHeight: number) => {
// Layout parameters
const padding = { top: 20, right: 60, bottom: 40, left: 60 };
const chartWidth = viewBoxWidth - padding.left - padding.right;
const chartHeight = viewBoxHeight - padding.top - padding.bottom;
const pointStep = chartWidth / Math.max(1, labels.length - 1);
const formatY = props.yAxisFormatter || ((val) => val.toString());
// Render Y Axis
const renderYAxis = () => {
return yTicks.map((val) => {
const y = padding.top + chartHeight - (val / niceMax) * chartHeight;
return (
{formatY(val)}
);
});
};
// Render X Axis
const renderXAxis = () => {
return labels.map((label, index) => {
const x = padding.left + index * pointStep;
const y = viewBoxHeight - padding.bottom + 20;
return (
{label}
);
});
};
// Render Points
const renderPoints = () => {
const points: any[] = [];
series.forEach((s, sIndex) => {
const color = s.color || getChartColor(sIndex);
const coordinates = s.data.map((val, lIndex) => {
const x = padding.left + lIndex * pointStep;
// Add some jitter for scatter if needed? Basic scatter uses exactly the values.
const y = padding.top + chartHeight - (val / niceMax) * chartHeight;
return { x, y, val, label: labels[lIndex] };
});
coordinates.forEach((c) => {
const handleMouseEnter = (e: any) => {
Tooltip.show(
e,
{c.label}
{s.name}: {formatY(c.val)}
,
{ position: 'auto' }
);
e.target.setAttribute('r', '8');
};
const handleMouseLeave = (e: any) => {
Tooltip.hide();
e.target.setAttribute('r', '5');
};
points.push(
);
});
});
return points;
};
return (
);
};
const chartVar = new HtmlVar(renderChart(1000, 300));
const ref: RefProps = {
referToCssId: globalCssId,
onLoad: async (el: Element) => {
const ro = new ResizeObserver((entries) => {
const { width, height } = entries[0].contentRect;
if (width > 50 && height > 50) {
chartVar.value = renderChart(width, height);
}
});
ro.observe(el);
(el as any)._ro = ro;
},
onUnload: async (el: Element) => {
if ((el as any)._ro) {
(el as any)._ro.disconnect();
}
},
};
const ratio = props.aspectRatio ?? 16 / 9;
const paddingTop = `${(1 / ratio) * 100}%`;
const styleStr = `width: ${props.width || '100%'};`;
return (
{props.title &&
{props.title}
}
{chartVar.node}
{showLegend && (
{series.map((s, i) => (
))}
)}
);
};