Skip to content

Commit

Permalink
Merge pull request #2411 from plotly/one-contour-label-fix
Browse files Browse the repository at this point in the history
fall back for contour labels when there's only one contour
  • Loading branch information
alexcjohnson authored Feb 27, 2018
2 parents efa1275 + 492db56 commit 36b8483
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
16 changes: 11 additions & 5 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,8 @@ function autoShiftMonthBins(binStart, data, dtick, dataMin, calendar) {
// Ticks and grids
// ----------------------------------------------------

// calculate the ticks: text, values, positioning
// if ticks are set to automatic, determine the right values (tick0,dtick)
// in any case, set tickround to # of digits to round tick labels to,
// or codes to this effect for log and date scales
axes.calcTicks = function calcTicks(ax) {
// ensure we have tick0, dtick, and tick rounding calculated
axes.prepTicks = function(ax) {
var rng = Lib.simpleMap(ax.range, ax.r2l);

// calculate max number of (auto) ticks to display based on plot size
Expand Down Expand Up @@ -787,6 +784,15 @@ axes.calcTicks = function calcTicks(ax) {

// now figure out rounding of tick values
autoTickRound(ax);
};

// calculate the ticks: text, values, positioning
// if ticks are set to automatic, determine the right values (tick0,dtick)
// in any case, set tickround to # of digits to round tick labels to,
// or codes to this effect for log and date scales
axes.calcTicks = function calcTicks(ax) {
axes.prepTicks(ax);
var rng = Lib.simpleMap(ax.range, ax.r2l);

// now that we've figured out the auto values for formatting
// in case we're missing some ticktext, we can break out for array ticks
Expand Down
2 changes: 1 addition & 1 deletion src/traces/carpet/calc_gridlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = function calcGridlines(trace, cd, axisLetter, crossAxisLetter)
var na = trace.a.length;
var nb = trace.b.length;

Axes.calcTicks(axis);
Axes.prepTicks(axis);

// don't leave tickvals in axis looking like an attribute
if(axis.tickmode === 'array') delete axis.tickvals;
Expand Down
12 changes: 6 additions & 6 deletions src/traces/contour/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,19 +428,19 @@ exports.labelFormatter = function(contours, colorbar, fullLayout) {
formatAxis.range = [value[0], value[value.length - 1]];
}
else formatAxis.range = [value, value];

if(formatAxis.range[0] === formatAxis.range[1]) {
formatAxis.range[1] += formatAxis.range[0] || 1;
}
formatAxis.nticks = 1000;
}
else {
formatAxis.range = [contours.start, contours.end];
formatAxis.nticks = (contours.end - contours.start) / contours.size;
}

if(formatAxis.range[0] === formatAxis.range[1]) {
formatAxis.range[1] += formatAxis.range[0] || 1;
}
if(!formatAxis.nticks) formatAxis.nticks = 1000;

setConvert(formatAxis, fullLayout);
Axes.calcTicks(formatAxis);
Axes.prepTicks(formatAxis);
formatAxis._tmin = null;
formatAxis._tmax = null;
}
Expand Down
21 changes: 20 additions & 1 deletion test/jasmine/tests/contour_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ describe('contour calc', function() {
});
});

describe('contour edits', function() {
describe('contour plotting and editing', function() {
var gd;

beforeEach(function() {
Expand Down Expand Up @@ -388,4 +388,23 @@ describe('contour edits', function() {
.catch(fail)
.then(done);
});

it('works and draws labels when explicitly specifying ncontours=1', function(done) {
Plotly.newPlot(gd, [{
z: [[0.20, 0.57], [0.3, 0.4]],
type: 'contour',
zmin: 0.4,
zmax: 0.41,
ncontours: 1,
showscale: false,
contours: {showlabels: true}
}], {
width: 500, height: 500
})
.then(function() {
expect(gd.querySelector('.contourlabels text').textContent).toBe('0.41');
})
.catch(fail)
.then(done);
});
});

0 comments on commit 36b8483

Please sign in to comment.