Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f5de76

Browse files
committed
standardize the extra we add to contours to avoid rounding errors
this avoids an edge case where the colorbar and the colorscale got out of sync or did not end at the same place
1 parent a004e40 commit 4f5de76

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

src/traces/contour/colorbar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var Plots = require('../../plots/plots');
1313
var drawColorbar = require('../../components/colorbar/draw');
1414

1515
var makeColorMap = require('./make_color_map');
16+
var endPlus = require('./end_plus');
1617

1718

1819
module.exports = function colorbar(gd, cd) {
@@ -52,7 +53,7 @@ module.exports = function colorbar(gd, cd) {
5253
})
5354
.levels({
5455
start: contours.start,
55-
end: contours.end,
56+
end: endPlus(contours),
5657
size: cs
5758
})
5859
.options(trace.colorbar)();

src/traces/contour/end_plus.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
/*
13+
* tiny helper to move the end of the contours a little to prevent
14+
* losing the last contour to rounding errors
15+
*/
16+
module.exports = function endPlus(contours) {
17+
return contours.end + contours.size / 1e6;
18+
};

src/traces/contour/make_color_map.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
var d3 = require('d3');
1313
var Colorscale = require('../../components/colorscale');
14+
var endPlus = require('./end_plus');
1415

1516
module.exports = function makeColorMap(trace) {
1617
var contours = trace.contours,
1718
start = contours.start,
18-
end = contours.end,
19+
end = endPlus(contours),
1920
cs = contours.size || 1,
20-
nc = Math.floor((end + cs / 10 - start) / cs) + 1,
21+
nc = Math.floor((end - start) / cs) + 1,
2122
extra = contours.coloring === 'lines' ? 0 : 1;
2223

2324
var scl = trace.colorscale,

src/traces/contour/plot.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Drawing = require('../../components/drawing');
1717
var heatmapPlot = require('../heatmap/plot');
1818
var makeCrossings = require('./make_crossings');
1919
var findAllPaths = require('./find_all_paths');
20+
var endPlus = require('./end_plus');
2021

2122

2223
module.exports = function plot(gd, plotinfo, cdcontours) {
@@ -81,9 +82,10 @@ function plotOne(gd, plotinfo, cd) {
8182

8283
function emptyPathinfo(contours, plotinfo, cd0) {
8384
var cs = contours.size,
84-
pathinfo = [];
85+
pathinfo = [],
86+
end = endPlus(contours);
8587

86-
for(var ci = contours.start; ci < contours.end + cs / 10; ci += cs) {
88+
for(var ci = contours.start; ci < end; ci += cs) {
8789
pathinfo.push({
8890
level: ci,
8991
// all the cells with nontrivial marching index

src/traces/contour/style.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ module.exports = function style(gd) {
3434

3535
var colorMap = makeColorMap(trace);
3636

37-
c.selectAll('g.contourlevel').each(function(d, i) {
37+
c.selectAll('g.contourlevel').each(function(d) {
3838
d3.select(this).selectAll('path')
3939
.call(Drawing.lineGroupStyle,
4040
line.width,
41-
contours.coloring === 'lines' ? colorMap(start + i * cs) : line.color,
41+
contours.coloring === 'lines' ? colorMap(d.level) : line.color,
4242
line.dash);
4343
});
4444

0 commit comments

Comments
 (0)