diff --git a/Cargo.toml b/Cargo.toml index c03e235..f6220c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,8 @@ license-file = "LICENSE" keywords = ["apexcharts", "wasm", "rust", "web"] readme = "README.md" +[lib] +crate-type = ["cdylib"] [dependencies] wasm-bindgen = { version = "0.2", features = ["serde-serialize"] } diff --git a/src/bindings/mod.rs b/src/bindings/mod.rs index 315b108..7baa0a2 100644 --- a/src/bindings/mod.rs +++ b/src/bindings/mod.rs @@ -10,11 +10,26 @@ extern "C" { /// ## Usage /// /// ```rust - /// use apexcharts_rs::{ApexChart, ChartOptions}; - /// - /// let options = ChartOptions::from_file("path/to/options.json"); + /// use apexcharts_rs::prelude::{ApexChart, ChartOptions}; + /// + /// let options_str = r#" + /// { + /// "chart": { + /// "type": "line" + /// }, + /// "series": [ + /// { + /// "name": "Series 1", + /// "data": [30, 40, 35, 50, 49, 125] + /// } + /// ] + /// }"#; + /// let filename = "options.json"; + /// std::fs::write(filename, options_str).unwrap(); + /// let options = ChartOptions::from_file(filename); /// let chart = ApexChart::new(&options.into()); - /// chart.render("chart-id"); + /// // chart.render("chart-id"); + /// std::fs::remove_file(filename).unwrap(); /// ``` pub type ApexChart; diff --git a/src/options.rs b/src/options.rs index d3294c6..438a83f 100644 --- a/src/options.rs +++ b/src/options.rs @@ -9,9 +9,6 @@ use wasm_bindgen_futures::js_sys; /// /// ## Usage /// ```rust -/// let options = apexcharts_rs::ChartOptions::from_file("path/to/options.json"); -/// -/// // or /// /// let options_str = r#" /// { @@ -25,7 +22,7 @@ use wasm_bindgen_futures::js_sys; /// } /// ] /// }"#; -/// let options = apexcharts_rs::ChartOptions::from_string(String::from(options_str)); +/// let options = apexcharts_rs::prelude::ChartOptions::from_string(String::from(options_str)); /// ``` #[derive(Clone, Debug)] pub struct ChartOptions { @@ -47,8 +44,8 @@ impl ChartOptions { /// } /// ] /// }"#; - /// let options = apexcharts_rs::ChartOptions::from_string(options_str); - /// let chart = apexcharts_rs::ApexChart::new(&options.into()); + /// let options = apexcharts_rs::prelude::ChartOptions::from_string(String::from(options_str)); + /// let chart = apexcharts_rs::prelude::ApexChart::new(&options.into()); /// ``` pub fn from_string(options: String) -> Self { Self { @@ -57,8 +54,23 @@ impl ChartOptions { } /// Create a new instance of ChartOptions from a json file. /// ```rust - /// let options = apexcharts_rs::ChartOptions::from_file("path/to/options.json"); - /// let chart = apexcharts_rs::ApexChart::new(&options.into()); + /// let options_str = r#" + /// { + /// "chart": { + /// "type": "line" + /// }, + /// "series": [ + /// { + /// "name": "Series 1", + /// "data": [30, 40, 35, 50, 49, 125] + /// } + /// ] + /// }"#; + /// let filename = "options.json"; + /// std::fs::write(filename, options_str).unwrap(); + /// let options = apexcharts_rs::prelude::ChartOptions::from_file(filename); + /// let chart = apexcharts_rs::prelude::ApexChart::new(&options.into()); + /// std::fs::remove_file(filename).unwrap(); /// ``` pub fn from_file(file_path: &str) -> Self { // Read the file and return the content as a string @@ -81,7 +93,7 @@ impl ChartOptions { impl From for JsValue { fn from(options: ChartOptions) -> JsValue { - JsValue::from_str(&options.options) + JsValue::from_str(options.options.as_str()) } } @@ -159,6 +171,9 @@ pub enum SeriesData { /// /// ## Usage /// ```rust +/// use apexcharts_rs::prelude::{ChartSeries, SeriesData, ChartOptions, ApexChart}; +/// use wasm_bindgen::prelude::*; +/// /// let series = vec![ChartSeries { /// name: "Series 1".to_string(), /// data: SeriesData::Single(vec![10.0, 20.0, 30.0]),