Skip to content
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

Support showing the values on top of the bars #777

Merged
merged 1 commit into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions caravel/assets/visualizations/nvd3_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require('../node_modules/nvd3/build/nv.d3.min.css');
require('./nvd3_vis.css');

const minBarWidth = 15;
const animationTime = 1000;

function nvd3Vis(slice) {
var chart;
Expand Down Expand Up @@ -40,6 +41,7 @@ function nvd3Vis(slice) {
var viz_type = fd.viz_type;
var f = d3.format('.3s');
var reduceXTicks = fd.reduce_x_ticks || false;
var stacked = false;

nv.addGraph(function () {
switch (viz_type) {
Expand Down Expand Up @@ -75,7 +77,14 @@ function nvd3Vis(slice) {
.showMaxMin(false)
.staggerLabels(true);

chart.stacked(fd.bar_stacked);
stacked = fd.bar_stacked;
chart.stacked(stacked);

if (fd.show_bar_value) {
setTimeout(function () {
addTotalBarValues(chart, payload.data, stacked);
}, animationTime);
}
break;

case 'dist_bar':
Expand All @@ -88,7 +97,14 @@ function nvd3Vis(slice) {
chart.xAxis
.showMaxMin(false);

chart.stacked(fd.bar_stacked);
stacked = fd.bar_stacked;
chart.stacked(stacked);

if (fd.show_bar_value) {
setTimeout(function () {
addTotalBarValues(chart, payload.data, stacked);
}, animationTime);
}
if (!reduceXTicks) {
width = barchartWidth();
}
Expand Down Expand Up @@ -265,6 +281,49 @@ function nvd3Vis(slice) {
}
};

var addTotalBarValues = function (chart, data, stacked) {
var svg = d3.select("svg"),
rectsToBeLabeled,
format = d3.format('.3s'),
countSeriesDisplayed = data.length;

var totalStackedValues = stacked && data.length !== 0 ?
data[0].values.map(function (bar, iBar) {
var bars = data.map(function (series) {
return series.values[iBar];
});
return d3.sum(bars, function (d) {
return d.y;
});
}) : [];

rectsToBeLabeled = svg.selectAll("g.nv-group").filter(
function (d, i) {
if (!stacked) {
return true;
}
return i === countSeriesDisplayed - 1;
}).selectAll("rect.positive");

var groupLabels = svg.select("g.nv-barsWrap").append("g");
rectsToBeLabeled.each(
function (d, index) {
var rectObj = d3.select(this);
var transformAttr = rectObj.attr("transform");
var yPos = parseFloat(rectObj.attr("y"));
var xPos = parseFloat(rectObj.attr("x"));
var rectWidth = parseFloat(rectObj.attr("width"));
var t = groupLabels.append("text")
.attr("x", xPos) // rough position first, fine tune later
.attr("y", yPos - 5)
.text(format(stacked ? totalStackedValues[index] : d.y))
.attr("transform", transformAttr)
.attr("class", "bar-chart-label");
var labelWidth = t.node().getBBox().width;
t.attr("x", xPos + rectWidth / 2 - labelWidth / 2); // fine tune
});
};

return {
render: render,
resize: update
Expand Down
5 changes: 5 additions & 0 deletions caravel/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ def __init__(self, viz):
"default": False,
"description": ""
}),
'show_bar_value': (BetterBooleanField, {
"label": _("Bar Values"),
"default": False,
"description": "Show the value on top of the bars or not"
}),
'show_controls': (BetterBooleanField, {
"label": _("Extra Controls"),
"default": False,
Expand Down
4 changes: 2 additions & 2 deletions caravel/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ class NVD3TimeSeriesBarViz(NVD3TimeSeriesViz):
fieldsets = [NVD3TimeSeriesViz.fieldsets[0]] + [{
'label': _('Chart Options'),
'fields': (
('show_brush', 'show_legend'),
('show_brush', 'show_legend', 'show_bar_value'),
('rich_tooltip', 'y_axis_zero'),
('y_log_scale', 'contribution'),
('x_axis_format', 'y_axis_format'),
Expand Down Expand Up @@ -1207,7 +1207,7 @@ class DistributionBarViz(DistributionPieViz):
'columns',
'metrics',
'row_limit',
('show_legend', 'bar_stacked'),
('show_legend', 'show_bar_value', 'bar_stacked'),
('y_axis_format', 'bottom_margin'),
('x_axis_label', 'y_axis_label'),
('reduce_x_ticks', 'contribution'),
Expand Down