Skip to content

Commit

Permalink
allow x-axis label truncation (re #249)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Short authored and jezdez committed Sep 6, 2018
1 parent 6362537 commit f2d5cd2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
6 changes: 6 additions & 0 deletions client/app/visualizations/chart/chart-editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@
<i class="input-helper"></i> Show Labels
</label>
</div>

<div class="form-group">
<label class="control-label">Label Length</label>
<input name="x-axis-label-length" type="number" class="form-control" ng-model="options.xAxisLabelLength">
<span class="info">How many characters should X Axis Labels be truncated at in the legend?</span>
</div>
</div>

<div ng-if="currentTab == 'yAxis'" class="m-t-10 m-b-10">
Expand Down
13 changes: 13 additions & 0 deletions client/app/visualizations/chart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,19 @@ function ChartEditor(ColorPalette, clientConfig) {
scope.options.legend = { enabled: true };
}

scope.$watch('options.globalSeriesType', (newType, oldType) => {
const defaultXAxisLength = 10;
if (!has(scope.options, 'xAxisLabelLength')) {
scope.options.xAxisLabelLength = defaultXAxisLength;
}
if (oldType !== newType) {
scope.options.xAxisLabelLength = defaultXAxisLength;
if (newType === 'pie') {
scope.options.xAxisLabelLength = 300;
}
}
}, true);

if (scope.columnNames) {
each(scope.options.columnMapping, (value, key) => {
if (scope.columnNames.length > 0 && !includes(scope.columnNames, key)) {
Expand Down
25 changes: 23 additions & 2 deletions client/app/visualizations/chart/plotly/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ function getUnifiedXAxisValues(seriesList, sorted) {
return sorted ? sortBy(result, identity) : result;
}

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 Down Expand Up @@ -260,9 +273,17 @@ function preparePieData(seriesList, options) {
});
});

const colorPalette = ColorPaletteArray.slice();
return {
values: map(serie.data, i => i.y),
labels: map(serie.data, row => (hasX ? normalizeValue(row.x) : `Slice ${index}`)),
labels: map(serie.data, (row, rowIdx) => {
const rowX = hasX ? truncateCategoryAxis(normalizeValue(row.x), options) : `Slice ${index}`;
const rowOpts = options.seriesOptions[rowX];
if (rowOpts) {
colorPalette[rowIdx] = rowOpts.color;
}
return rowX;
}),
type: 'pie',
hole: 0.4,
marker: {
Expand Down Expand Up @@ -317,7 +338,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);
const size = normalizeValue(row.size);
Expand Down

0 comments on commit f2d5cd2

Please sign in to comment.