Skip to content

Add switch to specify major/minor in axes.arrayTicks() #6829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 4, 2024
1 change: 1 addition & 0 deletions draftlogs/6829_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add argument to `arrayTicks()` to render major OR minor [[#6829](https://github.com/plotly/plotly.js/pull/6829)]
8 changes: 4 additions & 4 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
@@ -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;


68 changes: 67 additions & 1 deletion test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});