Skip to content

Commit

Permalink
When the label size is too short, the constant for calculating
Browse files Browse the repository at this point in the history
margin_size does not apply. Also nvd3 auto-adjust font-size of axis
labels.
Temporary solution here: Setting a fixed font-size on nvd3 axis labels
and a minimum threshold for label size.
  • Loading branch information
vera-liu committed Sep 15, 2016
1 parent e895807 commit 57c3efd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions caravel/assets/visualizations/nvd3_vis.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ text.nv-axislabel {

.dist_bar svg.nvd3-svg {
width: auto;
font-size: 14px;
}

.nv-x text{
font-size: 12px;
}

.bar .slice_container {
Expand Down
14 changes: 8 additions & 6 deletions caravel/assets/visualizations/nvd3_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ function nvd3Vis(slice) {

// Calculates the longest label size for stretching bottom margin
function calculateStretchMargins(payloadData) {
const axisLabels = payloadData.data[0].values;
let stretchMargin = 0;
const pixelsPerCharX = 4.5; // approx, depends on font size
let maxLabelSize = 0;
for (let i = 0; i < axisLabels.length; i++) {
maxLabelSize = Math.max(axisLabels[i].x.length, maxLabelSize);
}
stretchMargin = Math.ceil(Math.max(stretchMargin, pixelsPerCharX * maxLabelSize));
let maxLabelSize = 10; // accomodate for shorter labels
payloadData.data.forEach((d) => {
const axisLabels = d.values;
for (let i = 0; i < axisLabels.length; i++) {
maxLabelSize = Math.max(axisLabels[i].x.length, maxLabelSize);
}
});
stretchMargin = Math.ceil(pixelsPerCharX * maxLabelSize);
return stretchMargin;
}

Expand Down

0 comments on commit 57c3efd

Please sign in to comment.