Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal support for Events and Actions #115

Open
LukaOber opened this issue Nov 4, 2024 · 2 comments
Open

Minimal support for Events and Actions #115

LukaOber opened this issue Nov 4, 2024 · 2 comments

Comments

@LukaOber
Copy link
Collaborator

LukaOber commented Nov 4, 2024

For a project, I need to get the data index of a selected point in leptos. Echarts supports this Echarts Event and Action, but I am not familiar enough with wasm to implement this myself.

Using wasm_bindgen it should be possible to allow this functionality.

If someone could provide me with a minimal working PR, I would be able to extend the functionality.

chart.on('click', function(params) {
  console.log(params.name);
});

Code that would generate something like this, just hardcoded in the wasm_renderer.rs file, would be great.

Later on, we would need to figure out how we can support both html and wasm.

@yuankunzhang would you be able to tackle this?

@LukaOber
Copy link
Collaborator Author

LukaOber commented Nov 4, 2024

Nevermind, figured it out by "stealing" some ideas from #67. I will test it out a bit and open up a minimal PR soon-ish.

@Almaju
Copy link

Almaju commented Nov 7, 2024

A hacky way to do it while waiting for the PR:

// The function to call on click
let closure = Closure::wrap(Box::new(|_| {
    info!("clicked");
}) as Box<dyn FnMut(JsValue)>);
let js_function: &js_sys::Function = closure.as_ref().unchecked_ref();

// The chart
let chart = renderer.render("chart", ...).unwrap();
let js_value: JsValue = chart.into();

// The `on` method
let on = js_sys::Reflect::get(&js_value, &"on".into())
    .expect("Object should have 'on' method")
    .dyn_into::<js_sys::Function>()
    .expect("'on' should be a function");

// The call
on.call2(&js_value, &"click".into(), js_function)
    .expect("Failed to call 'on' method");

Would be nice to have access to the on method on the bindings though. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants