Skip to content

Commit 82677ac

Browse files
committed
tweak and test cleanData for finance traces
removing direction names & showlegend, and picking a composite name
1 parent 0d80a21 commit 82677ac

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

src/plot_api/helpers.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,21 @@ exports.cleanData = function(data, existingData) {
332332

333333
// fixes from converting finance from transforms to real trace types
334334
if(trace.type === 'candlestick' || trace.type === 'ohlc') {
335+
var increasingShowlegend = (trace.increasing || {}).showlegend !== false;
336+
var decreasingShowlegend = (trace.decreasing || {}).showlegend !== false;
335337
var increasingName = cleanFinanceDir(trace.increasing);
336338
var decreasingName = cleanFinanceDir(trace.decreasing);
337339

338340
// now figure out something smart to do with the separate direction
339341
// names we removed
340-
if(increasingName && decreasingName) {
342+
if((increasingName !== false) && (decreasingName !== false)) {
341343
// both sub-names existed: base name previously had no effect
342344
// so ignore it and try to find a shared part of the sub-names
343-
var newName = commonPrefix(increasingName, decreasingName);
345+
346+
var newName = commonPrefix(
347+
increasingName, decreasingName,
348+
increasingShowlegend, decreasingShowlegend
349+
);
344350
// if no common part, leave whatever name was (or wasn't) there
345351
if(newName) trace.name = newName;
346352
}
@@ -409,17 +415,24 @@ exports.cleanData = function(data, existingData) {
409415
};
410416

411417
function cleanFinanceDir(dirContainer) {
412-
if(!Lib.isPlainObject(dirContainer)) return '';
418+
if(!Lib.isPlainObject(dirContainer)) return false;
413419

414-
var dirName = (dirContainer.showlegend === false) ? '' : dirContainer.name;
420+
var dirName = dirContainer.name;
415421

416422
delete dirContainer.name;
417423
delete dirContainer.showlegend;
418424

419-
return dirName;
425+
return (typeof dirName === 'string' || typeof dirName === 'number') && String(dirName);
420426
}
421427

422-
function commonPrefix(name1, name2) {
428+
function commonPrefix(name1, name2, show1, show2) {
429+
// if only one is shown in the legend, use that
430+
if(show1 && !show2) return name1;
431+
if(show2 && !show1) return name2;
432+
433+
// if both or neither are in the legend, check if one is blank (or whitespace)
434+
// and use the other one
435+
// note that hover labels can still use the name even if the legend doesn't
423436
if(!name1.trim()) return name2;
424437
if(!name2.trim()) return name1;
425438

test/jasmine/tests/plot_api_test.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,6 +2276,90 @@ describe('Test plot api', function() {
22762276
expect(gd.layout.shapes[2].yref).toEqual('y');
22772277

22782278
});
2279+
2280+
it('removes direction names and showlegend from finance traces', function() {
2281+
var data = [{
2282+
type: 'ohlc', open: [1], high: [3], low: [0], close: [2],
2283+
increasing: {
2284+
showlegend: true,
2285+
name: 'Yeti goes up'
2286+
},
2287+
decreasing: {
2288+
showlegend: 'legendonly',
2289+
name: 'Yeti goes down'
2290+
},
2291+
name: 'Snowman'
2292+
}, {
2293+
type: 'candlestick', open: [1], high: [3], low: [0], close: [2],
2294+
increasing: {
2295+
name: 'Bigfoot'
2296+
},
2297+
decreasing: {
2298+
showlegend: false,
2299+
name: 'Biggerfoot'
2300+
},
2301+
name: 'Nobody'
2302+
}, {
2303+
type: 'ohlc', open: [1], high: [3], low: [0], close: [2],
2304+
increasing: {
2305+
name: 'Batman'
2306+
},
2307+
decreasing: {
2308+
showlegend: true
2309+
},
2310+
name: 'Robin'
2311+
}, {
2312+
type: 'candlestick', open: [1], high: [3], low: [0], close: [2],
2313+
increasing: {
2314+
showlegend: false,
2315+
},
2316+
decreasing: {
2317+
name: 'Fred'
2318+
}
2319+
}, {
2320+
type: 'ohlc', open: [1], high: [3], low: [0], close: [2],
2321+
increasing: {
2322+
showlegend: false,
2323+
name: 'Gruyere heating up'
2324+
},
2325+
decreasing: {
2326+
showlegend: false,
2327+
name: 'Gruyere cooling off'
2328+
},
2329+
name: 'Emmenthaler'
2330+
}];
2331+
2332+
Plotly.plot(gd, data);
2333+
2334+
// Even if both showlegends are false, leave trace.showlegend out
2335+
// My rationale for this is that legends are sufficiently different
2336+
// now that it's worthwhile resetting their existence to default
2337+
gd.data.forEach(function(trace) {
2338+
expect(trace.increasing.name).toBeUndefined();
2339+
expect(trace.increasing.showlegend).toBeUndefined();
2340+
expect(trace.decreasing.name).toBeUndefined();
2341+
expect(trace.decreasing.showlegend).toBeUndefined();
2342+
});
2343+
2344+
// Both directions have names: ignore trace.name, as it
2345+
// had no effect on the output previously
2346+
// Ideally 'Yeti goes' would be smart enough to truncate
2347+
// at 'Yeti' but I don't see how to do that...
2348+
expect(gd.data[0].name).toBe('Yeti goes');
2349+
// One direction has empty or hidden name so use the other
2350+
// Note that even '' in both names would render trace.name impact-less
2351+
expect(gd.data[1].name).toBe('Bigfoot');
2352+
2353+
// One direction has a name but trace.name is there too:
2354+
// just use trace.name
2355+
expect(gd.data[2].name).toBe('Robin');
2356+
2357+
// No trace.name, only one direction name: use the direction name
2358+
expect(gd.data[3].name).toBe('Fred');
2359+
2360+
// both names exist but hidden from the legend: still look for common prefix
2361+
expect(gd.data[4].name).toBe('Gruyere');
2362+
});
22792363
});
22802364

22812365
describe('Plotly.newPlot', function() {

0 commit comments

Comments
 (0)