Skip to content

Commit

Permalink
Merge pull request #4064 from plotly/minor-log-axes-no-suffix-prefix
Browse files Browse the repository at this point in the history
do not show prefix and suffix for minor log axes
  • Loading branch information
archmoj authored Jul 22, 2019
2 parents 84e79e4 + cb521f5 commit 1400437
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,34 +571,44 @@ axes.calcTicks = function calcTicks(ax) {
if((ax._tmin < startTick) !== axrev) return [];

// return the full set of tick vals
var vals = [];
var tickVals = [];
if(ax.type === 'category' || ax.type === 'multicategory') {
endTick = (axrev) ? Math.max(-0.5, endTick) :
Math.min(ax._categories.length - 0.5, endTick);
}

var isDLog = (ax.type === 'log') && !(isNumeric(ax.dtick) || ax.dtick.charAt(0) === 'L');

var xPrevious = null;
var maxTicks = Math.max(1000, ax._length || 0);
for(var x = ax._tmin;
(axrev) ? (x >= endTick) : (x <= endTick);
x = axes.tickIncrement(x, ax.dtick, axrev, ax.calendar)) {
// prevent infinite loops - no more than one tick per pixel,
// and make sure each value is different from the previous
if(vals.length > maxTicks || x === xPrevious) break;
if(tickVals.length > maxTicks || x === xPrevious) break;
xPrevious = x;

vals.push(x);
var minor = false;
if(isDLog && (x !== (x | 0))) {
minor = true;
}

tickVals.push({
minor: minor,
value: x
});
}

// If same angle over a full circle, the last tick vals is a duplicate.
// TODO must do something similar for angular date axes.
if(isAngular(ax) && Math.abs(rng[1] - rng[0]) === 360) {
vals.pop();
tickVals.pop();
}

// save the last tick as well as first, so we can
// show the exponent only on the last one
ax._tmax = vals[vals.length - 1];
ax._tmax = (tickVals[tickVals.length - 1] || {}).value;

// for showing the rest of a date when the main tick label is only the
// latter part: ax._prevDateHead holds what we showed most recently.
Expand All @@ -607,8 +617,15 @@ axes.calcTicks = function calcTicks(ax) {
ax._prevDateHead = '';
ax._inCalcTicks = true;

var ticksOut = new Array(vals.length);
for(var i = 0; i < vals.length; i++) ticksOut[i] = axes.tickText(ax, vals[i]);
var ticksOut = new Array(tickVals.length);
for(var i = 0; i < tickVals.length; i++) {
ticksOut[i] = axes.tickText(
ax,
tickVals[i].value,
false, // hover
tickVals[i].minor // noSuffixPrefix
);
}

ax._inCalcTicks = false;

Expand Down Expand Up @@ -937,7 +954,7 @@ axes.tickFirst = function(ax) {
// ax is the axis layout, x is the tick value
// hover is a (truthy) flag for whether to show numbers with a bit
// more precision for hovertext
axes.tickText = function(ax, x, hover) {
axes.tickText = function(ax, x, hover, noSuffixPrefix) {
var out = tickTextObj(ax, x);
var arrayMode = ax.tickmode === 'array';
var extraPrecision = hover || arrayMode;
Expand Down Expand Up @@ -983,8 +1000,10 @@ axes.tickText = function(ax, x, hover) {
else formatLinear(ax, out, hover, extraPrecision, hideexp);

// add prefix and suffix
if(ax.tickprefix && !isHidden(ax.showtickprefix)) out.text = ax.tickprefix + out.text;
if(ax.ticksuffix && !isHidden(ax.showticksuffix)) out.text += ax.ticksuffix;
if(!noSuffixPrefix) {
if(ax.tickprefix && !isHidden(ax.showtickprefix)) out.text = ax.tickprefix + out.text;
if(ax.ticksuffix && !isHidden(ax.showticksuffix)) out.text += ax.ticksuffix;
}

// Setup ticks and grid lines boundaries
// at 1/2 a 'category' to the left/bottom
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 121 additions & 0 deletions test/image/mocks/log-axis_no-minor_suffix-prefix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"data": [
{
"name": "L10000000",
"y": [
1e8,
1e6,
1e7
],
"type": "scatter"
},
{
"name": "dtick=0.5 | tick0=1",
"xaxis": "x2",
"yaxis": "y2",
"y": [
1e8,
1e6,
1e7
],
"type": "scatter"
},
{
"name": "D1",
"xaxis": "x3",
"yaxis": "y3",
"y": [
1e8,
1e6,
1e7
],
"type": "scatter"
},
{
"name": "default",
"xaxis": "x4",
"yaxis": "y4",
"y": [
1e8,
1e6,
1e7
],
"type": "scatter"
}
],
"layout": {
"width": 800,
"height": 800,
"xaxis": {
"domain": [
0,
0.45
]
},
"xaxis2": {
"anchor": "y2",
"domain": [
0.6,
1
]
},
"xaxis3": {
"anchor": "y3",
"domain": [
0,
0.45
]
},
"xaxis4": {
"anchor": "y4",
"domain": [
0.6,
1
]
},
"yaxis": {
"tick0": 1,
"dtick": "L10000000",
"type": "log",
"tickprefix": "$",
"ticksuffix": "Hz",
"domain": [
0,
0.45
]
},
"yaxis2": {
"tick0": 1,
"dtick": 0.5,
"type": "log",
"tickprefix": "$",
"ticksuffix": "Hz",
"anchor": "x2",
"domain": [
0,
0.45
]
},
"yaxis3": {
"dtick": "D1",
"type": "log",
"tickprefix": "$",
"ticksuffix": "Hz",
"anchor": "x3",
"domain": [
0.6,
1
]
},
"yaxis4": {
"type": "log",
"tickprefix": "$",
"ticksuffix": "Hz",
"anchor": "x4",
"domain": [
0.6,
1
]
}
}
}

0 comments on commit 1400437

Please sign in to comment.