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

Ensure x-axis in stacked bar charts is linear #2086

Merged
merged 8 commits into from
Apr 12, 2023

Conversation

sophiamersmann
Copy link
Member

@sophiamersmann sophiamersmann commented Apr 6, 2023

fixes #1865

Problem

If observations are unevenly spaced, stacked bar charts end up with a non-linear x-axis.

Solution

To enforce uniform spacing along the axis, this PR implements Marcel's suggestion that involves computed the greatest common divisor. In Marcel's words, the gist of it is:

given sortedYears = [1940, 1960, 1970, 1985]
compute deltas = [20, 10, 15]
compute gcd(deltas) = 5
=> the "minimum display interval" is 5 in this case
=> show 1940, 1945, 1950, 1955, 1960, 1965, ..., 1985 on the x axis

Weirdnesses

The SVG tester comes back with one problematic chart:

Screenshot 2023-04-06 at 14 35 02

Problem: This chart shows monthly data but since we currently have no native way to represent monthly data, this data is treated as daily data where only the 15th of every month is assigned a data point. Working with daily measurements becomes a problem here because slightly different deltas (number of days between measurements, i.e. 28, 30 or 31 days) lead to a small greatest common divisor (=1), and this leads in turn to many fake data point added (one for every day!).

Possible Solutions: This would be solved by making monthly data a first-class citizen as suggested in this issue. Going forward, we could either:

@marcelgerber
Copy link
Member

Cool, this is working well already!

I think we should do one of these two. The ugly hack could just be that we disable this behaviour for charts with daily data.

Copy link
Member

@marcelgerber marcelgerber left a comment

Choose a reason for hiding this comment

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

That's quite a lot of comments! Sorry, totally didn't want to roast your code; there's just a bunch of edge cases in here that don't work well, and a bunch of perf improvements for the common case.

@sophiamersmann
Copy link
Member Author

Thanks for the review!

The ugly hack could just be that we disable this behaviour for charts with daily data.

Makes sense! Let's do that for the moment then :)

@sophiamersmann sophiamersmann force-pushed the fix-non-linear-x-axis-in-bar-charts branch from 404b453 to 848df7a Compare April 12, 2023 08:31
@sophiamersmann sophiamersmann merged commit 116b9cc into master Apr 12, 2023
@sophiamersmann sophiamersmann deleted the fix-non-linear-x-axis-in-bar-charts branch April 12, 2023 09:24
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.

Stacked bar charts should always show a linear x axis, even when there's missing data
2 participants