Skip to content

Commit

Permalink
Closes getredash#387: Only truncate 'category' type labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina Samuel committed May 25, 2018
1 parent b048617 commit f6baa7a
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions client/app/visualizations/chart/plotly/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ function getUnifiedXAxisValues(seriesList, sorted) {

const DEFAULT_XAXIS_LABEL_LENGTH = 300;

// We only truncate category x-axis labels because the other types
// are correctly formatted by Plotly.
function truncateCategoryAxis(oldXLabel, options) {
const xAxisLabelLength = parseInt(options.xAxisLabelLength, 10) || DEFAULT_XAXIS_LABEL_LENGTH;

if (options && options.xAxis && options.xAxis.type === 'category') {
return String(oldXLabel).substr(0, xAxisLabelLength);
}
return oldXLabel;
}

function preparePieData(seriesList, options) {
const {
cellWidth, cellHeight, xPadding, yPadding, cellsInRow, hasX,
Expand All @@ -189,7 +200,7 @@ function preparePieData(seriesList, options) {
const xPosition = (index % cellsInRow) * cellWidth;
const yPosition = Math.floor(index / cellsInRow) * cellHeight;
const labels = map(serie.data, (row, rowIdx) => {
const rowX = hasX ? row.x : `Slice ${index}`;
const rowX = hasX ? truncateCategoryAxis(row.x, options) : `Slice ${index}`;
const rowOpts = options.seriesOptions[rowX];
if (rowOpts) {
colorPalette[rowIdx] = rowOpts.color;
Expand Down Expand Up @@ -231,7 +242,7 @@ function prepareChartData(seriesList, options) {
const yValues = [];
const yErrorValues = [];
each(data, (row) => {
const x = normalizeValue(row.x);
const x = truncateCategoryAxis(normalizeValue(row.x), options);
const y = normalizeValue(row.y);
const yError = normalizeValue(row.yError);
sourceData.set(x, {
Expand Down Expand Up @@ -388,19 +399,6 @@ export function prepareLayout(element, seriesList, options, data) {
}
}

// Truncate x-axis labels using ticktext.
// Example can be found here: https://plot.ly/javascript/axes/#enumerated-ticks-with-tickvals-and-ticktext
let ticktext, tickvals;
const xAxisLabelLength = parseInt(options.xAxisLabelLength, 10) || DEFAULT_XAXIS_LABEL_LENGTH;
if (data.length > 0) {
ticktext = map(data[0].x, xVal => String(xVal).substr(0, xAxisLabelLength));
tickvals = data[0].x;
}
if (ticktext && tickvals) {
result.xaxis.ticktext = ticktext;
result.xaxis.tickvals = tickvals;
}

return result;
}

Expand Down

0 comments on commit f6baa7a

Please sign in to comment.