|
| 1 | +import { |
| 2 | + CategoryScale, |
| 3 | + Chart as ChartJS, |
| 4 | + Legend, |
| 5 | + LineElement, |
| 6 | + LinearScale, |
| 7 | + PointElement, |
| 8 | + Tooltip, |
| 9 | +} from "chart.js" |
| 10 | +import { useEffect, useMemo, useState } from "react" |
| 11 | +import { Line } from "react-chartjs-2" |
| 12 | + |
| 13 | +const waypointLabelPlugin = { |
| 14 | + id: "waypointLabelPlugin", |
| 15 | + afterDatasetsDraw(chart) { |
| 16 | + const { ctx } = chart |
| 17 | + const datasetMeta = chart.getDatasetMeta(0) |
| 18 | + const dataset = chart.data?.datasets?.[0] |
| 19 | + |
| 20 | + if (!datasetMeta || !dataset?.data) return |
| 21 | + |
| 22 | + ctx.save() |
| 23 | + ctx.font = "11px sans-serif" |
| 24 | + ctx.fillStyle = "#e2e8f0" |
| 25 | + ctx.textAlign = "left" |
| 26 | + ctx.textBaseline = "bottom" |
| 27 | + |
| 28 | + datasetMeta.data.forEach((element, index) => { |
| 29 | + const point = dataset.data[index] |
| 30 | + if (!point) return |
| 31 | + |
| 32 | + const label = |
| 33 | + point.waypointLabel ?? |
| 34 | + (Number.isFinite(point.waypointSeq) |
| 35 | + ? `${point.waypointSeq}` |
| 36 | + : undefined) |
| 37 | + |
| 38 | + if (!label) return |
| 39 | + ctx.fillText(label, element.x + 6, element.y - 4) |
| 40 | + }) |
| 41 | + |
| 42 | + ctx.restore() |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +ChartJS.register( |
| 47 | + CategoryScale, |
| 48 | + LinearScale, |
| 49 | + PointElement, |
| 50 | + LineElement, |
| 51 | + Tooltip, |
| 52 | + Legend, |
| 53 | + waypointLabelPlugin, |
| 54 | +) |
| 55 | + |
| 56 | +const chartOptions = { |
| 57 | + responsive: true, |
| 58 | + maintainAspectRatio: false, |
| 59 | + animation: false, |
| 60 | + plugins: { |
| 61 | + legend: { |
| 62 | + display: true, |
| 63 | + position: "top", |
| 64 | + labels: { |
| 65 | + color: "#e2e8f0", |
| 66 | + }, |
| 67 | + }, |
| 68 | + tooltip: { |
| 69 | + callbacks: { |
| 70 | + title: () => "", |
| 71 | + label: (ctx) => { |
| 72 | + if (ctx.raw?.isHome) { |
| 73 | + return `Home: ${ctx.parsed.y.toFixed(2)} m` |
| 74 | + } |
| 75 | + const seq = ctx.raw?.waypointSeq |
| 76 | + return `WP ${seq}: ${ctx.parsed.y.toFixed(2)} m` |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + scales: { |
| 82 | + x: { |
| 83 | + type: "linear", |
| 84 | + title: { |
| 85 | + display: true, |
| 86 | + text: "Distance (m)", |
| 87 | + color: "#e2e8f0", |
| 88 | + }, |
| 89 | + ticks: { |
| 90 | + color: "#e2e8f0", |
| 91 | + }, |
| 92 | + grid: { |
| 93 | + color: "rgba(148, 163, 184, 0.15)", |
| 94 | + }, |
| 95 | + }, |
| 96 | + y: { |
| 97 | + title: { |
| 98 | + display: true, |
| 99 | + text: "Altitude (m)", |
| 100 | + color: "#e2e8f0", |
| 101 | + }, |
| 102 | + ticks: { |
| 103 | + color: "#e2e8f0", |
| 104 | + }, |
| 105 | + grid: { |
| 106 | + color: "rgba(148, 163, 184, 0.15)", |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + elements: { |
| 111 | + line: { |
| 112 | + borderWidth: 2, |
| 113 | + tension: 0, |
| 114 | + }, |
| 115 | + point: { |
| 116 | + radius: 4, |
| 117 | + hitRadius: 10, |
| 118 | + }, |
| 119 | + }, |
| 120 | +} |
| 121 | + |
| 122 | +export default function ElevationGraphWindow() { |
| 123 | + const [profile, setProfile] = useState({ |
| 124 | + points: [], |
| 125 | + totalDistance: 0, |
| 126 | + warnings: [], |
| 127 | + }) |
| 128 | + |
| 129 | + useEffect(() => { |
| 130 | + const handleUpdate = (_event, payload) => { |
| 131 | + setProfile( |
| 132 | + payload || { |
| 133 | + points: [], |
| 134 | + totalDistance: 0, |
| 135 | + warnings: [], |
| 136 | + }, |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + window.ipcRenderer.on("app:send-elevation-graph", handleUpdate) |
| 141 | + window.ipcRenderer.send("app:elevation-graph:ready") |
| 142 | + |
| 143 | + return () => { |
| 144 | + window.ipcRenderer.removeListener( |
| 145 | + "app:send-elevation-graph", |
| 146 | + handleUpdate, |
| 147 | + ) |
| 148 | + } |
| 149 | + }, []) |
| 150 | + |
| 151 | + const chartData = useMemo(() => { |
| 152 | + const data = profile.points.map((point) => ({ |
| 153 | + x: point.cumulativeDistance, |
| 154 | + y: point.altitude, |
| 155 | + waypointSeq: point.seq, |
| 156 | + waypointLabel: point.label, |
| 157 | + isHome: Boolean(point.isHome), |
| 158 | + })) |
| 159 | + |
| 160 | + return { |
| 161 | + datasets: [ |
| 162 | + { |
| 163 | + label: "Mission Elevation", |
| 164 | + data, |
| 165 | + borderColor: "#facc15", |
| 166 | + backgroundColor: "rgba(250, 204, 21, 0.35)", |
| 167 | + showLine: true, |
| 168 | + }, |
| 169 | + ], |
| 170 | + } |
| 171 | + }, [profile]) |
| 172 | + |
| 173 | + return ( |
| 174 | + <div className="w-full h-full bg-falcongrey-800 text-slate-100 p-4 flex flex-col gap-3"> |
| 175 | + <div className="flex flex-row justify-between items-center text-sm"> |
| 176 | + <p className="font-semibold">Mission elevation profile</p> |
| 177 | + <p className="text-slate-300"> |
| 178 | + Total distance: {Number(profile.totalDistance || 0).toFixed(2)} m |
| 179 | + </p> |
| 180 | + </div> |
| 181 | + |
| 182 | + {profile.warnings?.length > 0 && ( |
| 183 | + <div className="bg-yellow-950/40 border border-yellow-700 rounded px-3 py-2 text-yellow-200 text-xs"> |
| 184 | + {profile.warnings.map((warning, idx) => ( |
| 185 | + <p key={`${warning}-${idx}`}>{warning}</p> |
| 186 | + ))} |
| 187 | + </div> |
| 188 | + )} |
| 189 | + |
| 190 | + {profile.points.length === 0 ? ( |
| 191 | + <div className="h-full flex items-center justify-center text-slate-400 text-sm border border-falcongrey-600 rounded"> |
| 192 | + No NAV waypoints with altitude available for elevation graph. |
| 193 | + </div> |
| 194 | + ) : ( |
| 195 | + <div className="flex-1 min-h-0"> |
| 196 | + <Line options={chartOptions} data={chartData} /> |
| 197 | + </div> |
| 198 | + )} |
| 199 | + </div> |
| 200 | + ) |
| 201 | +} |
0 commit comments