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

[vis] fix line chart when slice is really narrow #2620

Merged
merged 2 commits into from
Apr 18, 2017
Merged
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
25 changes: 16 additions & 9 deletions superset/assets/visualizations/nvd3_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const timeStampFormats = TIME_STAMP_OPTIONS.map(opt => opt[0]);
const minBarWidth = 15;
const animationTime = 1000;

const BREAKPOINTS = {
small: 340,
};

const addTotalBarValues = function (chart, data, stacked) {
const svg = d3.select('svg');
const format = d3.format('.3s');
Expand Down Expand Up @@ -65,13 +69,12 @@ function hideTooltips() {
$('.nvtooltip').css({ opacity: 0 });
}

function getMaxLabelSize(container, axisClass, widthOrHeight) {
function getMaxLabelSize(container, axisClass) {
// axis class = .nv-y2 // second y axis on dual line chart
// axis class = .nv-x // x axis on time series line chart
const labelEls = container.find(`.${axisClass} .tick text`);
const labelDimensions = labelEls.map(i => labelEls[i].getBoundingClientRect()[widthOrHeight]);
const max = Math.max(...labelDimensions);
return max;
const labelEls = container.find(`.${axisClass} text`);
const labelDimensions = labelEls.map(i => labelEls[i].getComputedTextLength());
return Math.max(...labelDimensions);
}

function nvd3Vis(slice, payload) {
Expand Down Expand Up @@ -286,7 +289,11 @@ function nvd3Vis(slice, payload) {
}

if ('showLegend' in chart && typeof fd.show_legend !== 'undefined') {
chart.showLegend(fd.show_legend);
if (width < BREAKPOINTS.small && vizType !== 'pie') {
chart.showLegend(false);
} else {
chart.showLegend(fd.show_legend);
}
}

let height = slice.height() - 15;
Expand Down Expand Up @@ -385,7 +392,7 @@ function nvd3Vis(slice, payload) {
chart.yAxis1.tickFormat(yAxisFormatter1);
chart.yAxis2.tickFormat(yAxisFormatter2);
customizeToolTip(chart, xAxisFormatter, [yAxisFormatter1, yAxisFormatter2]);
chart.showLegend(true);
chart.showLegend(width > BREAKPOINTS.small);
}
svg
.datum(payload.data)
Expand All @@ -406,15 +413,15 @@ function nvd3Vis(slice, payload) {
// ---- (x axis labels are rotated 45 degrees so we use height),
// - adjust margins based on these measures and render again
if (isTimeSeries && vizType !== 'bar') {
const maxXAxisLabelHeight = getMaxLabelSize(slice.container, 'nv-x', 'height');
const maxXAxisLabelHeight = getMaxLabelSize(slice.container, 'nv-x');
const marginPad = isExplore ? width * 0.01 : width * 0.03;
const chartMargins = {
bottom: maxXAxisLabelHeight + marginPad,
right: maxXAxisLabelHeight + marginPad,
};

if (vizType === 'dual_line') {
const maxYAxis2LabelWidth = getMaxLabelSize(slice.container, 'nv-y2', 'width');
const maxYAxis2LabelWidth = getMaxLabelSize(slice.container, 'nv-y2');
// use y axis width if it's wider than axis width/height
if (maxYAxis2LabelWidth > maxXAxisLabelHeight) {
chartMargins.right = maxYAxis2LabelWidth + marginPad;
Expand Down