-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add tickson: 'boundaries'
for category cartesian axes
#3275
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3a953db
coerce tickson,
etpinard 90c653b
adapt Axes.calcTicks output for tickson:'boundaries'
etpinard 668f338
add tickson boundaries mock
etpinard 78ef22c
add tickson dflt
etpinard 78ff4d5
don't standoff ax labels under tickons:'boundaries'
etpinard 6d0a3fc
fix dtick!==1 case, mv tickon:'boundaries' to formatCategory
etpinard 09ac230
rotate labels 90deg when they overlap w/ tick-on-boundaries positions
etpinard b05f8fb
test a few more things in tickson_boundaries mock
etpinard bae37be
move 'maxFontSize' and 'autoangle' to fixLabelOverlaps scope
etpinard eeb9055
split overlap algo based on tickson + use tickwidth for extra pad
etpinard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1163,6 +1163,20 @@ function formatCategory(ax, out) { | |
var tt = ax._categories[Math.round(out.x)]; | ||
if(tt === undefined) tt = ''; | ||
out.text = String(tt); | ||
|
||
// Setup ticks and grid lines boundaries | ||
// at 1/2 a 'category' to the left/bottom | ||
if(ax.tickson === 'boundaries') { | ||
var inbounds = function(v) { | ||
var p = ax.l2p(v); | ||
return p >= 0 && p <= ax._length ? v : null; | ||
}; | ||
|
||
out.xbnd = [ | ||
inbounds(out.x - 0.5), | ||
inbounds(out.x + ax.dtick - 0.5) | ||
]; | ||
} | ||
} | ||
|
||
function formatLinear(ax, out, hover, extraPrecision, hideexp) { | ||
|
@@ -1610,14 +1624,41 @@ axes.drawOne = function(gd, ax, opts) { | |
var subplotsWithAx = axes.getSubplots(gd, ax); | ||
|
||
var vals = ax._vals = axes.calcTicks(ax); | ||
// We remove zero lines, grid lines, and inside ticks if they're within 1px of the end | ||
// The key case here is removing zero lines when the axis bound is zero | ||
var valsClipped = ax._valsClipped = axes.clipEnds(ax, vals); | ||
|
||
if(!ax.visible) return; | ||
|
||
var transFn = axes.makeTransFn(ax); | ||
|
||
// We remove zero lines, grid lines, and inside ticks if they're within 1px of the end | ||
// The key case here is removing zero lines when the axis bound is zero | ||
var valsClipped; | ||
var tickVals; | ||
var gridVals; | ||
|
||
if(ax.tickson === 'boundaries' && vals.length) { | ||
// valsBoundaries is not used for labels; | ||
// no need to worry about the other tickTextObj keys | ||
var valsBoundaries = []; | ||
var _push = function(d, bndIndex) { | ||
var xb = d.xbnd[bndIndex]; | ||
if(xb !== null) { | ||
valsBoundaries.push(Lib.extendFlat({}, d, {x: xb})); | ||
} | ||
}; | ||
for(i = 0; i < vals.length; i++) _push(vals[i], 0); | ||
_push(vals[i - 1], 1); | ||
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. nice solution, keeping |
||
|
||
valsClipped = axes.clipEnds(ax, valsBoundaries); | ||
tickVals = ax.ticks === 'inside' ? valsClipped : valsBoundaries; | ||
gridVals = valsClipped; | ||
} else { | ||
valsClipped = axes.clipEnds(ax, vals); | ||
tickVals = ax.ticks === 'inside' ? valsClipped : vals; | ||
gridVals = valsClipped; | ||
} | ||
|
||
ax._valsClipped = valsClipped; | ||
|
||
if(!fullLayout._hasOnlyLargeSploms) { | ||
// keep track of which subplots (by main conteraxis) we've already | ||
// drawn grids for, so we don't overdraw overlaying subplots | ||
|
@@ -1637,7 +1678,7 @@ axes.drawOne = function(gd, ax, opts) { | |
'M' + counterAxis._offset + ',0h' + counterAxis._length; | ||
|
||
axes.drawGrid(gd, ax, { | ||
vals: valsClipped, | ||
vals: gridVals, | ||
layer: plotinfo.gridlayer.select('.' + axId), | ||
path: gridPath, | ||
transFn: transFn | ||
|
@@ -1652,7 +1693,6 @@ axes.drawOne = function(gd, ax, opts) { | |
} | ||
|
||
var tickSigns = axes.getTickSigns(ax); | ||
var tickVals = ax.ticks === 'inside' ? valsClipped : vals; | ||
var tickSubplots = []; | ||
|
||
if(ax.ticks) { | ||
|
@@ -1920,8 +1960,9 @@ axes.makeTickPath = function(ax, shift, sgn) { | |
axes.makeLabelFns = function(ax, shift, angle) { | ||
var axLetter = ax._id.charAt(0); | ||
var pad = (ax.linewidth || 1) / 2; | ||
var ticksOnOutsideLabels = ax.tickson !== 'boundaries' && ax.ticks === 'outside'; | ||
|
||
var labelStandoff = ax.ticks === 'outside' ? ax.ticklen : 0; | ||
var labelStandoff = ticksOnOutsideLabels ? ax.ticklen : 0; | ||
var labelShift = 0; | ||
|
||
if(angle && ax.ticks === 'outside') { | ||
|
@@ -1930,7 +1971,7 @@ axes.makeLabelFns = function(ax, shift, angle) { | |
labelShift = ax.ticklen * Math.sin(rad); | ||
} | ||
|
||
if(ax.showticklabels && (ax.ticks === 'outside' || ax.showline)) { | ||
if(ax.showticklabels && (ticksOnOutsideLabels || ax.showline)) { | ||
labelStandoff += 0.2 * ax.tickfont.size; | ||
} | ||
|
||
|
@@ -2018,7 +2059,6 @@ axes.drawTicks = function(gd, ax, opts) { | |
ticks.attr('transform', opts.transFn); | ||
}; | ||
|
||
|
||
/** | ||
* Draw axis grid | ||
* | ||
|
@@ -2277,6 +2317,7 @@ axes.drawLabels = function(gd, ax, opts) { | |
(ax.type !== 'log' || String(ax.dtick).charAt(0) !== 'D') | ||
) { | ||
var lbbArray = []; | ||
var i; | ||
|
||
tickLabels.each(function(d) { | ||
var s = d3.select(this); | ||
|
@@ -2298,14 +2339,26 @@ axes.drawLabels = function(gd, ax, opts) { | |
}); | ||
}); | ||
|
||
for(var i = 0; i < lbbArray.length - 1; i++) { | ||
for(i = 0; i < lbbArray.length - 1; i++) { | ||
if(Lib.bBoxIntersect(lbbArray[i], lbbArray[i + 1])) { | ||
// any overlap at all - set 30 degrees | ||
autoangle = 30; | ||
break; | ||
} | ||
} | ||
|
||
if(ax.tickson === 'boundaries') { | ||
for(i = 0; i < lbbArray.length; i++) { | ||
if( | ||
(vals[i].xl !== null && (lbbArray[i].left - ax.l2p(vals[i].xbnd[0])) < 2) || | ||
alexcjohnson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(vals[i].xr !== null && (ax.l2p(vals[i].xbnd[1]) - lbbArray[i].right) < 2) | ||
) { | ||
autoangle = 90; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if(autoangle) { | ||
var tickspacing = Math.abs( | ||
(vals[vals.length - 1].x - vals[0].x) * ax._m | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
{ | ||
"data": [ | ||
{ | ||
"type": "box", | ||
"x": ["day 1", "day 1", "day 1", "day 1", "day 1", "day 1", "day 2", "day 2", "day 2", "day 2", "day 2", "day 2"], | ||
"y": [0.2, 0.2, 0.6, 1, 0.5, 0.4, 0.2, 0.7, 0.9, 0.1, 0.5, 0.3] | ||
}, | ||
{ | ||
"type": "box", | ||
"x": ["day 1", "day 1", "day 1", "day 1", "day 1", "day 1", "day 2", "day 2", "day 2", "day 2", "day 2", "day 2"], | ||
"y": [0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1, 0.3, 0.6, 0.8, 0.5] | ||
}, | ||
{ | ||
"type": "box", | ||
"x": ["day 1", "day 1", "day 1", "day 1", "day 1", "day 1", "day 2", "day 2", "day 2", "day 2", "day 2", "day 2"], | ||
"y": [0.6, 0.7, 0.3, 0.6, 0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2] | ||
}, | ||
|
||
{ | ||
"type": "bar", | ||
"x": [1, 2, 1], | ||
"y": ["apples", "bananas", "clementines"], | ||
"orientation": "h", | ||
"xaxis": "x2", | ||
"yaxis": "y2" | ||
}, | ||
{ | ||
"type": "bar", | ||
"x": [1.3, 2.2, 0.8], | ||
"y": ["apples", "bananas", "clementines"], | ||
"orientation": "h", | ||
"xaxis": "x2", | ||
"yaxis": "y2" | ||
}, | ||
{ | ||
"type": "bar", | ||
"x": [3, 3.2, 1.8], | ||
"y": ["apples", "bananas", "clementines"], | ||
"orientation": "h", | ||
"xaxis": "x2", | ||
"yaxis": "y2" | ||
}, | ||
|
||
{ | ||
"type": "bar", | ||
"name": "with dtick !== 1", | ||
"x": ["a", "b", "c", "d", "e", "f", "g", "h"], | ||
"y": [1, 2, 1, 2, 1, 3, 4, 1], | ||
"xaxis": "x3", | ||
"yaxis": "y3" | ||
}, | ||
|
||
{ | ||
"mode": "markers", | ||
"marker": {"symbol": "square"}, | ||
"name": "with overlapping tick labels", | ||
"x": ["A very long title", "short", "Another very long title"], | ||
"y": [1, 4, 2], | ||
"xaxis": "x4", | ||
"yaxis": "y4" | ||
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. |
||
} | ||
], | ||
"layout": { | ||
"boxmode": "group", | ||
"grid": { | ||
"rows": 4, | ||
"columns": 1, | ||
"pattern": "independent", | ||
"ygap": 0.2 | ||
}, | ||
"xaxis": { | ||
"ticks": "outside", | ||
"tickson": "boundaries", | ||
"gridcolor": "white", | ||
"gridwidth": 4 | ||
}, | ||
"yaxis2": { | ||
"ticks": "inside", | ||
"tickson": "boundaries", | ||
"gridcolor": "white", | ||
"gridwidth": 4 | ||
}, | ||
"xaxis3": { | ||
"ticks": "inside", | ||
"tickson": "boundaries", | ||
"gridcolor": "white", | ||
"gridwidth": 4, | ||
"dtick": 2 | ||
}, | ||
"xaxis4": { | ||
"domain": [0.22, 0.78], | ||
"ticks": "outside", | ||
"ticklen": 20, | ||
"tickson": "boundaries", | ||
"gridcolor": "white", | ||
"gridwidth": 4 | ||
}, | ||
"plot_bgcolor": "lightgrey", | ||
"showlegend": false, | ||
"width": 500, | ||
"height": 800, | ||
"margin": {"b": 140} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.