-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
WIP: Multiple y-axis formatting #6320
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
Changes from 6 commits
f013426
7e45c3b
68b8f8f
4194097
23efbe8
736db9d
e5f5e4c
06d1daf
78c51ca
4194f8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2248,16 +2248,27 @@ axes.draw = function(gd, arg, opts) { | |
|
||
var axList = (!arg || arg === 'redraw') ? axes.listIds(gd) : arg; | ||
|
||
var multAxisDepths = {'left': 0, 'right': 0}; | ||
|
||
return Lib.syncOrAsync(axList.map(function(axId) { | ||
return function() { | ||
if(!axId) return; | ||
|
||
var ax = axes.getFromId(gd, axId); | ||
var axDone = axes.drawOne(gd, ax, opts); | ||
var axDone = axes.drawOne(gd, ax, opts, multAxisDepths); | ||
|
||
// If we've just drawn a y axis, then keep track of its width so that we can push | ||
// out additional y axes if needed | ||
if(ax._id.charAt(0) === 'y') { | ||
if(ax.side === 'left') { | ||
hannahker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
multAxisDepths.left += 75; | ||
} else if(ax.side === 'right') { | ||
multAxisDepths.right += 75; | ||
} | ||
} | ||
|
||
ax._r = ax.range.slice(); | ||
ax._rl = Lib.simpleMap(ax._r, ax.r2l); | ||
|
||
return axDone; | ||
}; | ||
})); | ||
|
@@ -2290,7 +2301,7 @@ axes.draw = function(gd, arg, opts) { | |
* - ax._depth (when required only): | ||
* - and calls ax.setScale | ||
*/ | ||
axes.drawOne = function(gd, ax, opts) { | ||
axes.drawOne = function(gd, ax, opts, allDepths) { | ||
opts = opts || {}; | ||
|
||
var i, sp, plotinfo; | ||
|
@@ -2330,6 +2341,15 @@ axes.drawOne = function(gd, ax, opts) { | |
// (touching either the tick label or ticks) | ||
// depth can be expansive to compute, so we only do so when required | ||
ax._depth = null; | ||
// If drawing another y-axis, then look at the sum of the depths of existing axes | ||
// to determine how much to shift this one out by | ||
// TODO: Also need to account for the expected depth of the current axis | ||
// (if drawing from the left inwards) | ||
if(axLetter === 'y') { | ||
ax._xshift = allDepths[ax.side]; | ||
} else { | ||
ax._xshift = null; | ||
} | ||
|
||
// calcLabelLevelBbox can be expensive, | ||
// so make sure to not call it twice during the same Axes.drawOne call | ||
|
@@ -2640,13 +2660,11 @@ axes.drawOne = function(gd, ax, opts) { | |
Plots.autoMargin(gd, axMirrorAutoMarginID(ax), mirrorPush); | ||
Plots.autoMargin(gd, rangeSliderAutoMarginID(ax), rangeSliderPush); | ||
}); | ||
|
||
if(!opts.skipTitle && | ||
!(hasRangeSlider && ax.side === 'bottom') | ||
) { | ||
seq.push(function() { return drawTitle(gd, ax); }); | ||
} | ||
|
||
return Lib.syncOrAsync(seq); | ||
}; | ||
|
||
|
@@ -3770,16 +3788,28 @@ axes.getPxPosition = function(gd, ax) { | |
var side = ax.side; | ||
var anchorAxis; | ||
|
||
// Shift in the opposite direction depending on which side the axis is on | ||
// But don't shift if 'position' is specified | ||
var xshift = ax.position > 0 ? 0 : ax._xshift; | ||
xshift = side === 'left' ? xshift : -xshift; | ||
|
||
if(ax.anchor !== 'free') { | ||
anchorAxis = ax._anchorAxis; | ||
if(axLetter === 'y') { | ||
anchorAxis = { | ||
_offset: ax._anchorAxis._offset - xshift, | ||
_length: ax._anchorAxis._length | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Wondering it might be better to use anchorAxis = extendFlat({}, ax._anchorAxis, {
_offset: ax._anchorAxis._offset - xshift,
_length: ax._anchorAxis._length
}); or alternatively mutate ax._anchorAxis._offset = ax._anchorAxis._offset - xshift;
ax._anchorAxis._length = ax._anchorAxis._length; ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran into difficulties with the latter in that this function is called multiple times during the axis drawing so the offset value ends up as double what it should be. Could you clarify the issue with the modification here? I'm not totally understanding as this seems to be following the same pattern as was already implemented eg. in lines 3809-3817 below. |
||
} else { | ||
anchorAxis = ax._anchorAxis; | ||
} | ||
} else if(axLetter === 'x') { | ||
anchorAxis = { | ||
_offset: gs.t + (1 - (ax.position || 0)) * gs.h, | ||
_length: 0 | ||
}; | ||
} else if(axLetter === 'y') { | ||
anchorAxis = { | ||
_offset: gs.l + (ax.position || 0) * gs.w, | ||
_offset: (gs.l + (ax.position || 0) * gs.w) + xshift, | ||
_length: 0 | ||
}; | ||
} | ||
|
@@ -3868,7 +3898,7 @@ function drawTitle(gd, ax) { | |
} | ||
} | ||
} | ||
|
||
ax._titleDepth = titleStandoff; | ||
var pos = axes.getPxPosition(gd, ax); | ||
var transform, x, y; | ||
|
||
|
@@ -3880,7 +3910,6 @@ function drawTitle(gd, ax) { | |
x = (ax.side === 'right') ? pos + titleStandoff : pos - titleStandoff; | ||
transform = {rotate: '-90', offset: 0}; | ||
} | ||
|
||
var avoid; | ||
|
||
if(ax.type !== 'multicategory') { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
{ | ||
"data": [ | ||
{ | ||
"x": [ | ||
1, | ||
2, | ||
3 | ||
], | ||
"y": [ | ||
4, | ||
5, | ||
6 | ||
], | ||
"name": "yaxis1 data", | ||
"type": "scatter" | ||
}, | ||
{ | ||
"x": [ | ||
2, | ||
3, | ||
4 | ||
], | ||
"y": [ | ||
40, | ||
50, | ||
60 | ||
], | ||
"name": "yaxis2 data", | ||
"yaxis": "y2", | ||
"type": "scatter" | ||
}, | ||
{ | ||
"x": [ | ||
3, | ||
4, | ||
5 | ||
], | ||
"y": [ | ||
400, | ||
500, | ||
600 | ||
], | ||
"name": "yaxis3 data", | ||
"yaxis": "y3", | ||
"type": "scatter" | ||
}, | ||
{ | ||
"x": [ | ||
4, | ||
5, | ||
6 | ||
], | ||
"y": [ | ||
4000, | ||
5000, | ||
6000 | ||
], | ||
"name": "yaxis4 data", | ||
"yaxis": "y4", | ||
"type": "scatter" | ||
} | ||
], | ||
"layout": { | ||
"title": { | ||
"text": "multiple y-axes example - complex formatting" | ||
}, | ||
"width": 800, | ||
"xaxis": { | ||
"domain": [ | ||
0.5, | ||
1 | ||
] | ||
}, | ||
|
||
"yaxis": { | ||
"title": { | ||
"text": "yaxis title" | ||
|
||
}, | ||
"automargin": true | ||
}, | ||
"yaxis2": { | ||
"title": {"text": "yaxis2 title"}, | ||
"overlaying": "y", | ||
"ticklen":25 | ||
|
||
}, | ||
"yaxis3": { | ||
"title": { | ||
"text": "yaxis3 title", | ||
"font": {"size": 28} | ||
}, | ||
"tickfont": {"size": 28}, | ||
"overlaying": "y" | ||
}, | ||
"yaxis4": { | ||
"title": { | ||
"text": "yaxis4 title", | ||
"font": {"size": 8} | ||
}, | ||
"tickfont": {"size": 8}, | ||
"overlaying": "y" | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.