plotters
plotters copied to clipboard
chart context as parameter
Hello, how would you extract the following code into its own function?
chart
.draw_series(LineSeries::new(
(-100..100)
.map(|y| y as f64 / 40.0)
.map(|y| ((y * 10.0).sin(), y, (y * 10.0).cos())),
&BLACK,
))?
.label("Line")
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &BLACK));
What I tried:
fn my_function<DB, CT>(
chart: &mut ChartContext<DB, CT>,
) -> Result<(), Box<dyn std::error::Error>>
where
DB: DrawingBackend,
CT: CoordTranslate,
{
chart
.draw_series(LineSeries::new(
(-100..100)
.map(|y| y as f64 / 40.0)
.map(|y| ((y * 10.0).sin(), y, (y * 10.0).cos())),
&BLACK,
))?
.label("Line")
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &BLACK));
Ok(())
}
The compiler complains:
|
28 | .draw_series(LineSeries::new(
| ^^^^^^^^^^^ the trait `for<'b> PointCollection<'b, <CT as plotters::coord::CoordTranslate>::From>` is not implemented for `&'b DynElement<'static, DB, (f64, f64, f64)>`
|
help: consider extending the `where` bound, but there might be an alternative better way to express this requirement
|
25 | CT: CoordTranslate, &'b DynElement<'static, DB, (f64, f64, f64)>: for<'b> PointCollection<'b, <CT as plotters::coord::CoordTranslate>::From>
|
I also tried constraining types more since the apparent type of chart in my case is ChartContext<SVGBackend, Cartesian3D<Linspace<RangedCoordf64, f64, Exact<f64>, Linspace<RangedCoordf64, f64, Exact<f64>, Linspace<RangedCoordf64, f64, Exact<f64>>
The issue is that Exact<f64> is not public?
Thanks for your help,
I want to bump this issue, I haven't found a solution myself, and the result is either code duplication or unattractive workarounds.
I'm happy to help or work on a PR if the y'all have any pointers or thoughts on how you would like this handled.
Hi, I would like to look into the issue; would you be so kind as to provide a Complete example?
On first sight, I would suggest creating a closure instead of a function.
I am also having this issue - so here is a complete example:
use plotters::{prelude::*, series::LineSeries};
fn x_squared<'a, DB: DrawingBackend, CT: CoordTranslate>(chart: &mut ChartContext<'a, DB, CT>) {
chart
.draw_series(LineSeries::new(
(-100..100).map(|int_value| {
let x = int_value as f64 / 10.0;
(x, x.powf(2.0))
}),
&RED,
))
.unwrap();
}
fn main() {
let root = BitMapBackend::new("0.png", (640, 480)).into_drawing_area();
root.fill(&WHITE).unwrap();
let mut chart = ChartBuilder::on(&root)
//.caption("y=x^2", ("sans-serif", 50).into_font())
.margin(5)
.x_label_area_size(50)
.y_label_area_size(80)
.build_cartesian_2d(-10.0..10.0, 0.0..100.0)
.unwrap();
chart.configure_mesh().draw().unwrap();
x_squared(&mut chart);
root.present().unwrap();
}
This results in the error:
error[E0277]: the trait bound `for<'b> &'b DynElement<'static, DB, (f64, f64)>: PointCollection<'b, <CT as plotters::coord::CoordTranslate>::From>` is not satisfied
--> src/main.rs:6:10
|
6 | .draw_series(LineSeries::new(
| ^^^^^^^^^^^ the trait `for<'b> PointCollection<'b, <CT as plotters::coord::CoordTranslate>::From>` is not implemented for `&'b DynElement<'static, DB, (f64, f64)>`
Changing the function signature to:
fn x_squared<DB: DrawingBackend, CT: CoordTranslate<From = (f64, f64)>>(
chart: &mut ChartContext<DB, CT>,
) {
...
}
Worked for me.