Skip to content

Commit

Permalink
Adding contribution to total option to Bar chart (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jun 20, 2016
1 parent 55c549d commit deb197a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
13 changes: 9 additions & 4 deletions caravel/assets/visualizations/nvd3_vis.js
Expand Up @@ -16,8 +16,14 @@ function nvd3Vis(slice) {
var render = function () {
d3.json(slice.jsonEndpoint(), function (error, payload) {
var width = slice.width();
var fd = payload.form_data;
var barchartWidth = function () {
var bars = d3.sum(payload.data, function (d) { return d.values.length; });
var bars;
if (fd.bar_stacked) {
bars = d3.max(payload.data, function (d) { return d.values.length; });
} else {
bars = d3.sum(payload.data, function (d) { return d.values.length; });
}
if (bars * minBarWidth > width) {
return bars * minBarWidth;
} else {
Expand All @@ -33,7 +39,6 @@ function nvd3Vis(slice) {
}
return '';
}
var fd = payload.form_data;
var viz_type = fd.viz_type;
var f = d3.format('.3s');
var reduceXTicks = fd.reduce_x_ticks || false;
Expand Down Expand Up @@ -61,7 +66,7 @@ function nvd3Vis(slice) {

case 'bar':
chart = nv.models.multiBarChart()
.showControls(true)
.showControls(false)
.groupSpacing(0.1);

if (!reduceXTicks) {
Expand All @@ -77,7 +82,7 @@ function nvd3Vis(slice) {

case 'dist_bar':
chart = nv.models.multiBarChart()
.showControls(true) //Allow user to switch between 'Grouped' and 'Stacked' mode.
.showControls(false) //Allow user to switch between 'Grouped' and 'Stacked' mode.
.reduceXTicks(reduceXTicks)
.rotateLabels(45)
.groupSpacing(0.1); //Distance between each group of bars.
Expand Down
2 changes: 1 addition & 1 deletion caravel/forms.py
Expand Up @@ -228,7 +228,7 @@ def __init__(self, viz):
'reduce_x_ticks': (BetterBooleanField, {
"label": _("Reduce X ticks"),
"default": False,
"description": (
"description": _(
"Reduces the number of X axis ticks to be rendered. "
"If true, the x axis wont overflow and labels may be "
"missing. If false, a minimum width will be applied "
Expand Down
6 changes: 5 additions & 1 deletion caravel/viz.py
Expand Up @@ -1159,7 +1159,7 @@ class DistributionBarViz(DistributionPieViz):
('show_legend', 'bar_stacked'),
('y_axis_format', 'bottom_margin'),
('x_axis_label', 'y_axis_label'),
('reduce_x_ticks', None),
('reduce_x_ticks', 'contribution'),
)
},)
form_overrides = {
Expand Down Expand Up @@ -1198,6 +1198,10 @@ def get_df(self, query_obj=None):
index=self.groupby,
columns=columns,
values=self.metrics)
if fd.get("contribution"):
pt = pt.fillna(0)
pt = pt.T
pt = (pt / pt.sum()).T
pt = pt.reindex(row.index)
return pt

Expand Down

0 comments on commit deb197a

Please sign in to comment.