Spaces:
Running
Running
Yao-Ting Yao
commited on
Commit
·
ae38fe2
1
Parent(s):
61b9eb6
Add hover infos into plot
Browse files- src/streamlit_app.py +62 -36
src/streamlit_app.py
CHANGED
@@ -254,8 +254,12 @@ points = (
|
|
254 |
"cls_dim1": "x",
|
255 |
"cls_dim2": "y",
|
256 |
"Land Cover": "category"
|
257 |
-
})[["x","y","category","thumbs","dates_list"]]
|
258 |
-
.assign(
|
|
|
|
|
|
|
|
|
259 |
.to_dict(orient="records")
|
260 |
)
|
261 |
|
@@ -311,55 +315,77 @@ plot_html = f"""
|
|
311 |
const traces = cats.map(cat => {{
|
312 |
const pts = points.filter(p=>p.category===cat);
|
313 |
return {{
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
321 |
}};
|
322 |
}});
|
323 |
|
324 |
// plotly layout
|
325 |
const layout = {{
|
326 |
-
|
327 |
-
plot_bgcolor: "rgb(234, 234, 242)",
|
328 |
title: {title_js},
|
329 |
-
xaxis: {{
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
349 |
autosize: true,
|
350 |
margin: {{ l:40, r:40, t:40, b:40 }},
|
351 |
clickmode:'event+select',
|
352 |
-
legend:
|
353 |
-
}};
|
354 |
|
355 |
// select the scatter-plot div to render the chart into
|
356 |
const gd = document.getElementById('scatter-plot');
|
357 |
-
|
358 |
Plotly.newPlot(gd, traces, layout, {{ responsive: true }}).then(() => {{
|
|
|
|
|
359 |
gd.on('plotly_click', evt => {{
|
360 |
// grab thumbs and dates through point index
|
361 |
-
const idx
|
362 |
-
const
|
|
|
363 |
const dates = points[idx].dates_list;
|
364 |
|
365 |
// grab image container and clear out old thumbs
|
|
|
254 |
"cls_dim1": "x",
|
255 |
"cls_dim2": "y",
|
256 |
"Land Cover": "category"
|
257 |
+
})[["x","y","chip_id", "latitude", "longitude","category","thumbs","dates_list"]]
|
258 |
+
.assign(
|
259 |
+
id = chips_df["chip_id"],
|
260 |
+
lat = chips_df["latitude"],
|
261 |
+
lon = chips_df["longitude"],
|
262 |
+
color=chips_df["Land Cover"].map(color_dict_label))
|
263 |
.to_dict(orient="records")
|
264 |
)
|
265 |
|
|
|
315 |
const traces = cats.map(cat => {{
|
316 |
const pts = points.filter(p=>p.category===cat);
|
317 |
return {{
|
318 |
+
x: pts.map(p=>p.x),
|
319 |
+
y: pts.map(p=>p.y),
|
320 |
+
id: pts.map(p=>p.id),
|
321 |
+
lat: pts.map(p=>p.lat),
|
322 |
+
lon: pts.map(p=>p.lon),
|
323 |
+
customdata:pts.map(p=>[
|
324 |
+
p.id,
|
325 |
+
p.lat,
|
326 |
+
p.lon,
|
327 |
+
p.thumbs
|
328 |
+
]),
|
329 |
+
mode: 'markers',
|
330 |
+
type: 'scatter',
|
331 |
+
name: cat,
|
332 |
+
marker: {{ color: pts.map(p=>p.color), size:5 }},
|
333 |
+
hovertemplate:
|
334 |
+
`<b>ID:</b> %{{customdata[0]}}<br>` +
|
335 |
+
`<b>x:</b> %{{x:.2f}}<br>` +
|
336 |
+
`<b>y:</b> %{{y:.2f}}<br>` +
|
337 |
+
`<b>lat:</b> %{{customdata[1]:.4f}}<br>` +
|
338 |
+
`<b>lon:</b> %{{customdata[2]:.4f}}<extra></extra>`
|
339 |
}};
|
340 |
}});
|
341 |
|
342 |
// plotly layout
|
343 |
const layout = {{
|
344 |
+
hovermode: 'closest',
|
|
|
345 |
title: {title_js},
|
346 |
+
xaxis: {{
|
347 |
+
title: {xaxis_js},
|
348 |
+
range: [-110, 110],
|
349 |
+
showgrid: true,
|
350 |
+
gridcolor: 'rgb(255,255,255)',
|
351 |
+
gridwidth: 1,
|
352 |
+
showline: false,
|
353 |
+
zeroline: false,
|
354 |
+
showticklabels: true,
|
355 |
+
ticks: 'outside',
|
356 |
+
tickcolor: 'rgb(127,127,127)'
|
357 |
+
}},
|
358 |
+
yaxis: {{
|
359 |
+
title: {yaxis_js},
|
360 |
+
range: [-110, 110],
|
361 |
+
showgrid: true,
|
362 |
+
gridcolor: 'rgb(255,255,255)',
|
363 |
+
gridwidth: 1,
|
364 |
+
showline: false,
|
365 |
+
zeroline: false,
|
366 |
+
showticklabels: true,
|
367 |
+
ticks: 'outside',
|
368 |
+
tickcolor: 'rgb(127,127,127)'
|
369 |
+
}},
|
370 |
+
paper_bgcolor: 'rgb(255,255,255)',
|
371 |
+
plot_bgcolor: 'rgb(234,234,242)',
|
372 |
autosize: true,
|
373 |
margin: {{ l:40, r:40, t:40, b:40 }},
|
374 |
clickmode:'event+select',
|
375 |
+
legend: {{ font:{{ size:12 }}, x:1.01, y:0.5 }}
|
376 |
+
}};
|
377 |
|
378 |
// select the scatter-plot div to render the chart into
|
379 |
const gd = document.getElementById('scatter-plot');
|
380 |
+
|
381 |
Plotly.newPlot(gd, traces, layout, {{ responsive: true }}).then(() => {{
|
382 |
+
|
383 |
+
//click handler
|
384 |
gd.on('plotly_click', evt => {{
|
385 |
// grab thumbs and dates through point index
|
386 |
+
const idx = evt.points[0].pointIndex;
|
387 |
+
const cds = evt.points[0].customdata;
|
388 |
+
const thumbs = cds[3];
|
389 |
const dates = points[idx].dates_list;
|
390 |
|
391 |
// grab image container and clear out old thumbs
|