Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Stacked bar chart

Open lpilon opened this issue 13 years ago • 50 comments

Awesome lib, thanks for coding! Any chance for a stacked bar chart?

lpilon avatar Mar 18 '13 11:03 lpilon

Will be looking to add different chart types in the future. I'll aim to extract different chart types into a packager app, so you can pick and choose what charts you want to include.

nnnick avatar Mar 18 '13 20:03 nnnick

This great, nice work! I'm also looking for stacked bar/line chart, let me know if I can help ;)

jarthod avatar Mar 18 '13 23:03 jarthod

That would be great. Would love to switch to this from Highcharts but need stacked bars and stacked area charts. Great work so far!

Sigler avatar Mar 19 '13 22:03 Sigler

+1 on both stacked bar charts and stacked line area plots

reinpk avatar Mar 20 '13 07:03 reinpk

I threw something together for the stacked bar chart overnight. It's basically a hacked version of the original bar chart, so it runs off the same data, just puts one on top of the other rather than next to. It seems to work just fine with any positive data, I've tested up to 6 data sets, but negative data sets are not yet supported. I have submitted a pull request.

3stack

bmconklin avatar Mar 20 '13 14:03 bmconklin

:+1:

#37 is @Verran42's pr

aeosynth avatar Apr 10 '13 21:04 aeosynth

any plans for tooltip integration for this?

pinara747 avatar May 06 '13 22:05 pinara747

Bump, was just about to implement this myself.

gambogi avatar May 29 '13 04:05 gambogi

Maybe we can think about the packager after, and include this cool feature first !?

capripot avatar Jun 28 '13 15:06 capripot

An implementation on top of the existing bar chart (setting the option stacked: true): #168

jairtrejo avatar Jul 09 '13 18:07 jairtrejo

Another cool variation would be percent stacked bar charts where the bars all the same height (100%) but the sections vary according to proportion of the components.

schultzter avatar Sep 06 '13 17:09 schultzter

It looks like this solution needs an extreme amount of work. Is is an example I just ran with this version of chart.js, and then the plot using the same data with google analytics. chart_js googleanalytics

docbillnet avatar Dec 11 '13 19:12 docbillnet

The biggest problem you'll see is the stacked chart.js is using a minimum box size for every data value, and then summing those values. So in my 60% column where all the values are too small to even be visible, it gets total up to a over 2,000,000. The actual total for that column is 47.25. This bug should be fairly trivial to fix. What requires a lot of work is finding a way to format the charts js version to look nearly as pretty as the google version... I don't even see an option to make the bar widths wider, or at least appear directly above the labels...

Adding a legend off to the side is also extra manual work... I think it is possible to reform the numbers to have comma thousand separators in chart.js, but it doesn't happen by default.

docbillnet avatar Dec 11 '13 19:12 docbillnet

@docbillnet Do you have the dataset? Might it work with https://github.com/nnnick/Chart.js/pull/168 ?

jairtrejo avatar Dec 11 '13 19:12 jairtrejo

#168 has the same basic bug. It is calculating a yOffset by adding bar heights, which is a rounded up value with offsets added for a minimum size. So there is both accumulation of the minimum values and cumulative round-off error from each value. Preferably what it should do instead is keep track of total data value, and then calculate the yoffset based that... That way there would be no cumulative effect.

That said, it looks like #168 might get the bar widths correct, I will try it and see if it is a step in the right direction.

docbillnet avatar Dec 11 '13 20:12 docbillnet

Looks like I was wrong. #168 probably has cumulative round-off error (I'll have to look more carefully), but it does not accumulate a minimum size. So if I could figure out how to stretch the width of the bars to fill up the space, this would not be half bad.

chart_js

docbillnet avatar Dec 11 '13 20:12 docbillnet

I think I intentionally accounted for the minimum-size accumulation error. I'm not sure why the bars get narrow, but I'll give it a look.

jairtrejo avatar Dec 11 '13 20:12 jairtrejo

Setting barStrokeWidth to 0 makes the bars much wider. I'm not sure why data.datasets.length would be included at all in the barWidth calculation when stacked. That is probably a bug. Looking closer, it looks like yOffset is not a rounded value, so there should not be an accumulated error like I was expecting.

docbillnet avatar Dec 11 '13 20:12 docbillnet

Hi! You are right, that was a bug. I removed that configuration and fixed an horizontal misalignment. I also made it so that stacked charts would start at zero.

Could you check again, please?

jairtrejo avatar Dec 12 '13 01:12 jairtrejo

The fun with building from live data (from a test system), the data used has changed. So I'll show both again. As you can see chart.js is looking much better than before. :)

google-analytics

chart_js

docbillnet avatar Dec 12 '13 13:12 docbillnet