curran / screencasts

Code that goes along with my screencasts.
https://www.youtube.com/playlist?list=PL9yYRbwpkyktAZaphR2UfeYpgNGnIqjs9
MIT License
2.62k stars 2.03k forks source link

stacked bar chart #22

Open 7marjorie opened 7 years ago

7marjorie commented 7 years ago

Hey Curran,

Thanks for the sreencasts, I was very helpful. I made the stacked bar chart and it works ok, if there is no missing data. In your csv file" religionByCountryTop5.csv" for example Brazil Hindu is marked zero. In my case the combination of Brazil Hindu would not be in the file. How can adjust the code to work if data is missing, I plug the chart on oracle BI, so I don't know in advance if data is going to be missing.

Thanks,

curran commented 7 years ago

Hi @7marjorie ,

If the data is missing, would you like to treat that as a zero value? If so, probably the best approach would be to detect which pairs are missing, and add "fake data" that fills them in as zero values, before passing the data into d3.nest.

I think the problem is that stack() expects clean data where all possible pairs are filled in. Probably one way to sol you could do is not use d3.nest(), but instead roll your own nested loop that iterates over all combinations (the cartesian product of domains for the two fields), looks them up in the actual data, and generates a zero value for pairs that are not in the original data.

These answers might also be helpful:

Also, this would be a great question for the D3 mailing list, perhaps someone there has a better answer.

7marjorie commented 7 years ago

Thank you for the response Curran!!

2017-01-25 14:25 GMT+01:00 Curran Kelleher notifications@github.com:

Hi @7marjorie https://github.com/7marjorie ,

If the data is missing, would you like to treat that as a zero value? If so, probably the best approach would be to detect which pairs are missing, and add "fake data" that fills them in as zero values, before passing the data into d3.nest.

I think the problem is that stack() expects clean data where all possible pairs are filled in. Probably one way to sol you could do is not use d3.nest(), but instead roll your own nested loop that iterates over all combinations (the cartesian product https://en.wikipedia.org/wiki/Cartesian_product of domains for the two fields), looks them up in the actual data, and generates a zero value for pairs that are not in the original data.

These answers might also be helpful:

Also, this would be a great question for the D3 mailing list https://groups.google.com/forum/#!forum/d3-js, perhaps someone there has a better answer.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/curran/screencasts/issues/22#issuecomment-275106853, or mute the thread https://github.com/notifications/unsubscribe-auth/AVkHM0Ti7WHycjgMhMbsaaKcwdcXujn3ks5rV01JgaJpZM4LsZNK .

-- Met vriendelijke groeten,

Marjorie

curran commented 7 years ago

@7marjorie No problem! I feel like it's a common problem, and it would be interesting to have a library that does the filling in of the gaps based on the cartesian product.

You might also want to try Datalib https://github.com/vega/datalib/wiki/API-Reference#group-by-aggregation . They have a nice group-by aggregation feature. Although your data is already aggregated, I wonder if you pass it through their group-by processing if it might add in the zeros.

7marjorie commented 7 years ago

Hey Curran,

Thanks i will try it out!

Marjorie

2017-01-31 11:56 GMT+01:00 Curran Kelleher notifications@github.com:

@7marjorie https://github.com/7marjorie No problem! I feel like it's a common problem, and it would be interesting to have a library that does the filling in of the gaps based on the cartesian product.

You might also want to try Datalib https://github.com/vega/ datalib/wiki/API-Reference#group-by-aggregation . They have a nice group-by aggregation feature. Although your data is already aggregated, I wonder if you pass it through their group-by processing if it might add in the zeros.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/curran/screencasts/issues/22#issuecomment-276333419, or mute the thread https://github.com/notifications/unsubscribe-auth/AVkHM1xfj7pci4S7LDGRU4rQS1w9s1xjks5rXxNsgaJpZM4LsZNK .

-- Met vriendelijke groeten,

Marjorie