Yao-Ting Yao commited on
Commit
ae38fe2
·
1 Parent(s): 61b9eb6

Add hover infos into plot

Browse files
Files changed (1) hide show
  1. 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(color=chips_df["Land Cover"].map(color_dict_label))
 
 
 
 
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
- x: pts.map(p=>p.x),
315
- y: pts.map(p=>p.y),
316
- customdata:pts.map(p=>p.thumbs),
317
- mode: 'markers',
318
- type: 'scatter',
319
- name: cat,
320
- marker: {{ color: pts.map(p=>p.color), size:5 }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
  }};
322
  }});
323
 
324
  // plotly layout
325
  const layout = {{
326
- paper_bgcolor: "rgb(255,255,255)",
327
- plot_bgcolor: "rgb(234, 234, 242)",
328
  title: {title_js},
329
- xaxis: {{ title: {xaxis_js},
330
- range:[-110,110],
331
- gridcolor: "rgb(255,255,255)",
332
- showgrid: true,
333
- showline: false,
334
- showticklabels: true,
335
- tickcolor: "rgb(127,127,127)",
336
- ticks: "outside",
337
- zeroline: false,
338
- gridwidth: 1}},
339
- yaxis: {{ title: {yaxis_js},
340
- range:[-110,110],
341
- gridcolor: "rgb(255,255,255)",
342
- showgrid: true,
343
- showline: false,
344
- showticklabels: true,
345
- tickcolor: "rgb(127,127,127)",
346
- ticks: "outside",
347
- zeroline: false,
348
- gridwidth: 1}},
 
 
 
 
 
 
349
  autosize: true,
350
  margin: {{ l:40, r:40, t:40, b:40 }},
351
  clickmode:'event+select',
352
- legend: {{ font:{{ size:12 }}, x:1.01, y:0.5 }}
353
- }};
354
 
355
  // select the scatter-plot div to render the chart into
356
  const gd = document.getElementById('scatter-plot');
357
- // click event
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 = evt.points[0].pointIndex;
362
- const thumbs = points[idx].thumbs;
 
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