Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced Flexbox Layout #259

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ version = "^0.3"
optional = true
default_features = false

[dependencies.stretch]
version = "0.3.2"
optional = true

[dependencies.paste]
version = "1.0"
optional = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ttf-parser = { version = "0.12.0", optional = true }
lazy_static = { version = "1.4.0", optional = true }
Expand Down Expand Up @@ -61,7 +69,8 @@ default = [
"ttf",
"image",
"deprecated_items", "all_series", "all_elements",
"full_palette"
"full_palette",
"advanced_layout"
]
all_series = ["area_series", "line_series", "point_series", "surface_series"]
all_elements = ["errorbar", "candlestick", "boxplot", "histogram"]
Expand All @@ -75,6 +84,9 @@ svg_backend = ["plotters-svg"]
# Colors
full_palette = []

# Advanced Layout
advanced_layout = ["stretch", "paste"]

# Elements
errorbar = []
candlestick = []
Expand Down
46 changes: 46 additions & 0 deletions examples/layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use plotters::prelude::*;

const OUT_FILE_NAME: &'static str = "plotters-doc-data/layout2.png";

fn main() -> Result<(), Box<dyn std::error::Error>> {
const W: u32 = 600;
const H: u32 = 400;
let root = BitMapBackend::new(OUT_FILE_NAME, (W, H)).into_drawing_area();
root.fill(&full_palette::WHITE)?;

let x_spec = -3.1..3.01f32;
let y_spec = -1.1..1.1f32;

let mut chart = ChartLayout::new(&root);
chart
.set_chart_title_text("Chart Title")?
.set_chart_title_style(("serif", 60.).into_font().with_color(&RED))?
.set_left_label_text("Ratio of Sides")?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this seems odd as we also have https://docs.rs/plotters/0.3.1/plotters/chart/struct.MeshStyle.html#method.x_desc which have the similar effect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should be a broader discussion about how charts should be configured and what belongs to the chart vs. the coordinate system.

The way plotters seems to do it now is to attach labels to the coordinate system and then adjust whether the axes are printed to the left right/etc. Since ChartLayout doesn't handle coordinate systems right now, I just went an API that labels things based on position.

I think that maybe the best solution would be to have both. If you set via the coordinate system, it would automatically call the set_XXX_label_text. However if a person wanted to set the text without a coordinate system or to override it, they would be free to.

What do you think?

.set_bottom_label_text("Radians")?
.set_bottom_label_margin(10.)?
.set_left_label_margin((0., -5., 0., 10.))?
.build_cartesian_2d(x_spec.clone(), y_spec.clone())?
.draw()?;

// If we extract a drawing area corresponding to a chart area, we can
// use the usual chart API to draw.
let da_chart = chart.get_chart_drawing_area()?;
let x_axis = x_spec.clone().step(0.1);
let mut cc = ChartBuilder::on(&da_chart)
//.margin(5)
//.set_all_label_area_size(15)
.build_cartesian_2d(x_spec.clone(), y_spec.clone())?;

cc.draw_series(LineSeries::new(x_axis.values().map(|x| (x, x.sin())), &RED))?
.label("Sine")
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED));

cc.configure_series_labels().border_style(&BLACK).draw()?;
root.present()?;

Ok(())
}
#[test]
fn entry_point() {
main().unwrap()
}
2 changes: 1 addition & 1 deletion src/chart/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl<'a, 'b, DB: DrawingBackend> ChartBuilder<'a, 'b, DB> {
/// context, where data series can be rendered on.
/// - `x_spec`: The specification of X axis
/// - `y_spec`: The specification of Y axis
/// - `z_sepc`: The specification of Z axis
/// - `z_spec`: The specification of Z axis
/// - Returns: A chart context
#[allow(clippy::type_complexity)]
pub fn build_cartesian_3d<X: AsRangedCoord, Y: AsRangedCoord, Z: AsRangedCoord>(
Expand Down
Loading