Skip to content

Commit

Permalink
Merge pull request #4177 from plotly/contourlabel-format-via-colorbar…
Browse files Browse the repository at this point in the history
…-fix

Fix contour(carpet) label formatting via colorbar settings
  • Loading branch information
etpinard committed Sep 9, 2019
2 parents b5f0316 + 2ea8b79 commit 4ef2f80
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ function drawColorBar(g, opts, gd) {
opts._xLeftFrac = xLeftFrac;
opts._yBottomFrac = yBottomFrac;

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

// position can't go in through supplyDefaults
// because that restricts it to [0,1]
Expand Down
14 changes: 10 additions & 4 deletions src/traces/contour/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var d3 = require('d3');

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

var contourFormat = exports.labelFormatter(contours, cd0.t.cb, gd._fullLayout);
var contourFormat = exports.labelFormatter(gd, cd0);

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

exports.labelFormatter = function(contours, colorbar, fullLayout) {
exports.labelFormatter = function(gd, cd0) {
var fullLayout = gd._fullLayout;
var trace = cd0.trace;
var contours = trace.contours;

if(contours.labelformat) {
return fullLayout._d3locale.numberFormat(contours.labelformat);
} else {
var formatAxis;
if(colorbar) {
formatAxis = colorbar.axis;
var cOpts = Colorscale.extractOpts(trace);
if(cOpts && cOpts.colorbar && cOpts.colorbar._axis) {
formatAxis = cOpts.colorbar._axis;
} else {
formatAxis = {
type: 'linear',
Expand Down
2 changes: 1 addition & 1 deletion src/traces/contourcarpet/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours, plotinfo, ca
// invalidate the getTextLocation cache in case paths changed
Lib.clearLocationCache();

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

var dummyText = Drawing.tester.append('text')
.attr('data-notex', 1)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions test/image/mocks/contour_label-formatting-via-colorbar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"data": [
{
"name": "contour / non-shared",
"type": "contour",
"z": [
[ 10, 10.625, 12.5, 15.625, 20 ],
[ 5.625, 6.25, 8.125, 11.25, 15.625 ],
[ 2.5, 3.125, 5, 8.125, 12.5 ],
[ 0.625, 1.25, 3.125, 6.25, 10.625 ],
[ 0, 0.625, 2.5, 5.625, 10 ]
],
"contours": { "showlabels": true },
"colorbar": {
"tickvals": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
"ticktext": [ 1, 22, 34, 5, 6, 77, 88, 99, 10101 ],
"len": 0.65,
"x": -0.05,
"xanchor": "right",
"y": 1.1,
"yanchor": "top"
}
},
{
"name": "contour / shared",
"type": "contour",
"z": [
[ 10, 10.625, 12.5, 15.625, 20 ],
[ 5.625, 6.25, 8.125, 11.25, 15.625 ],
[ 2.5, 3.125, 5, 8.125, 12.5 ],
[ 0.625, 1.25, 3.125, 6.25, 10.625 ],
[ 0, 0.625, 2.5, 5.625, 10 ]
],
"contours": { "showlabels": true },
"coloraxis": "coloraxis",
"xaxis": "x2",
"yaxis": "y2"
},
{
"type": "carpet",
"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 ],
"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 ],
"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 ],
"xaxis": "x3",
"yaxis": "y3",
"carpet": "c1"
},
{
"name": "contourcarpet / non-shared",
"type": "contourcarpet",
"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 ],
"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 ],
"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 ],
"contours": { "showlabels": true },
"colorbar": {
"tickvals": [ 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 ],
"ticktext": [ 1, 22, 34, 5, 6, 77, 88, 99, 10101 ],
"len": 0.5,
"x": -0.094,
"xanchor": "right",
"y": -0.05,
"yanchor": "bottom"
},
"xaxis": "x3",
"yaxis": "y3",
"carpet": "c1"
},
{
"type": "carpet",
"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 ],
"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 ],
"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 ],
"xaxis": "x4",
"yaxis": "y4",
"carpet": "c2"
},
{
"name": "contourcarpet / shared",
"type": "contourcarpet",
"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 ],
"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 ],
"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 ],
"contours": { "showlabels": true },
"coloraxis": "coloraxis2",
"xaxis": "x4",
"yaxis": "y4",
"carpet": "c2"
}
],
"layout": {
"grid": { "rows": 2, "columns": 2, "pattern": "independent" },
"coloraxis": {
"colorbar": {
"tickvals": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
"ticktext": [ 1, 22, 34, 5, 6, 77, 88, 99, 10101 ],
"len": 0.65,
"x": 1.05,
"xanchor": "left",
"y": 1.1,
"yanchor": "top"
}
},
"coloraxis2": {
"colorbar": {
"tickvals": [ 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 ],
"ticktext": [ 1, 22, 34, 5, 6, 77, 88, 99, 10101 ],
"len": 0.5,
"x": 1.05,
"xanchor": "left",
"y": -0.05,
"yanchor": "bottom"
}
},
"title": { "text": "Contour with Labels formatted using colorbar axis" },
"margin": { "b": 20, "t": 40 },
"width": 700,
"height": 450
}
}

0 comments on commit 4ef2f80

Please sign in to comment.