-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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 bar chart #10
Comments
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. |
This great, nice work! |
That would be great. Would love to switch to this from Highcharts but need stacked bars and stacked area charts. Great work so far! |
+1 on both stacked bar charts and stacked line area plots |
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. |
👍 #37 is @Verran42's pr |
any plans for tooltip integration for this? |
Bump, was just about to implement this myself. |
Maybe we can think about the packager after, and include this cool feature first !? |
An implementation on top of the existing bar chart (setting the option |
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. |
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 Do you have the dataset? Might it work with #168 ? |
#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. |
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. |
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. |
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. |
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? |
Indeed! I'll try and implement the thousand separators in my fork, but I guess all this work will have to be redone after the big refactor. |
@jphellemons it is posible with my fork #168 Something like: var data = {
labels: ['January', 'February', 'March', 'April'],
datasets: [
{
// 2012
fillColor: '#EF2929',
strokeColor: '#CCC',
data: [60, 30, 25, 82]
},
{
// 2013
fillColor: '#729FCF',
strokeColor: '#CCC',
data: [40, 20, 75, 12]
},
{
// 2014
fillColor: '#FCE94F',
strokeColor: '#CCC',
data: [22, 34, 75, 92]
},
{
// 2015
fillColor: '#AD7FA8',
strokeColor: '#CCC',
data: [10, 34, 65, 42]
},
]
};
var ctx = document.getElementById('bars').getContext('2d');
var chart = new Chart(ctx).Bar(data, {stacked: true}); Notice the |
Thank you @jairtrejo but I meant something like this:
|
Oh! I see. You mean multiple stacked bar charts per month. I am affraid it 2014-02-06 JP Hellemons notifications@github.com:
Jair Trejo |
Hi, Is there a way to get the scale labels on the right instead of the left? Currently I use 2 canvases to mix Line and Bar, is there a better way? Cheers, Marcel |
This would be a perfect opportunity for someone to create a new chart type extension to the library. See http://www.chartjs.org/docs/#advanced-usage-writing-new-chart-types for some details. You could reuse/extend the |
so, was there ever support given for negative values? |
Ditto, @mantisory. +1 for negative values for sure. |
@jitendersaini have you been able to accomplish your described chart? I'm in need of something similar. |
@vjanssens, No actually not yet, that's why I switched to Highcharts. |
Highcharts costs a lot. |
@TheXardas creating a chart like @jitendersaini wanted is possible using the v2.x branch. The combo sample shows how to put bars and lines on the same chart. You could use the stacked sample to see how to make stacked bars and combining the two should give you what you are looking for, |
@etimberg you rock, man! That's just what i need, and i was able to run it in minutes. Thanks! |
Curious, is there an easy way to place the numbers at the top of the bar chart instead of at the bottom? Maybe I'm missing it. |
@vml-egruber which numbers are you referring to? |
@etimberg Sorry, I was referring to the numbers on the x-axis at the bottom. |
@vml-egruber this is possible using v2 but not possible in v1. In v2, include the following in your config. scales: {
xAxes: [{
position: 'top'
}]
} |
Sweet, thanks! |
@TheXardas do you have a fiddle you could share to show how to combine a stacked chart with lines. this is what i have, but it's not quite right |
@cdwilhelm that is exactly the way, that i was able to do it. Why is it not right for you case? |
Is there a way to customize the tooltip for stacked charts, right now it shows color, number(y-axis value) and x-axis label or is it possible to assign a label for each stack shown in a bar. Like: red- 200 Blogs, green- 300 Discussions etc. Basically assigning label to each color. |
Hi All!..Anyone could help me in creating a waterfall chart from bar charts? |
Ok, here's a unique thing on the internet as of today, a sample of a stacked bar chart using chart.js. Attached. |
Guys, @etimberg is right, just combine the two examples. As a whole this would look like this:
|
See this example: http://jtblin.github.io/angular-chart.js/examples/stacked-bars.html |
if you set stacking true on x axes it does not get narrow (at least it worked for me without any negative side effects on version 9) |
Awesome lib, thanks for coding! Any chance for a stacked bar chart?
The text was updated successfully, but these errors were encountered: