Skip to content

Possible to group a stacked bar chart? #2643

Closed

Description

@chadcodes

Is it possible to group a set of stacked bars in this version of ChartJS? I've got a fiddle of a triple stacked bar with 3 datasets, and I would like to group them together so I can add another set of 3 stacked bars. Hopefully this makes sense.

https://jsfiddle.net/7oo4ugbj/

screen shot 2016-05-26 at 10 19 41 am

Activity

  1. potatopeelings commented on May 26, 2016

    @potatopeelings
    Contributor

    @chadcodes if you're ok to set your y axis limits manually check out - http://jsfiddle.net/4rjge8sk/

    jaddx1

  2. etimberg commented on May 26, 2016

    @etimberg
    Member

    @potatopeelings awesome example there!

    Edit: could you PR those changes? I think they'd be a great way to round out the core bar implementation. When you PR, could you implement for both vertical and horizontal bar charts?

  3. chadcodes commented on May 26, 2016

    @chadcodes
    Author

    Definitely ok with setting the y axis manually. My data is normalized going in so I can control the upper limit of the y. Thank you so much for the fiddle, helps me out quite a bit.

    I have a follow up question for you. I've modified your fiddle a little to fit my data and I find I'm doubling up my labels. The data within each grouped stack is the same so I'm not sure how to avoid duplication of the labels. I might be coming at the problem wrong with my datasets.

    http://jsfiddle.net/4rjge8sk/1/

  4. potatopeelings commented on May 26, 2016

    @potatopeelings
    Contributor

    @etimberg - sure, will check if I can figure out a cleaner way (the fiddle uses a lot of shortcuts :-) - I just hide datasets and call the original calculateBarY and calculateBarBase, instead of changing those methods). Might take a week or so because I'm on something else right now. Cheers!

    @chadcodes - just set legend.labels.generateLabels. See http://jsfiddle.net/2xjwoLq0/

    ...
    options: {
      legend: {
        labels: {
          generateLabels: function(chart) {
            return Chart.defaults.global.legend.labels.generateLabels.apply(this, [chart]).filter(function(item, i){
              return i <= 2;
            });
          }
        }
      },
      ...
    

    The above is hardcoded to just take 3 - the nice way would be to use config.data to figure out the count.

  5. etimberg commented on May 26, 2016

    @etimberg
    Member

    @potatopeelings sounds good. changing the calculateBarY and calculateBarBase functions is probably the way to go

  6. chadcodes commented on May 26, 2016

    @chadcodes
    Author

    @potatopeelings amazing work! I really appreciate the help on this. I'll work on using the config.data to update the fiddle and repost it here.

  7. chadcodes commented on May 31, 2016

    @chadcodes
    Author

    @potatopeelings I had a quick follow up question on stack if you have a moment to have a look. Would love to understand a little more how to draw on the chart via the plugin interface. Thanks again!

    http://stackoverflow.com/questions/37546590/add-a-custom-label-to-the-top-or-bottom-of-a-stacked-bar-chart

    similar to this

  8. etimberg commented on Jun 29, 2016

    @etimberg
    Member

    @potatopeelings have you been able to work on a PR for this, or could someone run with your prototype and turn it into a PR?

  9. potatopeelings commented on Jun 30, 2016

    @potatopeelings
    Contributor

    @etimberg - it'd be great if someone can use the prototype for a PR. Sorry I couldn't get around to it as I hoped - got stuck with a couple of things at work.

  10. etimberg commented on Jun 30, 2016

    @etimberg
    Member

    @potatopeelings not a problem. I might look into this over the weekend. I'll link you if I get something done :)

  11. cgountanis commented on Aug 12, 2016

    @cgountanis

    This is a really interesting topic. The value of having a groupable "stacked" bar chart is huge. Any word on this becoming a standard feature or maybe it is already? Working on a project that could use this functionality and it would be great to simply add type: groupableBar, a stack id to the dataset JSON vs. all the examples. I think the hardest part is figuring out the MAX Y value which if it was built in, could be automatic, maybe?

  12. Wildhoney commented on Aug 22, 2016

    @Wildhoney

    +1 👍

  13. 6 remaining items

  14. Wildhoney commented on Sep 29, 2016

    @Wildhoney

    Not yet — it seems as though @potatopeelings or a native Chart.js solution are our only saviours.

  15. etimberg commented on Sep 29, 2016

    @etimberg
    Member

    I would love to have this in the core but I've been focused on fixing tooltips for v2.4. If someone wants to base something on @potatopeelings work then I can happily look at a PR.

  16. cgountanis commented on Sep 29, 2016

    @cgountanis

    I would love to see group/stacked with support as well. It should be a standard type if you ask me. Super useful.

  17. potatopeelings commented on Nov 7, 2016

    @potatopeelings
    Contributor

    PR for grouping stacked bars based on the previous code. Code Climate complains because of the duplication across bar and horizontal bar.

    I think tooltips (mode = group?) and axes (a separate label line with the stack names) will probably need some enhancements to really wrap this one up.

    @Wildhoney - check out the fiddle in the PR (https://jsfiddle.net/Lvj2qnrp/1/). It works now. Cheers!

  18. cgountanis commented on Nov 7, 2016

    @cgountanis

    Hate to ask a noobie question like this but does that mean this is standard in the main release now with examples?

  19. Wildhoney commented on Nov 7, 2016

    @Wildhoney

    Thanks @potatopeelings – good effort 👍

    @cgountanis it will be once the PR has been merged in, tagged, and released.

  20. Rido commented on Dec 12, 2016

    @Rido

    Great work @potatopeelings! 👍

    Today I tried the new code and it works great. I have only one question, is it possible to use the stack as legend filter instead of the dataset label?

  21. rjurado01 commented on Dec 28, 2016

    @rjurado01

    Thanks @potatopeelings, it works !!

  22. added a commit that references this issue on Jan 1, 2017
  23. etimberg commented on Jan 1, 2017

    @etimberg
    Member

    Done in #3563

  24. added this to the Version 2.5 milestone on Jan 1, 2017
  25. potatopeelings commented on Jan 3, 2017

    @potatopeelings
    Contributor

    @Rido - I believe you should be able to override generateLabels to return labels for each group and then override onClick to toggle visibility of datasets of the group (https://github.com/chartjs/Chart.js/blob/master/src/core/core.legend.js)

  26. atulpendse commented on Jan 19, 2017

    @atulpendse

    @potatopeelings - Great work..

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions