From 1bc622043602c09fede586b1566633293b1899dd Mon Sep 17 00:00:00 2001 From: Billy Sheppard Date: Thu, 26 Sep 2024 00:39:53 +0000 Subject: [PATCH] moar building --- Cargo.lock | 4 +- Cargo.toml | 2 +- examples/src/lib.rs | 132 ++++++++++++++++++++------------------------ src/bar.rs | 24 ++++++-- src/doughnut.rs | 24 ++++++-- src/lib.rs | 26 ++++++++- src/pie.rs | 24 ++++++-- src/scatter.rs | 24 ++++++-- 8 files changed, 164 insertions(+), 96 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a042b4..70765b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,7 +124,7 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" [[package]] name = "chart-js-rs" -version = "0.0.38" +version = "0.0.39" dependencies = [ "gloo-console", "gloo-utils", @@ -137,7 +137,7 @@ dependencies = [ "serde", "serde-wasm-bindgen", "serde_json", - "syn 2.0.77", + "syn 1.0.109", "thiserror", "wasm-bindgen", ] diff --git a/Cargo.toml b/Cargo.toml index c21108f..f32ee3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chart-js-rs" -version = "0.0.38" +version = "0.0.39" edition = "2021" authors = ["Billy Sheppard", "Luis Moreno"] license = "Apache-2.0" diff --git a/examples/src/lib.rs b/examples/src/lib.rs index c05ff3a..bdb44cd 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -91,26 +91,25 @@ impl Model { // construct and render chart here let id = "scatter"; - let chart = Scatter:: { + let chart = Scatter::::new(id) // we use here to type hint for the compiler - data: Dataset::new().datasets([ - XYDataset::new() - .data(x.iter().zip(y1).into_data_iter().unsorted_to_dataset_data()) // collect into dataset - .border_color("red") - .background_color("lightcoral") - .point_radius(4) - .label("Dataset 1"), - XYDataset::new() - .data(x.iter().zip(y2).into_data_iter().unsorted_to_dataset_data()) // collect into dataset - .border_color("blue") - .background_color("lightskyblue") - .point_radius(4) - .label("Dataset 2"), - ]), - options: ChartOptions::new().maintain_aspect_ratio(false), - id: id.into(), - ..Default::default() - }; + .data( + Dataset::new().datasets([ + XYDataset::new() + .data(x.iter().zip(y1).into_data_iter().unsorted_to_dataset_data()) // collect into dataset + .border_color("red") + .background_color("lightcoral") + .point_radius(4) + .label("Dataset 1"), + XYDataset::new() + .data(x.iter().zip(y2).into_data_iter().unsorted_to_dataset_data()) // collect into dataset + .border_color("blue") + .background_color("lightskyblue") + .point_radius(4) + .label("Dataset 2"), + ]), + ) + .options(ChartOptions::new().maintain_aspect_ratio(false)); html!("canvas", { // construct a html canvas element, and after its rendered into the DOM we can insert our chart .prop("id", id) .style("height", "calc(100vh - 270px)") @@ -125,9 +124,9 @@ impl Model { let id = "line"; let chart = - Scatter:: { + Scatter::::new(id) // we use here to type hint for the compiler - data: Dataset::new().datasets([ + .data(Dataset::new().datasets([ XYDataset::new() .data( x.iter() @@ -167,8 +166,8 @@ impl Model { .point_radius(4) .label("Dataset 2") .r_type("line"), - ]), - options: ChartOptions::new() + ])) + .options(ChartOptions::new() .scales([( "x", ChartScale::new() @@ -179,10 +178,8 @@ impl Model { ), )), )]) - .maintain_aspect_ratio(false), - id: id.into(), - ..Default::default() - }; + .maintain_aspect_ratio(false) + ); html!("canvas", { // construct a html canvas element, and after its rendered into the DOM we can insert our chart .prop("id", id) .style("height", "calc(100vh - 270px)") @@ -196,30 +193,29 @@ impl Model { // construct and render chart here let id = "bar"; - let chart = Bar:: { + let chart = Bar::::new(id) // we use here to type hint for the compiler - data: Dataset::new() - .labels( - // use a range to give us our X axis labels - (0..data.len()).map(|d| d + 1), - ) - .datasets([XYDataset::new() - .data( - data.iter() - .enumerate() - .map(|(x, y)| ((x + 1), y)) - .into_data_iter() - .unsorted_to_dataset_data(), // collect into dataset + .data( + Dataset::new() + .labels( + // use a range to give us our X axis labels + (0..data.len()).map(|d| d + 1), ) - .background_color("palegreen") - .border_color("green") - .border_width(2) - .label("Dataset 1") - .y_axis_id("y")]), - options: ChartOptions::new().maintain_aspect_ratio(false), - id: id.into(), - ..Default::default() - }; + .datasets([XYDataset::new() + .data( + data.iter() + .enumerate() + .map(|(x, y)| ((x + 1), y)) + .into_data_iter() + .unsorted_to_dataset_data(), // collect into dataset + ) + .background_color("palegreen") + .border_color("green") + .border_width(2) + .label("Dataset 1") + .y_axis_id("y")]), + ) + .options(ChartOptions::new().maintain_aspect_ratio(false)); html!("canvas", { // construct a html canvas element, and after its rendered into the DOM we can insert our chart .prop("id", id) .style("height", "calc(100vh - 270px)") @@ -231,11 +227,11 @@ impl Model { fn show_donut(self: Arc) -> Dom { // construct and render chart here - let three_id = "donut_a"; - let four_id = "donut_b"; + let three_a_id = "donut_a"; + let three_b_id = "donut_b"; - let three_a_chart: Doughnut = Doughnut { - data: { + let three_a_chart = Doughnut::::new(three_a_id) + .data( Dataset::new() .datasets({ [SinglePointDataset::new() @@ -247,14 +243,11 @@ impl Model { "goldenrod", ])] }) - .labels(["Blueberries", "Limes", "Apples", "Lemons"]) - }, - options: ChartOptions::new().maintain_aspect_ratio(false), - id: three_id.to_string(), - ..Default::default() - }; - let three_b_chart: Pie = Pie { - data: { + .labels(["Blueberries", "Limes", "Apples", "Lemons"]), + ) + .options(ChartOptions::new().maintain_aspect_ratio(false)); + let three_b_chart = Pie::::new(three_b_id) + .data( Dataset::new() .datasets({ [SinglePointDataset::new() @@ -266,12 +259,9 @@ impl Model { "goldenrod", ])] }) - .labels(["Blueberries", "Limes", "Apples", "Lemons"]) - }, - options: ChartOptions::new().maintain_aspect_ratio(false), - id: four_id.to_string(), - ..Default::default() - }; + .labels(["Blueberries", "Limes", "Apples", "Lemons"]), + ) + .options(ChartOptions::new().maintain_aspect_ratio(false)); html!("div", { .class("columns") .children([ @@ -279,7 +269,7 @@ impl Model { .class(["column", "is-half"]) .child( html!("canvas", { - .prop("id", three_id) + .prop("id", three_a_id) .style("height", "calc(100vh - 270px)") .after_inserted(move |_| { three_a_chart.into_chart().render() @@ -290,7 +280,7 @@ impl Model { .class(["column", "is-half"]) .child( html!("canvas", { - .prop("id", four_id) + .prop("id", three_b_id) .style("height", "calc(100vh - 270px)") .after_inserted(move |_| { three_b_chart.into_chart().render() @@ -402,7 +392,7 @@ impl Model { move |_: events::Click| { // update scatter chart colour let mut chart: Scatter:: = ChartExt::get_chart_from_id("scatter").expect("Unable to retrieve chart from JS."); - chart.data.get_datasets().get_mut(0).map(|d| { + chart.get_data().get_datasets().get_mut(0).map(|d| { if _self.tick.get() { *d.get_background_color() = "lightcoral".into(); *d.get_border_color() = "red".into(); @@ -436,7 +426,7 @@ impl Model { move |_: events::Click| { // update scatter chart colour let mut chart: Scatter:: = ChartExt::get_chart_from_id("scatter").expect("Unable to retrieve chart from JS."); - chart.data.get_datasets().get_mut(0).map(|d| { + chart.get_data().get_datasets().get_mut(0).map(|d| { if _self.tick.get() { *d.get_background_color() = "lightcoral".into(); *d.get_border_color() = "red".into(); diff --git a/src/bar.rs b/src/bar.rs index 529f995..3a8a087 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -8,16 +8,30 @@ use crate::{objects::*, traits::*, ChartExt}; #[derive(Debug, Clone, Deserialize, Serialize, Default)] pub struct Bar { #[serde(rename = "type")] - pub r#type: BarString, - pub data: Dataset>, - pub options: ChartOptions, - pub id: String, + r#type: BarString, + data: Dataset>, + options: ChartOptions, + id: String, } -impl ChartExt for Bar { +impl ChartExt for Bar { + type DS = Dataset>; + fn get_id(self) -> String { self.id } + fn id(mut self, id: String) -> Self { + self.id = id; + self + } + + fn get_data(&mut self) -> &mut Self::DS { + &mut self.data + } + + fn get_options(&mut self) -> &mut ChartOptions { + &mut self.options + } } #[derive(Debug, Default, Clone)] diff --git a/src/doughnut.rs b/src/doughnut.rs index 2284d37..595be57 100644 --- a/src/doughnut.rs +++ b/src/doughnut.rs @@ -8,15 +8,29 @@ use crate::{objects::*, traits::*, ChartExt}; #[derive(Debug, Clone, Deserialize, Serialize, Default)] pub struct Doughnut { #[serde(rename = "type")] - pub r#type: DoughnutString, - pub data: Dataset>, - pub options: ChartOptions, - pub id: String, + r#type: DoughnutString, + data: Dataset>, + options: ChartOptions, + id: String, } -impl ChartExt for Doughnut { +impl ChartExt for Doughnut { + type DS = Dataset>; + fn get_id(self) -> String { self.id } + fn id(mut self, id: String) -> Self { + self.id = id; + self + } + + fn get_data(&mut self) -> &mut Self::DS { + &mut self.data + } + + fn get_options(&mut self) -> &mut ChartOptions { + &mut self.options + } } #[derive(Debug, Default, Clone)] diff --git a/src/lib.rs b/src/lib.rs index 71a3744..a7a9309 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,8 +20,30 @@ use serde::{de::DeserializeOwned, Serialize}; pub use traits::*; pub use utils::*; -pub trait ChartExt: DeserializeOwned + Serialize { - fn get_id(self) -> String; +pub trait ChartExt: + DeserializeOwned + Serialize + Default +{ + type DS; + + fn new(id: impl AsRef) -> 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 { + *self.get_data() = data.into(); + self + } + + fn get_options(&mut self) -> &mut ChartOptions; + fn options(mut self, options: impl Into>) -> Self { + *self.get_options() = options.into(); + self + } + fn into_chart(self) -> Chart { Chart { diff --git a/src/pie.rs b/src/pie.rs index 95d4220..74ed64f 100644 --- a/src/pie.rs +++ b/src/pie.rs @@ -8,16 +8,30 @@ use crate::{objects::*, traits::*, ChartExt}; #[derive(Debug, Clone, Deserialize, Serialize, Default)] pub struct Pie { #[serde(rename = "type")] - pub r#type: PieString, - pub data: Dataset>, - pub options: ChartOptions, - pub id: String, + r#type: PieString, + data: Dataset>, + options: ChartOptions, + id: String, } -impl ChartExt for Pie { +impl ChartExt for Pie { + type DS = Dataset>; + fn get_id(self) -> String { self.id } + fn id(mut self, id: String) -> Self { + self.id = id; + self + } + + fn get_data(&mut self) -> &mut Self::DS { + &mut self.data + } + + fn get_options(&mut self) -> &mut ChartOptions { + &mut self.options + } } #[derive(Debug, Default, Clone)] diff --git a/src/scatter.rs b/src/scatter.rs index a89f465..f80c363 100644 --- a/src/scatter.rs +++ b/src/scatter.rs @@ -8,16 +8,30 @@ use crate::{objects::*, traits::*, ChartExt}; #[derive(Debug, Clone, Deserialize, Serialize, Default)] pub struct Scatter { #[serde(rename = "type")] - pub r#type: ScatterString, - pub data: Dataset>, - pub options: ChartOptions, - pub id: String, + r#type: ScatterString, + data: Dataset>, + options: ChartOptions, + id: String, } -impl ChartExt for Scatter { +impl ChartExt for Scatter { + type DS = Dataset>; + fn get_id(self) -> String { self.id } + fn id(mut self, id: String) -> Self { + self.id = id; + self + } + + fn get_data(&mut self) -> &mut Self::DS { + &mut self.data + } + + fn get_options(&mut self) -> &mut ChartOptions { + &mut self.options + } } #[derive(Debug, Default, Clone)]