Skip to content

Commit d7e6cba

Browse files
committed
Keep auto textposition in cleanData only for trace types that accept it
1 parent d8d7d59 commit d7e6cba

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

‎src/plot_api/helpers.js‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,12 @@ exports.cleanData = function (data) {
205205

206206
if (!traceIs(trace, 'pie-like') && !traceIs(trace, 'bar-like')) {
207207
if (Array.isArray(trace.textposition)) {
208+
const keepAuto = acceptsAutoTextPosition(trace);
208209
for (i = 0; i < trace.textposition.length; i++) {
209-
trace.textposition[i] = cleanTextPosition(trace.textposition[i]);
210+
trace.textposition[i] = cleanTextPosition(trace.textposition[i], keepAuto);
210211
}
211212
} else if (trace.textposition) {
212-
trace.textposition = cleanTextPosition(trace.textposition);
213+
trace.textposition = cleanTextPosition(trace.textposition, acceptsAutoTextPosition(trace));
213214
}
214215
}
215216

@@ -321,10 +322,16 @@ function commonPrefix(name1, name2, show1, show2) {
321322
return out.trim();
322323
}
323324

325+
// Only some trace types accept *auto*, so the others clean it like any other incorrect value
326+
function acceptsAutoTextPosition(trace) {
327+
const values = Registry.getModule(trace.type || 'scatter')?.attributes?.textposition?.values;
328+
return Array.isArray(values) && values.includes('auto');
329+
}
330+
324331
// textposition - support partial attributes (ie just 'top')
325332
// and incorrect use of middle / center etc.
326-
function cleanTextPosition(textposition) {
327-
if (textposition === 'auto') return textposition;
333+
function cleanTextPosition(textposition, keepAuto) {
334+
if (keepAuto && textposition === 'auto') return textposition;
328335

329336
var posY = 'middle';
330337
var posX = 'center';

‎test/jasmine/tests/plot_api_test.js‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,20 +2569,19 @@ describe('Test plot api', function () {
25692569
expect(gd.data[0].scl).toBe(undefined);
25702570
});
25712571

2572-
it('should keep *auto* textposition on scatter traces', function () {
2573-
var data = [
2574-
{
2575-
type: 'scatter',
2576-
mode: 'text',
2577-
x: [1],
2578-
y: [1],
2579-
text: ['a'],
2580-
textposition: 'auto'
2581-
}
2572+
it('should keep *auto* textposition only on trace types that accept it', () => {
2573+
const data = [
2574+
{ type: 'scatter', mode: 'text', x: [1], y: [1], text: ['a'], textposition: 'auto' },
2575+
{ type: 'scatter', mode: 'text', x: [1], y: [1], text: ['a'], textposition: ['auto', 'top'] },
2576+
{ type: 'scattergl', mode: 'text', x: [1], y: [1], text: ['a'], textposition: 'auto' },
2577+
{ type: 'scattergl', mode: 'text', x: [1], y: [1], text: ['a'], textposition: ['auto', 'top'] }
25822578
];
25832579

2584-
Plotly.newPlot(gd, data);
2585-
expect(gd.data[0].textposition).toBe('auto');
2580+
helpers.cleanData(data);
2581+
expect(data[0].textposition).toBe('auto');
2582+
expect(data[1].textposition).toEqual(['auto', 'top center']);
2583+
expect(data[2].textposition).toBe('middle center');
2584+
expect(data[3].textposition).toEqual(['middle center', 'top center']);
25862585
});
25872586

25882587
it("should not delete rename 'scl' to 'colorscale' when colorscale is defined ", function () {

0 commit comments

Comments
 (0)