Skip to content

Commit

Permalink
moar building
Browse files Browse the repository at this point in the history
  • Loading branch information
Billy-Sheppard committed Sep 26, 2024
1 parent 503091e commit 1bc6220
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 96 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
132 changes: 61 additions & 71 deletions examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,25 @@ impl Model {
// construct and render chart here
let id = "scatter";

let chart = Scatter::<NoAnnotations> {
let chart = Scatter::<NoAnnotations>::new(id)
// we use <NoAnnotations> 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)")
Expand All @@ -125,9 +124,9 @@ impl Model {
let id = "line";

let chart =
Scatter::<NoAnnotations> {
Scatter::<NoAnnotations>::new(id)
// we use <NoAnnotations> here to type hint for the compiler
data: Dataset::new().datasets([
.data(Dataset::new().datasets([
XYDataset::new()
.data(
x.iter()
Expand Down Expand Up @@ -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()
Expand All @@ -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)")
Expand All @@ -196,30 +193,29 @@ impl Model {
// construct and render chart here
let id = "bar";

let chart = Bar::<NoAnnotations> {
let chart = Bar::<NoAnnotations>::new(id)
// we use <NoAnnotations> 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)")
Expand All @@ -231,11 +227,11 @@ impl Model {

fn show_donut(self: Arc<Self>) -> 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<NoAnnotations> = Doughnut {
data: {
let three_a_chart = Doughnut::<NoAnnotations>::new(three_a_id)
.data(
Dataset::new()
.datasets({
[SinglePointDataset::new()
Expand All @@ -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<NoAnnotations> = Pie {
data: {
.labels(["Blueberries", "Limes", "Apples", "Lemons"]),
)
.options(ChartOptions::new().maintain_aspect_ratio(false));
let three_b_chart = Pie::<NoAnnotations>::new(three_b_id)
.data(
Dataset::new()
.datasets({
[SinglePointDataset::new()
Expand All @@ -266,20 +259,17 @@ 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([
html!("div", {
.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()
Expand All @@ -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()
Expand Down Expand Up @@ -402,7 +392,7 @@ impl Model {
move |_: events::Click| {
// update scatter chart colour
let mut chart: Scatter::<NoAnnotations> = 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();
Expand Down Expand Up @@ -436,7 +426,7 @@ impl Model {
move |_: events::Click| {
// update scatter chart colour
let mut chart: Scatter::<NoAnnotations> = 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();
Expand Down
24 changes: 19 additions & 5 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,30 @@ use crate::{objects::*, traits::*, ChartExt};
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct Bar<A: Annotation> {
#[serde(rename = "type")]
pub r#type: BarString,
pub data: Dataset<Vec<XYDataset>>,
pub options: ChartOptions<A>,
pub id: String,
r#type: BarString,
data: Dataset<Vec<XYDataset>>,
options: ChartOptions<A>,
id: String,
}

impl<A: Annotation + DeserializeOwned> ChartExt for Bar<A> {
impl<A: Annotation + DeserializeOwned> ChartExt<A> for Bar<A> {
type DS = Dataset<Vec<XYDataset>>;

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<A> {
&mut self.options
}
}

#[derive(Debug, Default, Clone)]
Expand Down
24 changes: 19 additions & 5 deletions src/doughnut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,29 @@ use crate::{objects::*, traits::*, ChartExt};
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct Doughnut<A: Annotation> {
#[serde(rename = "type")]
pub r#type: DoughnutString,
pub data: Dataset<Vec<SinglePointDataset>>,
pub options: ChartOptions<A>,
pub id: String,
r#type: DoughnutString,
data: Dataset<Vec<SinglePointDataset>>,
options: ChartOptions<A>,
id: String,
}
impl<A: Annotation + DeserializeOwned> ChartExt for Doughnut<A> {
impl<A: Annotation + DeserializeOwned> ChartExt<A> for Doughnut<A> {
type DS = Dataset<Vec<SinglePointDataset>>;

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<A> {
&mut self.options
}
}

#[derive(Debug, Default, Clone)]
Expand Down
26 changes: 24 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 {
Expand Down
24 changes: 19 additions & 5 deletions src/pie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,30 @@ use crate::{objects::*, traits::*, ChartExt};
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct Pie<A: Annotation> {
#[serde(rename = "type")]
pub r#type: PieString,
pub data: Dataset<Vec<SinglePointDataset>>,
pub options: ChartOptions<A>,
pub id: String,
r#type: PieString,
data: Dataset<Vec<SinglePointDataset>>,
options: ChartOptions<A>,
id: String,
}

impl<A: Annotation + DeserializeOwned> ChartExt for Pie<A> {
impl<A: Annotation + DeserializeOwned> ChartExt<A> for Pie<A> {
type DS = Dataset<Vec<SinglePointDataset>>;

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<A> {
&mut self.options
}
}

#[derive(Debug, Default, Clone)]
Expand Down
Loading

0 comments on commit 1bc6220

Please sign in to comment.