Skip to content

Commit 4ef2f80

Browse files
authored
Merge pull request #4177 from plotly/contourlabel-format-via-colorbar-fix
Fix contour(carpet) label formatting via colorbar settings
2 parents b5f0316 + 2ea8b79 commit 4ef2f80

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

Diff for: src/components/colorbar/draw.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ function drawColorBar(g, opts, gd) {
228228
opts._xLeftFrac = xLeftFrac;
229229
opts._yBottomFrac = yBottomFrac;
230230

231-
var ax = mockColorBarAxis(gd, opts, zrange);
231+
// stash mocked axis for contour label formatting
232+
var ax = opts._axis = mockColorBarAxis(gd, opts, zrange);
232233

233234
// position can't go in through supplyDefaults
234235
// because that restricts it to [0,1]

Diff for: src/traces/contour/plot.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var d3 = require('d3');
1313

1414
var Lib = require('../../lib');
1515
var Drawing = require('../../components/drawing');
16+
var Colorscale = require('../../components/colorscale');
1617
var svgTextUtils = require('../../lib/svg_text_utils');
1718
var Axes = require('../../plots/cartesian/axes');
1819
var setConvert = require('../../plots/cartesian/set_convert');
@@ -236,7 +237,7 @@ function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours) {
236237
// invalidate the getTextLocation cache in case paths changed
237238
Lib.clearLocationCache();
238239

239-
var contourFormat = exports.labelFormatter(contours, cd0.t.cb, gd._fullLayout);
240+
var contourFormat = exports.labelFormatter(gd, cd0);
240241

241242
var dummyText = Drawing.tester.append('text')
242243
.attr('data-notex', 1)
@@ -388,13 +389,18 @@ exports.createLineClip = function(lineContainer, clipLinesForLabels, gd, uid) {
388389
return lineClip;
389390
};
390391

391-
exports.labelFormatter = function(contours, colorbar, fullLayout) {
392+
exports.labelFormatter = function(gd, cd0) {
393+
var fullLayout = gd._fullLayout;
394+
var trace = cd0.trace;
395+
var contours = trace.contours;
396+
392397
if(contours.labelformat) {
393398
return fullLayout._d3locale.numberFormat(contours.labelformat);
394399
} else {
395400
var formatAxis;
396-
if(colorbar) {
397-
formatAxis = colorbar.axis;
401+
var cOpts = Colorscale.extractOpts(trace);
402+
if(cOpts && cOpts.colorbar && cOpts.colorbar._axis) {
403+
formatAxis = cOpts.colorbar._axis;
398404
} else {
399405
formatAxis = {
400406
type: 'linear',

Diff for: src/traces/contourcarpet/plot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours, plotinfo, ca
182182
// invalidate the getTextLocation cache in case paths changed
183183
Lib.clearLocationCache();
184184

185-
var contourFormat = contourPlot.labelFormatter(contours, cd0.t.cb, gd._fullLayout);
185+
var contourFormat = contourPlot.labelFormatter(gd, cd0);
186186

187187
var dummyText = Drawing.tester.append('text')
188188
.attr('data-notex', 1)
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"data": [
3+
{
4+
"name": "contour / non-shared",
5+
"type": "contour",
6+
"z": [
7+
[ 10, 10.625, 12.5, 15.625, 20 ],
8+
[ 5.625, 6.25, 8.125, 11.25, 15.625 ],
9+
[ 2.5, 3.125, 5, 8.125, 12.5 ],
10+
[ 0.625, 1.25, 3.125, 6.25, 10.625 ],
11+
[ 0, 0.625, 2.5, 5.625, 10 ]
12+
],
13+
"contours": { "showlabels": true },
14+
"colorbar": {
15+
"tickvals": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
16+
"ticktext": [ 1, 22, 34, 5, 6, 77, 88, 99, 10101 ],
17+
"len": 0.65,
18+
"x": -0.05,
19+
"xanchor": "right",
20+
"y": 1.1,
21+
"yanchor": "top"
22+
}
23+
},
24+
{
25+
"name": "contour / shared",
26+
"type": "contour",
27+
"z": [
28+
[ 10, 10.625, 12.5, 15.625, 20 ],
29+
[ 5.625, 6.25, 8.125, 11.25, 15.625 ],
30+
[ 2.5, 3.125, 5, 8.125, 12.5 ],
31+
[ 0.625, 1.25, 3.125, 6.25, 10.625 ],
32+
[ 0, 0.625, 2.5, 5.625, 10 ]
33+
],
34+
"contours": { "showlabels": true },
35+
"coloraxis": "coloraxis",
36+
"xaxis": "x2",
37+
"yaxis": "y2"
38+
},
39+
{
40+
"type": "carpet",
41+
"a": [ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 ],
42+
"b": [ 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82 ],
43+
"y": [ 4, 4.2, 4.4, 4.6, 4.8, 5, 5.1, 5.3, 5.5, 5.7, 5.9, 6.1, 6.2, 6.4, 6.6, 6.8, 7, 7.2, 7.4, 7.6, 7.8, 8, 8.2, 8.4, 8.8, 9, 9.2, 9.4, 9.6, 9.8 ],
44+
"xaxis": "x3",
45+
"yaxis": "y3",
46+
"carpet": "c1"
47+
},
48+
{
49+
"name": "contourcarpet / non-shared",
50+
"type": "contourcarpet",
51+
"a": [ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 ],
52+
"b": [ 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82 ],
53+
"z": [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.12, 0.22, 0.32, 0.42, 0.52, 0.62, 0.14, 0.24, 0.34, 0.44, 0.54, 0.64, 0.16, 0.26, 0.36, 0.46, 0.56, 0.66, 0.18, 0.28, 0.38, 0.48, 0.58, 0.68 ],
54+
"contours": { "showlabels": true },
55+
"colorbar": {
56+
"tickvals": [ 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 ],
57+
"ticktext": [ 1, 22, 34, 5, 6, 77, 88, 99, 10101 ],
58+
"len": 0.5,
59+
"x": -0.094,
60+
"xanchor": "right",
61+
"y": -0.05,
62+
"yanchor": "bottom"
63+
},
64+
"xaxis": "x3",
65+
"yaxis": "y3",
66+
"carpet": "c1"
67+
},
68+
{
69+
"type": "carpet",
70+
"a": [ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 ],
71+
"b": [ 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82 ],
72+
"y": [ 4, 4.2, 4.4, 4.6, 4.8, 5, 5.1, 5.3, 5.5, 5.7, 5.9, 6.1, 6.2, 6.4, 6.6, 6.8, 7, 7.2, 7.4, 7.6, 7.8, 8, 8.2, 8.4, 8.8, 9, 9.2, 9.4, 9.6, 9.8 ],
73+
"xaxis": "x4",
74+
"yaxis": "y4",
75+
"carpet": "c2"
76+
},
77+
{
78+
"name": "contourcarpet / shared",
79+
"type": "contourcarpet",
80+
"a": [ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 ],
81+
"b": [ 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82, 1.01, 1.12, 1.24, 1.38, 1.56, 1.82 ],
82+
"z": [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.12, 0.22, 0.32, 0.42, 0.52, 0.62, 0.14, 0.24, 0.34, 0.44, 0.54, 0.64, 0.16, 0.26, 0.36, 0.46, 0.56, 0.66, 0.18, 0.28, 0.38, 0.48, 0.58, 0.68 ],
83+
"contours": { "showlabels": true },
84+
"coloraxis": "coloraxis2",
85+
"xaxis": "x4",
86+
"yaxis": "y4",
87+
"carpet": "c2"
88+
}
89+
],
90+
"layout": {
91+
"grid": { "rows": 2, "columns": 2, "pattern": "independent" },
92+
"coloraxis": {
93+
"colorbar": {
94+
"tickvals": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
95+
"ticktext": [ 1, 22, 34, 5, 6, 77, 88, 99, 10101 ],
96+
"len": 0.65,
97+
"x": 1.05,
98+
"xanchor": "left",
99+
"y": 1.1,
100+
"yanchor": "top"
101+
}
102+
},
103+
"coloraxis2": {
104+
"colorbar": {
105+
"tickvals": [ 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 ],
106+
"ticktext": [ 1, 22, 34, 5, 6, 77, 88, 99, 10101 ],
107+
"len": 0.5,
108+
"x": 1.05,
109+
"xanchor": "left",
110+
"y": -0.05,
111+
"yanchor": "bottom"
112+
}
113+
},
114+
"title": { "text": "Contour with Labels formatted using colorbar axis" },
115+
"margin": { "b": 20, "t": 40 },
116+
"width": 700,
117+
"height": 450
118+
}
119+
}

0 commit comments

Comments
 (0)