Skip to content

Conversation

oldgalileo
Copy link
Contributor

Allow using f32 key point ranges when constructing a Cartesian2d chart:

fn main() -> Result<_> {
    // Chart setup ...
    let mut chart = ChartBuilder::on(&top)
        .x_label_area_size(20)
        .y_label_area_size(40)
        .build_cartesian_2d(
            (0f32..10f32)
                .with_key_points((0f32..10f32)
                    .step(0.5)
                    .values()
                    .map(|x| x * PI).collect()),
            0f32..2.5f32
        ).unwrap();

    
    chart.configure_mesh()
        .x_labels(5)
        .y_labels(2)
        .draw()?;
        
    // Draw stuff ...
}

This code fails to compile, and will complain with the following:

error[E0599]: the method `configure_mesh` exists for struct `ChartContext<'_, BitMapBackend<'_>, Cartesian2d<WithKeyPoints<RangedCoordf32>, RangedCoordf32>>`, but its trait bounds were not satisfied
  --> src/bin/plotting.rs:30:11
   |
30 |     chart.configure_mesh()
   |           ^^^^^^^^^^^^^^ method cannot be called due to unsatisfied trait bounds
   |
  ::: $HOME/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.7/src/coord/ranged1d/combinators/ckps.rs:16:1
   |
16 | pub struct WithKeyPoints<Inner: Ranged> {
   | --------------------------------------- doesn't satisfy `<_ as Ranged>::FormatOption = DefaultFormatting` or `WithKeyPoints<RangedCoordf32>: ValueFormatter<f32>`
   |
   = note: the following trait bounds were not satisfied:
           `<WithKeyPoints<RangedCoordf32> as plotters::prelude::Ranged>::FormatOption = DefaultFormatting`
           which is required by `WithKeyPoints<RangedCoordf32>: ValueFormatter<f32>`

My change addresses the immediate issue, although I suspect there's a more elegant fix here. Couldn't find a quick fix for the issue of a default implementation not existing when T::FormatOption != DefaultFormatting, but it occurred to me that maybe a macro at least would be better? Although conversely it's only for f32 and f64 at the moment so I'm not sure.

Copy link
Contributor

@10ne1 10ne1 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@AaronErhardt AaronErhardt left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@AaronErhardt AaronErhardt merged commit 48ed63f into plotters-rs:master Nov 8, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants