diff --git a/draftlogs/6829_fix.md b/draftlogs/6829_fix.md new file mode 100644 index 00000000000..abba571afa7 --- /dev/null +++ b/draftlogs/6829_fix.md @@ -0,0 +1 @@ +- Add argument to `arrayTicks()` to render major OR minor [[#6829](https://github.com/plotly/plotly.js/pull/6829)] diff --git a/src/plots/cartesian/axes.js b/src/plots/cartesian/axes.js index dfdb0e5166d..8368e1c5398 100644 --- a/src/plots/cartesian/axes.js +++ b/src/plots/cartesian/axes.js @@ -949,10 +949,10 @@ axes.calcTicks = function calcTicks(ax, opts) { if(mockAx.tickmode === 'array') { if(major) { tickVals = []; - ticksOut = arrayTicks(ax); + ticksOut = arrayTicks(ax, !isMinor); } else { minorTickVals = []; - minorTicks = arrayTicks(ax); + minorTicks = arrayTicks(ax, !isMinor); } continue; } @@ -1261,7 +1261,7 @@ function syncTicks(ax) { return ticksOut; } -function arrayTicks(ax) { +function arrayTicks(ax, majorOnly) { var rng = Lib.simpleMap(ax.range, ax.r2l); var exRng = expandRange(rng); var tickMin = Math.min(exRng[0], exRng[1]); @@ -1279,10 +1279,10 @@ function arrayTicks(ax) { var ticksOut = []; for(var isMinor = 0; isMinor <= 1; isMinor++) { + if((majorOnly !== undefined) && ((majorOnly && isMinor) || (majorOnly === false && !isMinor))) continue; if(isMinor && !ax.minor) continue; var vals = !isMinor ? ax.tickvals : ax.minor.tickvals; var text = !isMinor ? ax.ticktext : []; - if(!vals) continue; diff --git a/test/jasmine/tests/axes_test.js b/test/jasmine/tests/axes_test.js index d128072a307..9f1dff70617 100644 --- a/test/jasmine/tests/axes_test.js +++ b/test/jasmine/tests/axes_test.js @@ -28,7 +28,6 @@ var supplyDefaults = require('../assets/supply_defaults'); describe('Test axes', function() { 'use strict'; - describe('swap', function() { it('should swap most attributes and fix placeholder titles', function() { var gd = { @@ -8217,3 +8216,70 @@ describe('shift tests', function() { }); }); }); +describe('test tickmode calculator', function() { + var gd; + + beforeEach(function() { + gd = createGraphDiv(); + }); + + afterEach(destroyGraphDiv); + + function generateTickConfig() { + var standardConfig = {tickmode: 'array', ticks: 'inside', ticklen: 1, showticklabels: false}; + + // Number of ticks will be random + Lib.seedPseudoRandom(); + var n = (Lib.pseudoRandom() * 99) + 1; + var tickVals = []; + for(var i = 0; i <= n; i++) { + tickVals.push(i); + } + standardConfig.tickvals = tickVals; + standardConfig.ticktext = tickVals; + return standardConfig; + } + var ticksOff = {tickmode: 'array', ticks: '', tickvals: [], ticktext: [], ticklen: 0, showticklabels: false}; + + function _assert(expLength) { + var ax = gd._fullLayout.xaxis; + + // all positions + var positions = + ax._vals + .filter(function(d) { return d; }) + .map(function(d) { return d.x; }); + + expect(positions.length).toEqual(expLength); + } + + describe('arrayTicks', function() { + it('should return the specified correct number of major ticks and minor ticks', function(done) { + var xMajorConfig = ticksOff; + var xMinorConfig = ticksOff; + xMajorConfig = generateTickConfig(); + xMinorConfig = generateTickConfig(); + xMajorConfig.range = [0, 1000]; + xMajorConfig.minor = xMinorConfig; + Plotly.newPlot(gd, { + data: [{ + x: [0, 1], + y: [0, 1] + }], + layout: { + width: 400, + height: 400, + margin: { + t: 40, + b: 40, + l: 40, + r: 40 + }, + xaxis: xMajorConfig, + } + }).then(function() { + _assert(xMajorConfig.tickvals.length + xMinorConfig.tickvals.length); + }).then(done, done.fail); + }); + }); +});