Skip to content

chart context as parameter #314

Open
@PicoJr

Description

@PicoJr

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,

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions