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

Feature: Display the verbose name for metrics within Timeseries charts and legend. #3504

Merged
merged 1 commit into from
Sep 26, 2017
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
45 changes: 36 additions & 9 deletions superset/assets/visualizations/nvd3_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,41 @@ function getMaxLabelSize(container, axisClass) {
return Math.max(...labelDimensions);
}

/* eslint-disable camelcase */
function formatLabel(column, verbose_map) {
let label;
if (verbose_map) {
if (Array.isArray(column) && column.length) {
label = verbose_map[column[0]];
if (column.length > 1) {
label += `, ${column.slice(1).join(', ')}`;
}
} else {
label = verbose_map[column];
}
}
return label || column;
}
/* eslint-enable camelcase */

function nvd3Vis(slice, payload) {
let chart;
let colorKey = 'key';
const isExplore = $('#explore-container').length === 1;

let data;
if (payload.data) {
data = payload.data.map(x => ({
...x, key: formatLabel(x.key, slice.datasource.verbose_map),
}));
} else {
data = [];
}

slice.container.html('');
slice.clearError();


// Calculates the longest label size for stretching bottom margin
function calculateStretchMargins(payloadData) {
let stretchMargin = 0;
Expand All @@ -102,9 +129,9 @@ function nvd3Vis(slice, payload) {
const barchartWidth = function () {
let bars;
if (fd.bar_stacked) {
bars = d3.max(payload.data, function (d) { return d.values.length; });
bars = d3.max(data, function (d) { return d.values.length; });
} else {
bars = d3.sum(payload.data, function (d) { return d.values.length; });
bars = d3.sum(data, function (d) { return d.values.length; });
}
if (bars * minBarWidth > width) {
return bars * minBarWidth;
Expand Down Expand Up @@ -162,7 +189,7 @@ function nvd3Vis(slice, payload) {

if (fd.show_bar_value) {
setTimeout(function () {
addTotalBarValues(svg, chart, payload.data, stacked, fd.y_axis_format);
addTotalBarValues(svg, chart, data, stacked, fd.y_axis_format);
}, animationTime);
}
break;
Expand All @@ -179,13 +206,13 @@ function nvd3Vis(slice, payload) {
stacked = fd.bar_stacked;
chart.stacked(stacked);
if (fd.order_bars) {
payload.data.forEach((d) => {
data.forEach((d) => {
d.values.sort((a, b) => tryNumify(a.x) < tryNumify(b.x) ? -1 : 1);
});
}
if (fd.show_bar_value) {
setTimeout(function () {
addTotalBarValues(svg, chart, payload.data, stacked, fd.y_axis_format);
addTotalBarValues(svg, chart, data, stacked, fd.y_axis_format);
}, animationTime);
}
if (!reduceXTicks) {
Expand All @@ -208,7 +235,7 @@ function nvd3Vis(slice, payload) {

if (fd.pie_label_type === 'percent') {
let total = 0;
payload.data.forEach((d) => { total += d.y; });
data.forEach((d) => { total += d.y; });
chart.tooltip.valueFormatter(d => `${((d / total) * 100).toFixed()}%`);
}

Expand Down Expand Up @@ -248,7 +275,7 @@ function nvd3Vis(slice, payload) {
return s;
});
chart.pointRange([5, fd.max_bubble_size ** 2]);
chart.pointDomain([0, d3.max(payload.data, d => d3.max(d.values, v => v.size))]);
chart.pointDomain([0, d3.max(data, d => d3.max(d.values, v => v.size))]);
break;

case 'area':
Expand Down Expand Up @@ -395,7 +422,7 @@ function nvd3Vis(slice, payload) {
chart.showLegend(width > BREAKPOINTS.small);
}
svg
.datum(payload.data)
.datum(data)
.transition().duration(500)
.attr('height', height)
.attr('width', width)
Expand Down Expand Up @@ -471,7 +498,7 @@ function nvd3Vis(slice, payload) {

// render chart
svg
.datum(payload.data)
.datum(data)
.transition().duration(500)
.attr('height', height)
.attr('width', width)
Expand Down
13 changes: 4 additions & 9 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,16 +840,11 @@ def to_series(self, df, classed='', title_suffix=''):
ys = series[name]
if df[name].dtype.kind not in "biufc":
continue
if isinstance(name, string_types):
series_title = name
else:
name = ["{}".format(s) for s in name]
if len(self.form_data.get('metrics')) > 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users reported an issue related to removing this logic. In the case where there's a single metric and a one or many dimension, the metric name would not be reported prior to this change, which is the desired behavior. Where before using metric count with gender dimension would give ['female', 'male', 'other'] in both the legend and tooltip, now it says ['count, female', 'count, male', 'count, other']

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't report this to me if you don't want me to fix it. Just wasted 30min on context switching and pushing a branch only to find your fix...

series_title = ", ".join(name)
else:
series_title = ", ".join(name[1:])
if title_suffix:
series_title = name
if isinstance(series_title, string_types):
series_title += title_suffix
elif title_suffix and isinstance(series_title, list):
series_title.append(title_suffix)

d = {
"key": series_title,
Expand Down