Skip to content

Commit

Permalink
Merge pull request #6829 from geopozo/pikul-arrayTicks-duplicate-tick…
Browse files Browse the repository at this point in the history
…-bug

Add switch to specify major/minor in `axes.arrayTicks()`
  • Loading branch information
archmoj committed Jan 4, 2024
2 parents f6c33bd + 1051bb7 commit 9e9da55
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
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
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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]);
Expand All @@ -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;


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

0 comments on commit 9e9da55

Please sign in to comment.