File size: 6,497 Bytes
ab2e84f
779968d
 
3adb9d3
779968d
 
 
4f4a1ca
779968d
 
 
 
 
4f4a1ca
779968d
 
 
 
ab2e84f
888bb2a
779968d
 
888bb2a
 
ab2e84f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
779968d
 
 
 
 
 
 
 
 
 
 
 
3f34e78
 
779968d
 
4f4a1ca
 
 
 
 
 
 
 
 
 
 
 
779968d
 
3f34e78
3adb9d3
 
 
 
 
 
 
a375433
3adb9d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
779968d
 
 
3f34e78
ab2e84f
 
 
 
 
 
 
a375433
ab2e84f
 
 
 
 
 
 
779968d
ab2e84f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3adb9d3
779968d
 
ab2e84f
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { useState, useRef, useEffect } from "react";
import Plot from "react-plotly.js";
import type { PlotData } from "./types";
import { Button, Card } from "@elvis/ui";

interface OptimizationPlotProps {
  data: PlotData;
  isLoading: boolean;
  xlim: [number, number];
  ylim: [number, number];
  setAxisLimits: (xlim: [number, number], ylim: [number, number]) => void;
}

export default function OptimizationPlot({ data, isLoading, 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 [colorScaleRange, setColorScaleRange] = useState<[number, number] | null>(null);
  const nextColorScaleRangeRef = useRef<[number, number] | null>(null);

  useEffect(() => {
    if (z) {
      if (!z || z.length === 0) {
        nextColorScaleRangeRef.current = null;
        return;
      }

      let zMin = Infinity;
      let zMax = -Infinity;

      for (let i = 0; i < z.length; i++) {
        for (let j = 0; j < z[i].length; j++) {
          const v = z[i][j];
          if (!Number.isFinite(v)) {
            continue;
          }
          if (v < zMin) {
            zMin = v;
          }
          if (v > zMax) {
            zMax = v;
          }
        }
      }

      const padding = (zMax - zMin) * 0.1;
      nextColorScaleRangeRef.current = [zMin - padding, zMax + padding];

      if (colorScaleRange === null) {
        setColorScaleRange(nextColorScaleRangeRef.current);
      }
    }
  }, [z]);

  function updateColorScaleRange() {
    if (nextColorScaleRangeRef.current) {
      setColorScaleRange(nextColorScaleRangeRef.current);
      nextColorScaleRangeRef.current = null;
    }
  }

  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,
    },
    margin: { t: 40, r: 40, b: 40, l: 40 }
  })

  const hasNoData = !data.functionValues && !data.trajectoryValues;

  if (isLoading && hasNoData) {
    return (
      <Card className="min-h-[320px] flex items-center justify-center p-6">
        <div className="text-center">
          <div className="text-lg font-medium">Loading...</div>
        </div>
      </Card>
    );
  }

  if (z.length === 0) {
    return (
      <Card className="min-h-[320px]">
        <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}
          style={{ width: '100%', height: '100%' }}
          config={{ 
            responsive: true,
            displayModeBar: true,
            scrollZoom: true,
          }}
        />
      </Card>
    );
  } else {
    return (
      <Card className="flex flex-col min-h-[420px]">
        <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,
              zmin: colorScaleRange?.[0],
              zmax: colorScaleRange?.[1],
              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 flex-1"
          config={{ 
            responsive: true,
            displayModeBar: false,
            scrollZoom: true,
          }}
        />
        <div className="mt-2 flex justify-end">
          <Button label="Update Color Scale" onClick={updateColorScaleRange} />
        </div>
      </Card>
    ); 
  }
}