Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stacked bars are offset by a wrong value #4454

Open
rctlmk opened this issue May 3, 2024 · 0 comments · May be fixed by #4455
Open

Stacked bars are offset by a wrong value #4454

rctlmk opened this issue May 3, 2024 · 0 comments · May be fixed by #4455
Labels
bug Something is broken

Comments

@rctlmk
Copy link

rctlmk commented May 3, 2024

Describe the bug
I was working on a little app with a stacked bar chart, and came across this possible bug (black lines were added in Paint for illustrative purposes):
filtered

Each bar chart is based upon a Vec<(f64, f64)>, which contains pairs of (value, frequency), all vectors have the same length, value isn't being used as the Bar's argument intentionally.

// declared elsewhere
frequencies: Vec<Vec<(f64, f64)>>,

// plot rendering
let mut charts = vec![];
for (frequencies, label) in self.frequencies.iter().zip(self.labels.iter()) {
    let bars = frequencies
        .iter()
        .enumerate()
        .filter(|(_, (_, f))| f > &0.0)
        .map(|(i, (v, f))| Bar::new(i as f64, *f).width(self.width).name(v))
        .collect();

    let mut chart = BarChart::new(bars).name(&label);
    if !self.vertical {
        chart = chart.horizontal();
    }

    if charts.len() > 0 {
        let other_charts: Vec<_> = charts.iter().collect();
        chart = chart.stack_on(&other_charts);
    }

    charts.push(chart);
}

Zero frequencies are filtered out, so the total number of bars will vary. The reason for filtering is that a Bar with zero height will be rendered anyway, and to me it looks a little confusing (the plot's aspect ratio is different, but all values are the same):
non-filtered
Here, the offset values are correct, since the number of bars in both charts is the same.

To Reproduce
The easiest way to reproduce this behavior is to modify the demo app by commenting out this line:
demo-app

Expected behavior
I expect the modified demo app to look like this:
demo-app-fix

Additional context
I assume it's a bug, since I never used stacked bar charts before.

@rctlmk rctlmk added the bug Something is broken label May 3, 2024
@rctlmk rctlmk linked a pull request May 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant