Skip to content

Commit

Permalink
Customize tooltip with axis format (#2068)
Browse files Browse the repository at this point in the history
  • Loading branch information
vera-liu authored Jan 30, 2017
1 parent 544211f commit 87869a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
25 changes: 25 additions & 0 deletions superset/assets/javascripts/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,28 @@ export function getAjaxErrorMsg(error) {
return (respJSON && respJSON.message) ? respJSON.message :
error.responseText;
}

export function customizeToolTip(chart, xAxisFormatter, yAxisFormatters) {
chart.useInteractiveGuideline(true);
chart.interactiveLayer.tooltip.contentGenerator(function (d) {
const tooltipTitle = xAxisFormatter(d.value);
let tooltip = '';

tooltip += "<table><thead><tr><td colspan='3'>"
+ `<strong class='x-value'>${tooltipTitle}</strong>`
+ '</td></tr></thead><tbody>';

d.series.forEach((series, i) => {
const yAxisFormatter = yAxisFormatters[i];
const value = yAxisFormatter(series.value);
tooltip += "<tr><td class='legend-color-guide'>"
+ `<div style="background-color: ${series.color};"></div></td>`
+ `<td class='key'>${series.key}</td>`
+ `<td class='value'>${value}</td></tr>`;
});

tooltip += '</tbody></table>';

return tooltip;
});
}
9 changes: 7 additions & 2 deletions superset/assets/visualizations/nvd3_vis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// JS
import { category21 } from '../javascripts/modules/colors';
import { timeFormatFactory, formatDate } from '../javascripts/modules/dates';
import { customizeToolTip } from '../javascripts/modules/utils';

const d3 = require('d3');
const nv = require('nvd3');

Expand Down Expand Up @@ -345,8 +347,11 @@ function nvd3Vis(slice, payload) {
svg = d3.select(slice.selector).append('svg');
}
if (vizType === 'dual_line') {
chart.yAxis1.tickFormat(d3.format(fd.y_axis_format));
chart.yAxis2.tickFormat(d3.format(fd.y_axis_2_format));
const yAxisFormatter1 = d3.format(fd.y_axis_format);
const yAxisFormatter2 = d3.format(fd.y_axis_2_format);
chart.yAxis1.tickFormat(yAxisFormatter1);
chart.yAxis2.tickFormat(yAxisFormatter2);
customizeToolTip(chart, xAxisFormatter, [yAxisFormatter1, yAxisFormatter2]);
chart.showLegend(true);
chart.margin({ right: 50 });
}
Expand Down

0 comments on commit 87869a2

Please sign in to comment.