Skip to content

Commit 7d92747

Browse files
authored
Merge pull request #8160 from davrens101/fix/tickvals-prefix-suffix
Fix tick prefixes and suffixes with explicit tick values
2 parents bb3fedf + 636ea7d commit 7d92747

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎draftlogs/8160_fix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Apply `tickprefix` and `ticksuffix` to `tickvals` without explicit `ticktext` [[#8160](https://github.com/plotly/plotly.js/pull/8160)], with thanks to @davrens101 for the contribution!

‎src/plots/cartesian/axes.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ function arrayTicks(ax, majorOnly) {
15021502
for(var i = 0; i < vals.length; i++) {
15031503
var vali = tickVal2l(vals[i]);
15041504
if(vali > tickMin && vali < tickMax) {
1505-
var obj = axes.tickText(ax, vali, false, String(text[i]), {
1505+
const obj = axes.tickText(ax, vali, false, text[i] !== undefined, {
15061506
first: i === firstIdx,
15071507
last: i === lastIdx,
15081508
});

‎test/jasmine/tests/axes_test.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,6 +3986,40 @@ describe('Test axes', function() {
39863986
});
39873987
});
39883988

3989+
it('applies prefixes and suffixes to array ticks without explicit text', () => {
3990+
['linear', 'log', 'date', 'category'].forEach(axType => {
3991+
const vals = axType === 'date' ?
3992+
['2020-01-01', '2020-01-02', '2020-01-03'] :
3993+
axType === 'category' ? ['a', 'b', 'c'] : [1, 2, 3];
3994+
const ax = {
3995+
type: axType,
3996+
tickmode: 'array',
3997+
tickvals: vals,
3998+
range: axType === 'log' ? [0, 1] :
3999+
axType === 'category' ? [-0.5, 2.5] : [vals[0], vals[2]],
4000+
tickprefix: '<',
4001+
ticksuffix: '>',
4002+
showtickprefix: 'all',
4003+
showticksuffix: 'all',
4004+
nticks: 10
4005+
};
4006+
if(axType === 'category') {
4007+
ax._categories = vals;
4008+
ax._categoriesMap = {a: 0, b: 1, c: 2};
4009+
}
4010+
const bare = mockCalc({...ax, tickprefix: '', ticksuffix: ''});
4011+
expect(mockCalc(ax)).toEqual(bare.map(text => `<${text}>`), axType);
4012+
ax.ticktext = ['custom'];
4013+
expect(mockCalc(ax)).toEqual(['custom', `<${bare[1]}>`, `<${bare[2]}>`], axType);
4014+
ax.showtickprefix = 'first';
4015+
ax.showticksuffix = 'last';
4016+
delete ax.ticktext;
4017+
expect(mockCalc(ax)).toEqual([`<${bare[0]}`, bare[1], `${bare[2]}>`], axType);
4018+
ax.showtickprefix = ax.showticksuffix = 'none';
4019+
expect(mockCalc(ax)).toEqual(bare, axType);
4020+
});
4021+
});
4022+
39894023
it('should handle tickvals edge cases with category axes', function() {
39904024
var ax = {
39914025
type: 'category',

0 commit comments

Comments
 (0)