Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a0d4d0d

Browse files
committed
improve auto ticks on axes with rangebreaks
1 parent ef14d60 commit a0d4d0d

19 files changed

+51
-9
lines changed

src/plots/cartesian/axes.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ axes.prepTicks = function(ax) {
536536
if(ax.tickmode === 'array') nt *= 100;
537537

538538

539-
ax._roughDTick = (Math.abs(rng[1] - rng[0]) - (ax._lBreaks || 0)) / nt;
539+
ax._roughDTick = Math.abs(rng[1] - rng[0]) / nt;
540540
axes.autoTicks(ax, ax._roughDTick);
541541

542542
// check for a forced minimum dtick
@@ -731,6 +731,25 @@ function arrayTicks(ax) {
731731
return ticksOut;
732732
}
733733

734+
function roundBaseDay(dayHours) {
735+
switch(dayHours) {
736+
case 4: return [1, 2];
737+
case 6: return [1, 2, 3];
738+
case 8: return [1, 2, 4];
739+
case 9: return [1, 3];
740+
case 10: return [1, 2, 5];
741+
case 12: return [1, 2, 3, 4, 6];
742+
case 14: return [1, 2, 7];
743+
case 15: return [1, 3, 5];
744+
case 16: return [1, 2, 4, 8];
745+
case 18: return [1, 2, 3, 6, 9];
746+
case 20: return [1, 2, 4, 5, 10];
747+
case 21: return [1, 3, 7];
748+
case 22: return [1, 2, 11];
749+
}
750+
return [1];
751+
}
752+
734753
var roundBase10 = [2, 5, 10];
735754
var roundBase24 = [1, 2, 3, 6, 12];
736755
var roundBase60 = [1, 2, 5, 10, 15, 30];
@@ -776,22 +795,34 @@ axes.autoTicks = function(ax, roughDTick) {
776795
// being > half of the final unit - so precalculate twice the rough val
777796
var roughX2 = 2 * roughDTick;
778797

798+
var oneDay = ONEDAY;
799+
var dayRatio = 1;
800+
if(ax._hasHourBreaks) {
801+
oneDay = ax._dayHours * ONEHOUR;
802+
dayRatio = Math.round(ax._dayHours / 24 * 7) / 7; // we use this in week context
803+
}
804+
779805
if(roughX2 > ONEAVGYEAR) {
780806
roughDTick /= ONEAVGYEAR;
781807
base = getBase(10);
782808
ax.dtick = 'M' + (12 * roundDTick(roughDTick, base, roundBase10));
783809
} else if(roughX2 > ONEAVGMONTH) {
784810
roughDTick /= ONEAVGMONTH;
785811
ax.dtick = 'M' + roundDTick(roughDTick, 1, roundBase24);
786-
} else if(roughX2 > ONEDAY) {
787-
ax.dtick = roundDTick(roughDTick, ONEDAY, ax._hasDayOfWeekBreaks ? [1, 7, 14] : roundDays);
788-
812+
} else if(roughX2 > oneDay) {
813+
ax.dtick = roundDTick(roughDTick, oneDay, ax._hasDayOfWeekBreaks ?
814+
[1, 2 * dayRatio, 7 * dayRatio, 14 * dayRatio] :
815+
roundDays
816+
);
789817
// get week ticks on sunday
790818
// this will also move the base tick off 2000-01-01 if dtick is
791819
// 2 or 3 days... but that's a weird enough case that we'll ignore it.
792820
ax.tick0 = Lib.dateTick0(ax.calendar, true);
793821
} else if(roughX2 > ONEHOUR) {
794-
ax.dtick = roundDTick(roughDTick, ONEHOUR, roundBase24);
822+
ax.dtick = roundDTick(roughDTick, ONEHOUR, ax._hasHourBreaks ?
823+
roundBaseDay(ax._dayHours) :
824+
roundBase24
825+
);
795826
} else if(roughX2 > ONEMIN) {
796827
ax.dtick = roundDTick(roughDTick, ONEMIN, roundBase60);
797828
} else if(roughX2 > ONESEC) {

src/plots/cartesian/axis_defaults.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,22 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
135135
if(!containerOut.rangebreaks.length) {
136136
delete containerOut.rangebreaks;
137137
} else {
138+
var n = 0;
138139
for(var k = 0; k < containerOut.rangebreaks.length; k++) {
139-
if(containerOut.rangebreaks[k].pattern === DAY_OF_WEEK) {
140+
var brk = containerOut.rangebreaks[k];
141+
if(brk.pattern === DAY_OF_WEEK) {
140142
containerOut._hasDayOfWeekBreaks = true;
141-
break;
143+
n++;
142144
}
145+
146+
if(brk.pattern === HOUR) {
147+
containerOut._hasHourBreaks = true;
148+
containerOut._dayHours = Math.round((brk.bounds[1] - brk.bounds[0] + 24) % 24);
149+
n++;
150+
}
151+
152+
// break when found all types
153+
if(n === 2) break;
143154
}
144155

145156
setConvert(containerOut, layoutOut);
-408 Bytes
Loading
-1.37 KB
Loading
-1.37 KB
Loading
-6.44 KB
Loading
-3.26 KB
Loading
-2.08 KB
Loading
-2.08 KB
Loading
-1.63 KB
Loading
-4.58 KB
Loading
Loading
-4.97 KB
Loading
Loading

test/image/baselines/axes_breaks.png

255 Bytes
Loading

test/jasmine/tests/axes_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4977,7 +4977,7 @@ describe('Test axes', function() {
49774977
.withContext(msg).toEqual(exp.tickVals);
49784978
}
49794979

4980-
it('should include requested ticks that fall within rangebreaks', function(done) {
4980+
it('should not include requested ticks that fall within rangebreaks', function(done) {
49814981
Plotly.plot(gd, [{
49824982
x: [
49834983
'1970-01-01 00:00:00.000',
@@ -5016,7 +5016,7 @@ describe('Test axes', function() {
50165016
})
50175017
.then(function() {
50185018
_assert('with two rangebreaks', {
5019-
tickVals: [0, 5, 10, 90, 95, 100, 190, 195, 200]
5019+
tickVals: [0, 89, 100, 200]
50205020
});
50215021
})
50225022
.catch(failTest)

0 commit comments

Comments
 (0)