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

Skip only last border in stacked bar #10360

Open
liondog opened this issue May 19, 2022 · 2 comments
Open

Skip only last border in stacked bar #10360

liondog opened this issue May 19, 2022 · 2 comments

Comments

@liondog
Copy link

liondog commented May 19, 2022

Expected behavior

Separating stacked bar elements with a border / separator improves readability and is a common UI pattern:
image

For example see https://www.ibm.com/design/language/data-visualization/charts or https://developers.google.com/chart/interactive/docs/gallery/barchart#stacked-bar-charts

Current behavior

Chart.js provides

borderWidth: { top: 1 }

that shows the separators as expected. But the issue is that the separator on the last stacked bar element is not hidden. This leads to the unwanted side-effect that the stacked bar is not pixel perfect anymore to the respective value axis:

image

The issue is that skipping only the last border is not possible using

elements: { bar: { borderSkipped: 'end' // 'top' shows the same unexpected behavior } }

Reproducible sample

https://codepen.io/mimizan/pen/ZErKoWp

Optional extra steps/info to reproduce

No response

Possible solution

Simply not providing a top border on the last element programmatically is not a solution as the user might want to show/hide individual bar elements via the legend. Determining the last visible stacked bar element via ScriptableOptions looks impossible or very complex.

#8941 changed the global bar border radius option to only apply to the last bar element.

I'd propose that:

  • Using any borderSkipped property in the bar options applies only to the last element of a stack,
  • and for convenience 'end' skips the last top border or for horizontal bar charts the last right border / 'start' skips the first bottom border or for horizontal bar charts the first left border.

Context

No response

chart.js version

v3.7.1

Browser name and version

Chrome Canary, Firefox Developer Edition

Link to your project

No response

@LeeLenaleee
Copy link
Collaborator

LeeLenaleee commented May 19, 2022

You can use a scriptable function for this:

options: {
  elements: {
    bar: {
      borderSkipped: (ctx) => {
        const {
          chart,
          datasetIndex
        } = ctx;

        const topVisebleBar = chart.data.datasets.reduce((acc, curr, i) => {
          if (!chart.getDatasetMeta(i).hidden) {
            acc = i;
          }

          return acc;
        }, 0);

        return datasetIndex === topVisebleBar ? 'end' : 'start'
      }
    }
  }
}

https://codepen.io/leelenaleee/pen/RwQVJZW

@kurkle
Copy link
Member

kurkle commented May 23, 2022

In the original pen there are options defined twice, that is not something you can do.

Is this what you are trying to do: https://codepen.io/kurkle/pen/MWQvMeG ?

(Probably the scriptable option that @LeeLenaleee suggested is what you need)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants