Skip to content

Commit 6de9589

Browse files
committed
add ticklabelperiod to date axes to move labels to the middle of period
1 parent aaac215 commit 6de9589

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

src/components/colorbar/draw.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ function mockColorBarAxis(gd, opts, zrange) {
706706
font: fullLayout.font,
707707
noHover: true,
708708
noTickson: true,
709+
noTicklabelmode: true,
709710
calendar: fullLayout.calendar // not really necessary (yet?)
710711
};
711712

src/plots/cartesian/axes.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,13 @@ axes.calcTicks = function calcTicks(ax, opts) {
692692
false, // hover
693693
_minor // noSuffixPrefix
694694
);
695+
696+
if(ax.ticklabelmode) {
697+
ticksOut[i].periodX = i ? (
698+
tickVals[i].value +
699+
tickVals[i - 1].value
700+
) / 2 : false;
701+
}
695702
}
696703

697704
ax._inCalcTicks = false;
@@ -1785,6 +1792,10 @@ axes.drawOne = function(gd, ax, opts) {
17851792
if(!ax.visible) return;
17861793

17871794
var transFn = axes.makeTransFn(ax);
1795+
var transTickLabelFn = ax.ticklabelmode ?
1796+
axes.makeTransPeriodFn(ax) :
1797+
axes.makeTransFn(ax);
1798+
17881799
var tickVals;
17891800
// We remove zero lines, grid lines, and inside ticks if they're within 1px of the end
17901801
// The key case here is removing zero lines when the axis bound is zero
@@ -1903,7 +1914,7 @@ axes.drawOne = function(gd, ax, opts) {
19031914
return axes.drawLabels(gd, ax, {
19041915
vals: vals,
19051916
layer: mainAxLayer,
1906-
transFn: transFn,
1917+
transFn: transTickLabelFn,
19071918
labelFns: axes.makeLabelFns(ax, mainLinePosition)
19081919
});
19091920
});
@@ -2213,6 +2224,14 @@ axes.makeTransFn = function(ax) {
22132224
function(d) { return 'translate(0,' + (offset + ax.l2p(d.x)) + ')'; };
22142225
};
22152226

2227+
axes.makeTransPeriodFn = function(ax) {
2228+
var axLetter = ax._id.charAt(0);
2229+
var offset = ax._offset;
2230+
return axLetter === 'x' ?
2231+
function(d) { return 'translate(' + (offset + ax.l2p(d.periodX)) + ',0)'; } :
2232+
function(d) { return 'translate(0,' + (offset + ax.l2p(d.periodX)) + ')'; };
2233+
};
2234+
22162235
/**
22172236
* Make axis tick path string
22182237
*
@@ -2511,6 +2530,14 @@ axes.drawLabels = function(gd, ax, opts) {
25112530
var axLetter = axId.charAt(0);
25122531
var cls = opts.cls || axId + 'tick';
25132532
var vals = opts.vals;
2533+
if(
2534+
ax.showticklabels &&
2535+
ax.ticklabelmode // === 'period'
2536+
) {
2537+
vals = vals.slice();
2538+
vals.shift();
2539+
}
2540+
25142541
var labelFns = opts.labelFns;
25152542
var tickAngle = opts.secondary ? 0 : ax.tickangle;
25162543
var prevAngle = (ax._prevTickAngles || {})[cls];

src/plots/cartesian/axis_defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
126126
}
127127

128128
if(axType === 'date') {
129+
if(!options.noTicklabelmode) coerce('ticklabelmode');
130+
129131
handleArrayContainerDefaults(containerIn, containerOut, {
130132
name: 'rangebreaks',
131133
inclusionAttr: 'enabled',

src/plots/cartesian/layout_attributes.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,20 @@ module.exports = {
478478
'to the left/bottom of labels.'
479479
].join(' ')
480480
},
481+
ticklabelmode: {
482+
valType: 'enumerated',
483+
values: ['', 'period'],
484+
dflt: '',
485+
role: 'info',
486+
editType: 'ticks',
487+
description: [
488+
'Determines where tick labels are drawn with respect to their',
489+
'corresponding ticks and grid lines.',
490+
'Only has an effect for axes of `type` *date*',
491+
'When set to *period*, tick labels are drawn in the middle of the period',
492+
'between ticks.'
493+
].join(' ')
494+
},
481495
mirror: {
482496
valType: 'enumerated',
483497
values: [true, 'ticks', false, 'all', 'allticks'],

src/plots/gl3d/layout/axis_defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, options) {
5151
data: options.data,
5252
showGrid: true,
5353
noTickson: true,
54+
noTicklabelmode: true,
5455
bgColor: options.bgColor,
5556
calendar: options.calendar
5657
},

0 commit comments

Comments
 (0)