diff options
Diffstat (limited to 'examples/chart.rs')
-rw-r--r-- | examples/chart.rs | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/examples/chart.rs b/examples/chart.rs index 15fb23d..0502a5e 100644 --- a/examples/chart.rs +++ b/examples/chart.rs @@ -10,11 +10,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { let (upper, lower) = root_area.split_vertically(512); + let x_axis = (-3.4f32..3.4).step(0.1); + let mut cc = ChartBuilder::on(&upper) .margin(5) .set_all_label_area_size(50) .caption("Sine and Cosine", ("sans-serif", 40).into_font()) - .build_ranged(-3.4f32..3.4f32, -1.2f32..1.2f32)?; + .build_cartesian_2d(-3.4f32..3.4, -1.2f32..1.2f32)?; cc.configure_mesh() .x_labels(20) @@ -24,20 +26,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { .y_label_formatter(&|v| format!("{:.1}", v)) .draw()?; - cc.draw_series(LineSeries::new( - (0..12).map(|x| ((x - 6) as f32 / 2.0, ((x - 6) as f32 / 2.0).sin())), - &RED, - ))? - .label("Sine") - .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED)); + 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.draw_series(LineSeries::new( - (0..6800).map(|x| { - ( - (x - 3400) as f32 / 1000.0, - ((x - 3400) as f32 / 1000.0).cos(), - ) - }), + x_axis.values().map(|x| (x, x.cos())), &BLUE, ))? .label("Cosine") @@ -48,14 +42,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { /* // It's possible to use a existing pointing element cc.draw_series(PointSeries::<_, _, Circle<_>>::new( - (0..6).map(|x| ((x - 3) as f32 / 1.0, ((x - 3) as f32 / 1.0).sin())), + (-3.0f32..2.1f32).step(1.0).values().map(|x| (x, x.sin())), 5, Into::<ShapeStyle>::into(&RGBColor(255,0,0)).filled(), ))?;*/ // Otherwise you can use a function to construct your pointing element yourself cc.draw_series(PointSeries::of_element( - (0..6).map(|x| ((x - 3) as f32 / 1.0, ((x - 3) as f32 / 1.0).sin())), + (-3.0f32..2.1f32).step(1.0).values().map(|x| (x, x.sin())), 5, ShapeStyle::from(&RED).filled(), &|coord, size, style| { @@ -80,19 +74,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { format!("y = x^{}", 1 + 2 * idx), ("sans-serif", 40).into_font(), ) - .build_ranged(-1f32..1f32, -1f32..1f32)?; + .build_cartesian_2d(-1f32..1f32, -1f32..1f32)?; cc.configure_mesh().x_labels(5).y_labels(3).draw()?; cc.draw_series(LineSeries::new( - (-100..100).map(|x| { - ( - x as f32 / 100.0, - (x as f32 / 100.0).powf(idx as f32 * 2.0 + 1.0), - ) - }), + (-1f32..1f32) + .step(0.01) + .values() + .map(|x| (x, x.powf(idx as f32 * 2.0 + 1.0))), &BLUE, ))?; } Ok(()) } +#[test] +fn entry_point() { + main().unwrap() +} |