chart_js_rs/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#![allow(non_snake_case)]
#![doc = include_str!("../README.md")]

pub mod bar;
pub mod doughnut;
pub mod exports;
pub mod functions;
pub mod objects;
pub mod pie;
pub mod scatter;
pub mod traits;

#[doc(hidden)]
mod utils;

use exports::get_chart;
use gloo_utils::format::JsValueSerdeExt;
pub use objects::*;
use serde::{de::DeserializeOwned, Serialize};
pub use traits::*;
pub use utils::*;

pub trait ChartExt<A: Annotation + DeserializeOwned>:
    DeserializeOwned + Serialize + Default
{
    type DS;

    fn new(id: impl AsRef<str>) -> Self {
        Self::default().id(id.as_ref().into())
    }

    fn get_id(self) -> String;
    fn id(self, id: String) -> Self;

    fn get_data(&mut self) -> &mut Self::DS;
    fn data(mut self, data: impl Into<Self::DS>) -> Self {
        *self.get_data() = data.into();
        self
    }

    fn get_options(&mut self) -> &mut ChartOptions<A>;
    fn options(mut self, options: impl Into<ChartOptions<A>>) -> Self {
        *self.get_options() = options.into();
        self
    }

    fn into_chart(self) -> Chart {
        Chart {
            obj: <::wasm_bindgen::JsValue as JsValueSerdeExt>::from_serde(&self)
                .expect("Unable to serialize chart."),
            id: self.get_id(),
            mutate: false,
            plugins: String::new(),
            defaults: String::new(),
        }
    }

    fn get_chart_from_id(id: &str) -> Option<Self> {
        let chart = get_chart(id);

        serde_wasm_bindgen::from_value(chart)
            .inspect_err(|e| {
                gloo_console::error!(e.to_string());
            })
            .ok()
    }
}