Skip to content

Commit 353c6a6

Browse files
committed
Add accessor for chart_area drawing area
1 parent b403db3 commit 353c6a6

File tree

6 files changed

+52
-17
lines changed

6 files changed

+52
-17
lines changed

examples/layout.rs

+35-11
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,44 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
let mut chart = ChartLayout::new(&root);
1212
chart
1313
.set_chart_title_text("Chart Title")?
14-
.set_top_label_text("A label at the top")?
1514
.set_chart_title_style(("serif", 60.).into_font().with_color(&RED))?
16-
.set_left_label_text("Left label")?
15+
.set_left_label_text("Ratio of Sides")?
1716
.set_right_label_text("Right label")?
18-
.set_bottom_label_text("Bottom label")?
17+
.set_bottom_label_text("Radians")?
1918
.set_bottom_label_margin(10.)?
20-
.set_top_label_margin(10.)?
21-
.draw()?;
22-
23-
let extent = chart.get_chart_area_extent()?;
24-
root.draw(&Rectangle::new(extent.into_array_of_tuples(), &BLUE))?;
25-
let extent = chart.get_chart_title_extent()?;
26-
root.draw(&Rectangle::new(extent.into_array_of_tuples(), &BLUE))?;
27-
//dbg!();
19+
.set_left_label_margin(10.)?
20+
.set_right_label_margin(10.)?
21+
.draw()?;
22+
23+
// If we extract a drawing area corresponding to a chart area, we can
24+
// use the usual chart API to draw.
25+
let da_chart = chart.get_chart_drawing_area()?;
26+
let x_axis = (-3.4f32..3.4).step(0.1);
27+
let mut cc = ChartBuilder::on(&da_chart)
28+
.margin(5)
29+
.set_all_label_area_size(15)
30+
.build_cartesian_2d(-3.4f32..3.4, -1.2f32..1.2f32)?;
31+
32+
cc.configure_mesh()
33+
.x_labels(20)
34+
.y_labels(10)
35+
.disable_mesh()
36+
.x_label_formatter(&|v| format!("{:.1}", v))
37+
.y_label_formatter(&|v| format!("{:.1}", v))
38+
.draw()?;
39+
40+
cc.draw_series(LineSeries::new(x_axis.values().map(|x| (x, x.sin())), &RED))?
41+
.label("Sine")
42+
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED));
43+
44+
cc.draw_series(LineSeries::new(
45+
x_axis.values().map(|x| (x, x.cos())),
46+
&BLUE,
47+
))?
48+
.label("Cosine")
49+
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &BLUE));
50+
51+
cc.configure_series_labels().border_style(&BLACK).draw()?;
2852

2953
Ok(())
3054
}

src/chart/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, Cartesia
311311
.map(|(w, _)| w)
312312
.unwrap_or(0) as i32
313313
} else {
314-
// Don't ever do the layout estimationfor the drawing area that is either not
314+
// Don't ever do the layout estimation for the drawing area that is either not
315315
// the right one or the tick mark is inward.
316316
0
317317
}

src/chart/layout/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ impl<'a, 'b, DB: DrawingBackend> ChartLayout<'a, 'b, DB> {
256256
Ok(self)
257257
}
258258

259+
/// Return a drawing area which corresponds to the `chart_area` of the current layout.
260+
/// [`layout`] should be called before this function.
261+
pub fn get_chart_drawing_area(
262+
&mut self,
263+
) -> Result<DrawingArea<DB, Shift>, DrawingAreaErrorKind<DB::ErrorType>> {
264+
let chart_area_extent = self.get_chart_area_extent()?;
265+
Ok(DrawingArea::clone(self.root_area).shrink(
266+
(chart_area_extent.x0, chart_area_extent.y0),
267+
chart_area_extent.size(),
268+
))
269+
}
270+
259271
/// Set the text of a label. If the text is a blank screen, the label is cleared.
260272
#[inline(always)]
261273
fn set_text<S: AsRef<str>>(elm: &mut Label, text: S) {

src/chart/layout/nodes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use paste::paste;
2-
use std::iter::once;
32
use std::{
43
collections::HashMap,
54
ops::{Add, Sub},

src/coord/ranged1d/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl KeyPointWeight {
109109
/// The trait for a hint provided to the key point algorithm used by the coordinate specs.
110110
/// The most important constraint is the `max_num_points` which means the algorithm could emit no more than specific number of key points
111111
/// `weight` is used to determine if this is used as a bold grid line or light grid line
112-
/// `bold_points` returns the max number of coresponding bold grid lines
112+
/// `bold_points` returns the max number of corresponding bold grid lines
113113
pub trait KeyPointHint {
114114
/// Returns the max number of key points
115115
fn max_num_points(&self) -> usize;
@@ -178,12 +178,12 @@ impl KeyPointHint for LightPoints {
178178
/// Which is used to describe any 1D axis.
179179
pub trait Ranged {
180180
/// This marker decides if Plotters default [ValueFormatter](trait.ValueFormatter.html) implementation should be used.
181-
/// This assicated type can be one of follow two types:
181+
/// This associated type can be one of follow two types:
182182
/// - [DefaultFormatting](struct.DefaultFormatting.html) will allow Plotters automatically impl
183183
/// the formatter based on `Debug` trait, if `Debug` trait is not impl for the `Self::Value`,
184184
/// [ValueFormatter](trait.ValueFormatter.html) will not impl unless you impl it manually.
185185
///
186-
/// - [NoDefaultFormatting](struct.NoDefaultFormatting.html) Disable the automatical `Debug`
186+
/// - [NoDefaultFormatting](struct.NoDefaultFormatting.html) Disable the automatic `Debug`
187187
/// based value formatting. Thus you have to impl the
188188
/// [ValueFormatter](trait.ValueFormatter.html) manually.
189189
///

src/coord/ranged2d/cartesian.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
The 2-dimensional cartesian coordinate system.
33
44
This module provides the 2D cartesian coordinate system, which is composed by two independent
5-
ranged 1D coordinate sepcification.
5+
ranged 1D coordinate specification.
66
77
This types of coordinate system is used by the chart constructed with [ChartBuilder::build_cartesian_2d](../../chart/ChartBuilder.html#method.build_cartesian_2d).
88
*/

0 commit comments

Comments
 (0)