Spaces:
Running
Running
Download frontends/react/src/DecisionBoundaryPlot.tsx from nhamid289/decision_boundary-dev: direct link, hf CLI and curl.
- Browser
- Download file 3.77 kB
-
https://huggingface.co/spaces/nhamid289/decision_boundary-dev/resolve/main/frontends/react/src/DecisionBoundaryPlot.tsx
- Command line
-
hf download hf://spaces/nhamid289/decision_boundary-dev/frontends/react/src/DecisionBoundaryPlot.tsx
-
curl -L -o DecisionBoundaryPlot.tsx https://huggingface.co/spaces/nhamid289/decision_boundary-dev/resolve/main/frontends/react/src/DecisionBoundaryPlot.tsx
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> | |
| ) | |
| } | |