Spaces:
Running
Running
File size: 4,319 Bytes
a375433 779968d a375433 779968d a375433 779968d a375433 779968d a375433 779968d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import { useRef } from "react";
import Plot from "react-plotly.js";
import type { PlotData } from "./types";
interface OptimizationPlotProps {
data: PlotData;
xlim: [number, number];
ylim: [number, number];
setAxisLimits: (xlim: [number, number], ylim: [number, number]) => void;
}
export default function OptimizationPlot({ data, xlim, ylim, setAxisLimits }: OptimizationPlotProps) {
// data
let x: number[] = data.functionValues ? data.functionValues.x : [];
let y: number[] = data.functionValues ? data.functionValues.y : [];
let z: number[] = data.functionValues && data.functionValues.z ? data.functionValues.z : [];
let trajX: number[] = data.trajectoryValues ? data.trajectoryValues.x : [];
let trajY: number[] = data.trajectoryValues ? data.trajectoryValues.y : [];
let trajZ: number[] = data.trajectoryValues && data.trajectoryValues.z ? data.trajectoryValues.z : [];
const plotRef = useRef<any>(null);
const layoutRef = useRef<any>({
dragmode: 'pan',
showlegend: false,
xaxis: {
title: { text: 'x' },
range: xlim,
},
yaxis: {
title: { text: 'y' },
range: ylim,
}
})
if (z.length === 0) {
return (
<Plot
ref={plotRef}
onRelayout={(event) => {
const x0 = event['xaxis.range[0]'];
const x1 = event['xaxis.range[1]'];
const y0 = event['yaxis.range[0]'];
const y1 = event['yaxis.range[1]'];
if (
typeof x0 === "number"
&& typeof x1 === "number"
&& typeof y0 === "number"
&& typeof y1 === "number"
) {
setAxisLimits([x0, x1], [y0, y1]);
}
}}
data={[
{
x: x,
y: y,
type: 'scatter',
mode: 'lines',
line: { color: '#1f77b4', width: 2 },
hoverinfo: "skip",
},
{
x: trajX,
y: trajY,
type: 'scatter',
mode: 'lines+markers',
line: { color: '#d97871', width: 2 },
marker: { color: '#d97871', size: 10 },
hoverinfo: "skip",
},
{
x: trajX.length > 0 ? [trajX.at(-1)!] : [],
y: trajY.length > 0 ? [trajY.at(-1)!] : [],
type: 'scatter',
mode: 'markers',
marker: { color: 'red', size: 12 },
hoverinfo: "skip",
}
]}
layout={layoutRef.current}
className="w-full h-full"
config={{
responsive: true,
displayModeBar: false,
scrollZoom: true,
}}
/>
);
} else {
return (
<Plot
ref={plotRef}
onRelayout={(event) => {
const x0 = event['xaxis.range[0]'];
const x1 = event['xaxis.range[1]'];
const y0 = event['yaxis.range[0]'];
const y1 = event['yaxis.range[1]'];
if (
typeof x0 === "number"
&& typeof x1 === "number"
&& typeof y0 === "number"
&& typeof y1 === "number"
) {
setAxisLimits([x0, x1], [y0, y1]);
}
}}
data={[
{
x: x,
y: y,
z: z,
type: 'contour',
colorscale: 'Viridis',
hoverinfo: "skip",
contours: {
coloring: "heatmap",
showlines: false,
}
},
{
x: trajX,
y: trajY,
z: trajZ,
type: 'scatter',
mode: 'lines+markers',
line: { color: '#d97871', width: 2 },
marker: { color: '#d97871', size: 10 },
hoverinfo: "skip",
},
{
x: trajX.length > 0 ? [trajX.at(-1)!] : [],
y: trajY.length > 0 ? [trajY.at(-1)!] : [],
z: trajZ.length > 0 ? [trajZ.at(-1)!] : [],
type: 'scatter',
mode: 'markers',
marker: { color: 'red', size: 12 },
hoverinfo: "skip",
}
]}
layout={layoutRef.current}
className="w-full h-full"
config={{
responsive: true,
displayModeBar: false,
scrollZoom: true,
}}
/>
);
}
} |