Skip to content
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

Add insiderange to cartesian axes #6735

Merged
merged 8 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ function mockColorBarAxis(gd, opts, zrange) {
noHover: true,
noTickson: true,
noTicklabelmode: true,
noInsideRange: true,
calendar: fullLayout.calendar // not really necessary (yet?)
};

Expand Down
15 changes: 5 additions & 10 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,11 @@ function _doPlot(gd, data, layout, config) {
seq.push(
drawAxes,
function insideTickLabelsAutorange(gd) {
if(gd._fullLayout._insideTickLabelsAutorange) {
relayout(gd, gd._fullLayout._insideTickLabelsAutorange).then(function() {
gd._fullLayout._insideTickLabelsAutorange = undefined;
if(gd._fullLayout._insideTickLabelsUpdaterange) {
relayout(gd, gd._fullLayout._insideTickLabelsUpdaterange).then(function() {
Axes.saveRangeInitial(gd, true);

gd._fullLayout._insideTickLabelsUpdaterange = undefined;
});
}
}
Expand All @@ -376,16 +378,9 @@ function _doPlot(gd, data, layout, config) {
// calculated. Would be much better to separate margin calculations from
// component drawing - see https://github.com/plotly/plotly.js/issues/2704
Plots.doAutoMargin,
saveRangeInitialForInsideTickLabels,
Plots.previousPromises
);

function saveRangeInitialForInsideTickLabels(gd) {
if(gd._fullLayout._insideTickLabelsAutorange) {
if(graphWasEmpty) Axes.saveRangeInitial(gd, true);
}
}

// even if everything we did was synchronous, return a promise
// so that the caller doesn't care which route we took
var plotDone = Lib.syncOrAsync(seq, gd);
Expand Down
94 changes: 79 additions & 15 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3821,28 +3821,92 @@ axes.drawLabels = function(gd, ax, opts) {
});
}

var computeTickLabelBoundingBoxes = function() {
var labelsMaxW = 0;
var labelsMaxH = 0;
tickLabels.each(function(d, i) {
var thisLabel = selectTickLabel(this);
var mathjaxGroup = thisLabel.select('.text-math-group');

if(mathjaxGroup.empty()) {
var bb;

if(ax._vals[i]) {
bb = ax._vals[i].bb || Drawing.bBox(thisLabel.node());
ax._vals[i].bb = bb;
}

labelsMaxW = Math.max(labelsMaxW, bb.width);
labelsMaxH = Math.max(labelsMaxW, bb.height);
}
});

return {
labelsMaxW: labelsMaxW,
labelsMaxH: labelsMaxH
};
};

var anchorAx = ax._anchorAxis;
if(
anchorAx && anchorAx.autorange &&
anchorAx && (anchorAx.autorange || anchorAx.insiderange) &&
insideTicklabelposition(ax) &&
!isLinked(fullLayout, ax._id)
) {
if(!fullLayout._insideTickLabelsAutorange) {
fullLayout._insideTickLabelsAutorange = {};
if(!fullLayout._insideTickLabelsUpdaterange) {
fullLayout._insideTickLabelsUpdaterange = {};
}
fullLayout._insideTickLabelsAutorange[anchorAx._name + '.autorange'] = anchorAx.autorange;

seq.push(
function computeFinalTickLabelBoundingBoxes() {
tickLabels.each(function(d, i) {
var thisLabel = selectTickLabel(this);
var mathjaxGroup = thisLabel.select('.text-math-group');
if(mathjaxGroup.empty()) {
ax._vals[i].bb = Drawing.bBox(thisLabel.node());
}
});

if(anchorAx.autorange) {
fullLayout._insideTickLabelsUpdaterange[anchorAx._name + '.autorange'] = anchorAx.autorange;

seq.push(computeTickLabelBoundingBoxes);
}

if(anchorAx.insiderange) {
var BBs = computeTickLabelBoundingBoxes();
var move = ax._id.charAt(0) === 'y' ?
BBs.labelsMaxW + 4 * TEXTPAD :
BBs.labelsMaxH;

if(ax.ticklabelposition === 'inside') {
move += ax.ticklen || 0;
}
);

if(ax._id.charAt(0) !== 'y') move = -move;

var sgn = (ax.side === 'right' || ax.side === 'top') ? 1 : -1;
var index = sgn === 1 ? 1 : 0;
var otherIndex = sgn === 1 ? 0 : 1;

var newRange = [];
newRange[otherIndex] = anchorAx.range[otherIndex];
newRange[index] = anchorAx.p2d(
anchorAx.d2p(anchorAx.range[index]) +
sgn * move
);
archmoj marked this conversation as resolved.
Show resolved Hide resolved

// handle partial ranges in insiderange
if(
anchorAx.autorange === 'min' ||
anchorAx.autorange === 'max reversed'
) {
newRange[0] = null;

anchorAx._rangeInitial0 = undefined;
anchorAx._rangeInitial1 = undefined;
} else if(
anchorAx.autorange === 'max' ||
anchorAx.autorange === 'min reversed'
) {
newRange[1] = null;

anchorAx._rangeInitial0 = undefined;
anchorAx._rangeInitial1 = undefined;
}

fullLayout._insideTickLabelsUpdaterange[anchorAx._name + '.range'] = newRange;
}
}

var done = Lib.syncOrAsync(seq);
Expand Down
7 changes: 6 additions & 1 deletion src/plots/cartesian/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,

setConvert(containerOut, layoutOut);

var insiderange;
if(!options.noInsiderange && axType !== 'log') {
insiderange = coerce('insiderange');
}

coerce('minallowed');
coerce('maxallowed');
var range = coerce('range');
var range = coerce('range', insiderange);
var autorangeDflt = containerOut.getAutorangeDflt(range, options);
var autorange = coerce('autorange', autorangeDflt);

Expand Down
15 changes: 15 additions & 0 deletions src/plots/cartesian/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,21 @@ module.exports = {
'If true, then zoom is disabled.'
].join(' ')
},
insiderange: {
valType: 'info_array',
items: [
{valType: 'any', editType: 'axrange'},
{valType: 'any', editType: 'axrange'}
],
editType: 'axrange',
description: [
'Could be used to set the desired inside range of this axis',
'(excluding the labels) when `ticklabelposition` of',
'the anchored axis has *inside*.',
'Not implemented for axes with `type` *log*',
archmoj marked this conversation as resolved.
Show resolved Hide resolved
'This would be ignored when `range` is provided.'
].join(' ')
},
// scaleanchor: not used directly, just put here for reference
// values are any opposite-letter axis id, or `false`.
scaleanchor: {
Expand Down
1 change: 1 addition & 0 deletions src/plots/gl3d/layout/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, options) {
noTicklabelstep: true,
noTicklabelposition: true,
noTicklabeloverflow: true,
noInsiderange: true,
bgColor: options.bgColor,
calendar: options.calendar
},
Expand Down
Binary file added test/image/baselines/zz-insiderange-x-partial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/zz-insiderange-x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/zz-insiderange-y.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions test/image/mocks/zz-insiderange-x-partial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"data": [{
"xaxis": "x",
"yaxis": "y",
"y": [1000, 10, 100, 1]
}, {
"xaxis": "x2",
"yaxis": "y2",
"y": [1000, 10, 100, 1]
}, {
"xaxis": "x3",
"yaxis": "y3",
"y": [1000, 10, 100, 1]
}, {
"xaxis": "x4",
"yaxis": "y4",
"y": [1000, 10, 100, 1]
}],
"layout": {
"xaxis": {
"insiderange": [-1, null],
"anchor": "y",
"domain": [0, 0.45],
"gridcolor": "white"
},
"yaxis": {
"anchor": "x",
"domain": [0, 0.45],
"side": "right",
"ticks": "inside",
"ticklabelposition": "inside",
"ticklen": 8,
"tickwidth": 3,
"gridcolor": "white"
},
"xaxis2": {
"insiderange": [null, 4],
"anchor": "y2",
"domain": [0, 0.45],
"gridcolor": "white"
},
"yaxis2": {
"anchor": "x2",
"domain": [0.55, 1],
"side": "left",
"ticks": "inside",
"ticklabelposition": "inside",
"ticklen": 8,
"tickwidth": 3,
"gridcolor": "white"
},
"xaxis3": {
"insiderange": [null, -1],
"autorange": "max reversed",
"anchor": "y3",
"domain": [0.55, 1],
"gridcolor": "white"
},
"yaxis3": {
"anchor": "x3",
"domain": [0, 0.45],
"side": "left",
"ticks": "inside",
"ticklabelposition": "inside",
"ticklen": 8,
"tickwidth": 3,
"gridcolor": "white"
},
"xaxis4": {
"insiderange": [4, null],
"autorange": "min reversed",
"anchor": "y4",
"domain": [0.55, 1],
"gridcolor": "white"
},
"yaxis4": {
"anchor": "x4",
"domain": [0.55, 1],
"side": "right",
"ticks": "inside",
"ticklabelposition": "inside",
"ticklen": 8,
"tickwidth": 3,
"gridcolor": "white"
},
"plot_bgcolor": "lightblue",
"showlegend": false,
"width": 900,
"height": 900,
"margin": {
"t": 60,
"b": 60,
"l": 60,
"r": 60
}
}
}
99 changes: 99 additions & 0 deletions test/image/mocks/zz-insiderange-x.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"data": [{
"xaxis": "x",
"yaxis": "y",
"y": [1000, 10, 100, 1]
}, {
"xaxis": "x2",
"yaxis": "y2",
"y": [1000, 10, 100, 1]
}, {
"xaxis": "x3",
"yaxis": "y3",
"x": [2000, 2001, 2002, 2003],
"y": [1000, 10, 100, 1]
}, {
"xaxis": "x4",
"yaxis": "y4",
"x": [2000, 2001, 2002, 2003],
"y": [1000, 10, 100, 1]
}],
"layout": {
"xaxis": {
"insiderange": [1, 2],
"anchor": "y",
"domain": [0, 0.45],
"gridcolor": "white"
},
"yaxis": {
"anchor": "x",
"domain": [0, 0.45],
"side": "right",
"ticks": "inside",
"ticklabelposition": "inside",
"ticklen": 8,
"tickwidth": 3,
"gridcolor": "white"
},
"xaxis2": {
"insiderange": [2, 1],
"anchor": "y2",
"domain": [0, 0.45],
"gridcolor": "white"
},
"yaxis2": {
"anchor": "x2",
"domain": [0.55, 1],
"side": "left",
"ticks": "inside",
"ticklabelposition": "inside",
"ticklen": 8,
"tickwidth": 3,
"gridcolor": "white"
},
"xaxis3": {
"type": "date",
"insiderange": ["2001-01", "2002-01"],
"anchor": "y3",
"domain": [0.55, 1],
"gridcolor": "white"
},
"yaxis3": {
"anchor": "x3",
"domain": [0, 0.45],
"side": "right",
"ticks": "inside",
"ticklabelposition": "inside",
"ticklen": 8,
"tickwidth": 3,
"gridcolor": "white"
},
"xaxis4": {
"type": "date",
"insiderange": ["2002-01", "2001-01"],
"anchor": "y4",
"domain": [0.55, 1],
"gridcolor": "white"
},
"yaxis4": {
"anchor": "x4",
"domain": [0.55, 1],
"side": "left",
"ticks": "inside",
"ticklabelposition": "inside",
"ticklen": 8,
"tickwidth": 3,
"gridcolor": "white"
},
"plot_bgcolor": "lightblue",
"showlegend": false,
"width": 900,
"height": 900,
"margin": {
"t": 60,
"b": 60,
"l": 60,
"r": 60
}
}
}
Loading