Skip to content

Commit

Permalink
Closes #374: Truncating x-axis should be done to ticktext not x values.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina Samuel committed Apr 20, 2018
1 parent eb1b3cb commit c151935
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions client/app/visualizations/chart/plotly/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,11 @@ function preparePieData(seriesList, options) {
} = calculateDimensions(seriesList, options);

const colorPalette = ColorPaletteArray.slice();
const xAxisLabelLength = parseInt(options.xAxisLabelLength, 10) || DEFAULT_XAXIS_LABEL_LENGTH;
return map(seriesList, (serie, index) => {
const xPosition = (index % cellsInRow) * cellWidth;
const yPosition = Math.floor(index / cellsInRow) * cellHeight;
const labels = map(serie.data, (row, rowIdx) => {
const rowX = hasX ? row.x.substr(0, xAxisLabelLength) : `Slice ${index}`;
const rowX = hasX ? row.x : `Slice ${index}`;
const rowOpts = options.seriesOptions[rowX];
if (rowOpts) {
colorPalette[rowIdx] = rowOpts.color;
Expand Down Expand Up @@ -230,8 +229,7 @@ function prepareChartData(seriesList, options) {
const yValues = [];
const yErrorValues = [];
each(data, (row) => {
const xAxisLabelLength = parseInt(options.xAxisLabelLength, 10) || DEFAULT_XAXIS_LABEL_LENGTH;
const x = normalizeValue(row.x).substr(0, xAxisLabelLength);
const x = normalizeValue(row.x);
const y = normalizeValue(row.y);
const yError = normalizeValue(row.yError);
sourceData.set(x, {
Expand Down Expand Up @@ -380,6 +378,19 @@ 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 c151935

Please sign in to comment.