Skip to content

Commit 3b66a48

Browse files
committed
remove unwrap calls
- update changelog Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com>
1 parent 418d0a3 commit 3b66a48

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ jobs:
115115
strategy:
116116
fail-fast: false
117117
matrix:
118-
example: [wasm-yew-minimal]
118+
example: [wasm-yew-minimal, wasm-yew-callback-minimal]
119119
runs-on: ubuntu-latest
120120
steps:
121121
- uses: actions/checkout@v4

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
77
### Changed
88
- [[#277](https://github.com/plotly/plotly.rs/pull/277)] Removed `wasm` feature flag and put evrything behind target specific dependencies. Added `.cargo/config.toml` for configuration flags needed by `getrandom` version 0.3 on `wasm` targets.
99
- [[#281]((https://github.com/plotly/plotly.rs/pull/xxx))] Update to askama 0.13.0
10+
- [[#287]](https://github.com/plotly/plotly.rs/pull/287) Added functionality for callbacks (using wasm)
1011
- [[#289]](https://github.com/plotly/plotly.rs/pull/289) Fixes Kaleido static export for MacOS targets by removing `--disable-gpu` flag for MacOS
1112
- [[#290]](https://github.com/plotly/plotly.rs/pull/289) Remove `--disable-gpu` flag for Kaleido static-image generation for all targets.
1213

examples/wasm-yew-callback-minimal/src/main.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use plotly::callbacks::ClickEvent;
2-
use plotly::{Histogram, Plot, Scatter, common::Mode};
2+
use plotly::{Histogram, Plot, Scatter, common::Mode, histogram::Bins};
33
use web_sys::js_sys::Math;
44
use yew::prelude::*;
55

@@ -23,14 +23,18 @@ pub fn plot_component() -> Html {
2323
let id = "plot-div";
2424
let mut fig = Plot::new();
2525
let xs: Vec<f64> = (0..50).map(|i| i as f64).collect();
26-
let ys: Vec<f64> = xs.iter().map(|x| x.sin()).collect();
26+
let ys: Vec<f64> = xs.iter().map(|x| x.sin() * 5.0).collect();
2727
fig.add_trace(
2828
Scatter::new(xs.clone(), ys.clone())
2929
.mode(Mode::Markers)
30-
.name("Sine markers"),
30+
.name("Sine Wave Markers"),
31+
);
32+
let random_values: Vec<f64> = (0..500).map(|_| Math::random() * 100.0).collect();
33+
fig.add_trace(
34+
Histogram::new(random_values)
35+
.name("Random Data Histogram")
36+
.x_bins(Bins::new(-1.0, 30.0, 5.0)),
3137
);
32-
let random_values: Vec<f64> = (0..100).map(|_| Math::random()).collect();
33-
fig.add_trace(Histogram::new(random_values).name("Random histogram"));
3438
let layout = plotly::Layout::new().title("Click Event Callback Example in Yew");
3539
fig.set_layout(layout);
3640
async move {

plotly/src/callbacks.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22
use wasm_bindgen::prelude::*;
3-
use web_sys::{js_sys::Function, window, HtmlElement};
3+
use web_sys::{js_sys::Function, HtmlElement};
44

55
/// Provides utilities for binding Plotly.js click events to Rust closures
66
/// via `wasm-bindgen`.
@@ -61,22 +61,25 @@ pub fn bind_click<F>(div_id: &str, mut cb: F)
6161
where
6262
F: 'static + FnMut(ClickEvent),
6363
{
64-
let plot_div: PlotlyDiv = window()
65-
.unwrap()
66-
.document()
67-
.unwrap()
68-
.get_element_by_id(div_id)
69-
.unwrap()
70-
.unchecked_into();
7164
let closure = Closure::wrap(Box::new(move |event: JsValue| {
7265
let event: ClickEvent =
73-
serde_wasm_bindgen::from_value(event).expect("\n Couldn't serialize the event \n");
66+
serde_wasm_bindgen::from_value(event).expect("Could not serialize the event");
7467
cb(event);
7568
}) as Box<dyn FnMut(JsValue)>);
69+
70+
let plot_div: PlotlyDiv = get_div(div_id).expect("Could not get Div element by Id");
7671
plot_div.on("plotly_click", closure.as_ref().unchecked_ref());
7772
closure.forget();
7873
}
7974

75+
fn get_div(tag: &str) -> Option<PlotlyDiv> {
76+
web_sys::window()?
77+
.document()?
78+
.get_element_by_id(tag)?
79+
.dyn_into()
80+
.ok()
81+
}
82+
8083
/// Represents a single point from a Plotly click event.
8184
///
8285
/// Fields mirror Plotly’s `event.points[i]` properties, all optional

0 commit comments

Comments
 (0)