stacked bar chart
7marjorie opened this issue · 4 comments
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,
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:
- http://stackoverflow.com/questions/14713503/how-to-handle-layers-with-missing-data-points-in-d3-layout-stack
- http://stackoverflow.com/questions/17415406/filling-the-gaps-in-d3-array-nesting
- d3.nest() "internal" and "external" keys - This one has a nice solution.
Also, this would be a great question for the D3 mailing list, perhaps someone there has a better answer.
@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.