Skip to content

Commit

Permalink
Merge pull request #5 from elonaire/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jun 29, 2024
2 parents 30d514f + efea55d commit 6f8f8db
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion 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 = "visualize-yew"
version = "0.20.9"
version = "0.20.10"
edition = "2021"
description = "A simple data visualization library for Yew"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Visualize Yew
![Visualize Yew CI](https://github.com/elonaire/visualize-yew/actions/workflows/main.yml/badge.svg)
![Visualize Yew CI](https://github.com/elonaire/visualize-yew/actions/workflows/main.yml/badge.svg?event=merge)
![Stable Version](https://img.shields.io/crates/v/visualize-yew)

This is a simple crate to help you visualize your data in the browser using Yew. It is a wrapper around the yew crate that provides a simple API to create charts.
Expand Down
9 changes: 7 additions & 2 deletions src/charts/bar_chart/bar_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ pub fn BarChart(props: &BarChartProps) -> Html {
let device_pixel_ratio = window().unwrap().device_pixel_ratio();
let parent = canvas.parent_element().unwrap();
let width = parent.client_width() as f64;
let height = parent.client_height() as f64;
// let height = parent.client_height() as f64;
let height = width * 0.6;

// Set the canvas dimensions to match its parent's dimensions
canvas.set_width((width * device_pixel_ratio) as u32);
Expand Down Expand Up @@ -172,7 +173,11 @@ mod tests {

let props = BarChartProps {
data,
config: BarChartConfig::default(),
config: BarChartConfig {
bar_color: "blue".to_string(),
grid_color: "gray".to_string(),
axis_color: "black".to_string(),
}
};

draw_bar_chart(&context, width, height, &props);
Expand Down
12 changes: 4 additions & 8 deletions src/charts/doughnut_chart/doughnut_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,12 @@ pub fn DoughnutChart(props: &DoughnutChartProps) -> Html {
let device_pixel_ratio = window().unwrap().device_pixel_ratio();
let parent = canvas.parent_element().unwrap();
let width = parent.client_width() as f64;
let height = parent.client_height() as f64;
// let height = parent.client_height() as f64;
let height = width * 0.8;

// Set the canvas dimensions to match its parent's dimensions
canvas.set_width((width * device_pixel_ratio) as u32);
if height < width {
canvas.set_height((height * device_pixel_ratio) as u32);
} else {
canvas.set_height((width * device_pixel_ratio) as u32);

}
canvas.set_height((height * device_pixel_ratio) as u32);

// Scale the context to account for the device pixel ratio
context.scale(device_pixel_ratio, device_pixel_ratio).unwrap();
Expand All @@ -72,7 +68,7 @@ pub fn DoughnutChart(props: &DoughnutChartProps) -> Html {
let legend_html = if props.config.show_legend {

html! {
<div style="display: flex; flex-direction: row; gap: 5px; margin-bottom: 1em;">
<div style="display: flex; flex-direction: row; gap: 5px; flex-wrap: wrap;">
{ for props.data.iter().map(|(label, _value, color)| {
html! {
<div style="display: flex; flex-direction: row; align-items: center; gap: 2px;">
Expand Down
14 changes: 7 additions & 7 deletions src/charts/line_chart/line_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ pub fn LineCurveChart(props: &LineCurveChartProps) -> Html {
let device_pixel_ratio = window().unwrap().device_pixel_ratio();
let parent = canvas.parent_element().unwrap();
let width = parent.client_width() as f64;
let height = parent.client_height() as f64;
// let height = parent.client_height() as f64;
let height = width * 0.6;

// Set the canvas dimensions to match its parent's dimensions
// Set the canvas dimensions to match its parent's dimensions
canvas.set_width((width * device_pixel_ratio) as u32);
canvas.set_height((height * device_pixel_ratio) as u32);
Expand Down Expand Up @@ -111,7 +113,7 @@ pub fn LineCurveChart(props: &LineCurveChartProps) -> Html {
// Render the legend if enabled
let legend_html = if props.config.show_legend {
html! {
<div style="display: flex; flex-direction: row; gap: 5px; margin-bottom: 1em;">
<div style="display: flex; flex-direction: row; gap: 5px; flex-wrap: wrap;">
{ for props.data.iter().map(|(series, _)| {
html! {
<div style="display: flex; flex-direction: row; align-items: center; gap: 2px;">
Expand All @@ -126,12 +128,11 @@ pub fn LineCurveChart(props: &LineCurveChartProps) -> Html {
html! {}
};


html! {
<div style="width: 100%; height: 100%;">
<div>
// legend
{ legend_html }
<canvas ref={canvas_ref} style="width: 90%; height: 90%;"></canvas>
<canvas ref={canvas_ref} style="width: 100%; height: 100%; box-sizing: border-box;"></canvas>
</div>
}
}
Expand Down Expand Up @@ -178,7 +179,6 @@ fn draw_multiline_chart(
context.stroke();
}


// Draw the y-axis grid lines and labels
context.set_stroke_style(&JsValue::from_str("#cccccc"));
context.set_line_width(1.0);
Expand Down Expand Up @@ -303,7 +303,7 @@ mod tests {
let context = mock_context();
let width = 800.0;
let height = 600.0;

let props = LineCurveChartProps {
data: vec![
(
Expand Down
17 changes: 5 additions & 12 deletions src/charts/pie_chart/pie_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,12 @@ pub fn PieChart(props: &PieChartProps) -> Html {
let device_pixel_ratio = window().unwrap().device_pixel_ratio();
let parent = canvas.parent_element().unwrap();
let width = parent.client_width() as f64;
let height = parent.client_height() as f64;
// let height = parent.client_height() as f64;
let height = width * 0.8;

// Set the canvas dimensions to match its parent's dimensions
canvas.set_width((width * device_pixel_ratio) as u32);
if height < width {
canvas.set_height((height * device_pixel_ratio) as u32);
} else {
canvas.set_height((width * device_pixel_ratio) as u32);

}
canvas.set_height((height * device_pixel_ratio) as u32);

// Scale the context to account for the device pixel ratio
context.scale(device_pixel_ratio, device_pixel_ratio).unwrap();
Expand All @@ -82,7 +78,7 @@ pub fn PieChart(props: &PieChartProps) -> Html {
let legend_html = if props.config.show_legend {

html! {
<div style="display: flex; flex-direction: row; gap: 5px; margin-bottom: 1em;">
<div style="display: flex; flex-direction: row; gap: 5px; flex-wrap: wrap;">
{ for props.data.iter().map(|data_point| {
html! {
<div style="display: flex; flex-direction: row; align-items: center; gap: 2px;">
Expand Down Expand Up @@ -190,10 +186,7 @@ mod tests {
DataPoint { name: "C".to_string(), value: 30, color: "".to_string() },
DataPoint { name: "D".to_string(), value: 40, color: "".to_string() },
],
config: PieChartConfig {
text_align: "center".to_string(),
show_legend: true,
},
config: PieChartConfig::default(),
};

draw_pie_chart(&context, width, height, &props);
Expand Down

0 comments on commit 6f8f8db

Please sign in to comment.