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

Resizing echarts with wasm renderer #28

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 90 additions & 3 deletions charming/src/renderer/wasm_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl WasmRenderer {
self
}

pub fn render(&self, id: &str, chart: &Chart) -> Result<(), EchartsError> {
pub fn render(&self, id: &str, chart: &Chart) -> Result<Echarts, EchartsError> {
let window = web_sys::window().ok_or(EchartsError::WasmError(
"no `window` object found".to_string(),
))?;
Expand All @@ -47,7 +47,14 @@ impl WasmRenderer {
.unwrap(),
);
echarts.set_option(to_value(chart).unwrap());
Ok(())

Ok(echarts)
}

/// Resizes a chart with options specified in [`ChartResize`]
pub fn resize_chart(echarts: &Echarts, chart_size: ChartResize) {
echarts
.resize(to_value(&chart_size).expect("could not convert resize options to `JsValue`"));
}
}

Expand All @@ -57,14 +64,94 @@ struct ChartSize {
height: u32,
}

#[derive(Serialize)]
pub struct ChartResize {
/// New width in px
width: u32,
/// New height in px
height: u32,
/// If true, emits events on resize
silent: bool,
/// Resize animation options
animation: Option<Animation>,
}

#[derive(Serialize)]
pub struct Animation {
/// duration of the animation
pub duration: u32,
/// easing function used for the animation
#[serde(skip_serializing_if = "Option::is_none")]
pub easing: Option<Easing>,
}

/// available easing functions in echarts
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Easing {
#[default]
Linear,
QuadraticIn,
QuadraticOut,
QuadraticInOut,
CubicIn,
CubicOut,
CubicInOut,
QuarticIn,
QuarticOut,
QuarticInOut,
QuinticIn,
QuinticOut,
QuinticInOut,
SinusoidalIn,
SinusoidalOut,
SinusoidalInOut,
ExponentialIn,
ExponentialOut,
ExponentialInOut,
CircularIn,
CircularOut,
CircularInOut,
ElasticIn,
ElasticOut,
ElasticInOut,
BackIn,
BackOut,
BackInOut,
BounceIn,
BounceOut,
BounceInOut,
}

impl Default for Animation {
fn default() -> Self {
Self {
duration: 100,
easing: Some(Easing::default()),
}
}
}

impl Animation {
pub fn new(duration: u32, easing: Option<Easing>) -> Self {
Self {
duration,
easing: easing.or_else(|| Some(Easing::default())),
}
}
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = echarts)]
type Echarts;
pub type Echarts;

#[wasm_bindgen(js_namespace = echarts, js_name = init)]
fn init(id: &web_sys::Element, theme: &str, size: JsValue) -> Echarts;

#[wasm_bindgen(method, js_name = "setOption")]
fn set_option(this: &Echarts, option: JsValue);

#[wasm_bindgen(method, js_name = "resize")]
pub fn resize(this: &Echarts, opts: JsValue);
}