decision_boundary-dev / frontends /react /src /DecisionBoundaryPlot.tsx
nhamid289's picture
Deploy decision boundary: new layout, dataset picker, model settings and results
f586f77 verified
Raw History Blame Contribute Delete
3.77 kB
import { useRef, useImperativeHandle, type RefObject } from 'react';
import { usePlotlyClick } from "@elvis/ui";
import Plot from 'react-plotly.js';
import Plotly from "plotly.js-dist-min";
import type { Data, ColorScale } from 'plotly.js';
import type { DataPoints, DecisionBoundaryResult } from './types.ts';
import { LabelColors } from './colors.ts';
interface DecisionBoundaryPlotProps {
dataPoints: DataPoints,
// Called with data coordinates when the plot is clicked; null ignores clicks
onDataClick: ((x: number, y: number) => void) | null;
decisionBoundaryResult: DecisionBoundaryResult | null;
xRangeRef: RefObject<[number, number]>;
yRangeRef: RefObject<[number, number]>;
setRanges: (xRange: [number, number], yRange: [number, number]) => void;
labelColors: LabelColors;
plotHandle: any;
}
export default function DecisionBoundaryPlot({
dataPoints,
onDataClick,
decisionBoundaryResult,
xRangeRef,
yRangeRef,
setRanges,
labelColors,
plotHandle,
}: DecisionBoundaryPlotProps) {
const plotRef = useRef<Plot | null>(null);
// todo layout ref type
const layoutRef = useRef<any>({
dragmode: "pan",
xaxis: {
range: xRangeRef.current,
},
yaxis: {
range: yRangeRef.current,
},
margin: { t: 40, r: 40, b: 40, l: 40 },
});
layoutRef.current.xaxis.range = xRangeRef.current;
layoutRef.current.yaxis.range = yRangeRef.current;
useImperativeHandle(plotHandle, () => ({
async downloadImage(format: 'svg' | 'png') {
if (!plotRef.current) return;
Plotly.downloadImage((plotRef.current as any)?.el, {
format,
width: 800,
height: 600,
filename: 'plot'
});
}
}));
// el property does not exist until runtime
usePlotlyClick(() => (plotRef.current as any)?.el, onDataClick);
const scatterTrace: Data = {
x: dataPoints.xPoints,
y: dataPoints.yPoints,
text: dataPoints.labels,
mode: 'markers' as const,
marker: { size: 10, color: dataPoints.labels.map((label: string) => labelColors.getColor(label)) },
type: 'scatter' as const,
hovertemplate: '%{x:.2f}, %{y:.2f}, %{text}<extra></extra>',
}
const regionTrace: Data | null = decisionBoundaryResult ? {
x: decisionBoundaryResult.x,
y: decisionBoundaryResult.y,
z: decisionBoundaryResult.labels.map(row =>
row.map((label: string) => labelColors.labelToIndex(label))
),
type: "heatmap" as const,
showscale: false,
zmin: 0,
zmax: labelColors.getLength() - 1,
colorscale: labelColors.getColorScale() as ColorScale,
hoverinfo: "skip" as const,
opacity: 0.3,
} : null;
return (
<div className="flex-1 min-h-[320px]">
<Plot
ref={plotRef}
data={regionTrace ? [regionTrace, scatterTrace] : [scatterTrace]}
layout={layoutRef.current}
onRelayout={(event) => {
// detect autoscale
const gd = (plotRef.current as any)?.el;
const fx = gd?._fullLayout?.xaxis?.range as [number, number] | undefined;
const fy = gd?._fullLayout?.yaxis?.range as [number, number] | undefined;
// detect zoom & pan
const x0 = event["xaxis.range[0]"] ?? fx?.[0] ?? xRangeRef.current[0];
const x1 = event["xaxis.range[1]"] ?? fx?.[1] ?? xRangeRef.current[1];
const y0 = event["yaxis.range[0]"] ?? fy?.[0] ?? yRangeRef.current[0];
const y1 = event["yaxis.range[1]"] ?? fy?.[1] ?? yRangeRef.current[1];
setRanges([x0, x1], [y0, y1]);
}}
config={{
scrollZoom: true,
displayModeBar: true,
doubleClick: false,
responsive: true,
}}
style={{ width: '100%', height: '100%', minWidth: 0 }}
/>
</div>
)
}