Skip to content

Skip non-numeric values in image trace plot #4325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/traces/image/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ module.exports = extendFlat({
values: cm,
dflt: 'rgb',
role: 'info',
editType: 'plot',
editType: 'calc',
description: 'Color model used to map the numerical color components described in `z` into colors.'
},
zmin: {
valType: 'info_array',
items: [
{valType: 'number', editType: 'plot'},
{valType: 'number', editType: 'plot'},
{valType: 'number', editType: 'plot'},
{valType: 'number', editType: 'plot'}
{valType: 'number', editType: 'calc'},
{valType: 'number', editType: 'calc'},
{valType: 'number', editType: 'calc'},
{valType: 'number', editType: 'calc'}
],
role: 'info',
editType: 'plot',
editType: 'calc',
description: [
'Array defining the lower bound for each color component.',
'Note that the default value will depend on the colormodel.',
Expand All @@ -57,13 +57,13 @@ module.exports = extendFlat({
zmax: {
valType: 'info_array',
items: [
{valType: 'number', editType: 'plot'},
{valType: 'number', editType: 'plot'},
{valType: 'number', editType: 'plot'},
{valType: 'number', editType: 'plot'}
{valType: 'number', editType: 'calc'},
{valType: 'number', editType: 'calc'},
{valType: 'number', editType: 'calc'},
{valType: 'number', editType: 'calc'}
],
role: 'info',
editType: 'plot',
editType: 'calc',
description: [
'Array defining the higher bound for each color component.',
'Note that the default value will depend on the colormodel.',
Expand Down
48 changes: 48 additions & 0 deletions src/traces/image/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

'use strict';

var Lib = require('../../lib');
var constants = require('./constants');
var isNumeric = require('fast-isnumeric');
var Axes = require('../../plots/cartesian/axes');
var maxRowLength = require('../../lib').maxRowLength;

Expand All @@ -28,6 +31,7 @@ module.exports = function calc(gd, trace) {
if(ya && ya.type === 'log') for(i = 0; i < h; i++) yrange.push(y0 + i * trace.dy);
trace._extremes[xa._id] = Axes.findExtremes(xa, xrange);
trace._extremes[ya._id] = Axes.findExtremes(ya, yrange);
trace._scaler = makeScaler(trace);

var cd0 = {
x0: x0,
Expand All @@ -38,3 +42,47 @@ module.exports = function calc(gd, trace) {
};
return [cd0];
};

function scale(zero, factor, min, max) {
return function(c) {
c = (c - zero) * factor;
c = Lib.constrain(c, min, max);
return c;
};
}

function constrain(min, max) {
return function(c) { return Lib.constrain(c, min, max);};
}

// Generate a function to scale color components according to zmin/zmax and the colormodel
function makeScaler(trace) {
var colormodel = trace.colormodel;
var n = colormodel.length;
var cr = constants.colormodel[colormodel];

trace._sArray = [];
// Loop over all color components
for(var k = 0; k < n; k++) {
if(cr.min[k] !== trace.zmin[k] || cr.max[k] !== trace.zmax[k]) {
trace._sArray.push(scale(
trace.zmin[k],
(cr.max[k] - cr.min[k]) / (trace.zmax[k] - trace.zmin[k]),
cr.min[k],
cr.max[k]
));
} else {
trace._sArray.push(constrain(cr.min[k], cr.max[k]));
}
}

return function(pixel) {
var c = pixel.slice(0, n);
for(var k = 0; k < n; k++) {
var ck = c[k];
if(!isNumeric(ck)) return false;
c[k] = trace._sArray[k](ck);
}
return c;
};
}
1 change: 0 additions & 1 deletion src/traces/image/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

module.exports = {
Expand Down
1 change: 0 additions & 1 deletion src/traces/image/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var Lib = require('../../lib');
Expand Down
2 changes: 1 addition & 1 deletion src/traces/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
attributes: require('./attributes'),
supplyDefaults: require('./defaults'),
calc: require('./calc'),
plot: require('./plot').plot,
plot: require('./plot'),
style: require('./style'),
hoverPoints: require('./hover'),
eventData: require('./event_data'),
Expand Down
55 changes: 9 additions & 46 deletions src/traces/image/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,13 @@
*/

'use strict';

var d3 = require('d3');
var Lib = require('../../lib');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
var constants = require('./constants');

module.exports = {};

// Generate a function to scale color components according to zmin/zmax and the colormodel
var scaler = function(trace) {
var colormodel = trace.colormodel;
var n = colormodel.length;
var cr = constants.colormodel[colormodel];

function scale(zero, factor, min, max) {
return function(c) {
c = (c - zero) * factor;
c = Lib.constrain(c, min, max);
return c;
};
}

function constrain(min, max) {
return function(c) { return Lib.constrain(c, min, max);};
}

var s = [];
// Loop over all color components
for(var k = 0; k < n; k++) {
if(cr.min[k] !== trace.zmin[k] || cr.max[k] !== trace.zmax[k]) {
s.push(scale(
trace.zmin[k],
(cr.max[k] - cr.min[k]) / (trace.zmax[k] - trace.zmin[k]),
cr.min[k],
cr.max[k]
));
} else {
s.push(constrain(cr.min[k], cr.max[k]));
}
}

return function(pixel) {
var c = pixel.slice(0, n);
for(var k = 0; k < n; k++) {
c[k] = s[k](c[k]);
}
return c;
};
};
module.exports.plot = function(gd, plotinfo, cdimage, imageLayer) {
module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
var xa = plotinfo.xaxis;
var ya = plotinfo.yaxis;

Expand Down Expand Up @@ -129,10 +87,10 @@ module.exports.plot = function(gd, plotinfo, cdimage, imageLayer) {
canvas.width = imageWidth;
canvas.height = imageHeight;
var context = canvas.getContext('2d');

var ipx = function(i) {return Lib.constrain(Math.round(xa.c2p(x0 + i * dx) - left), 0, imageWidth);};
var jpx = function(j) {return Lib.constrain(Math.round(ya.c2p(y0 + j * dy) - top), 0, imageHeight);};

trace._scaler = scaler(trace);
var fmt = constants.colormodel[trace.colormodel].fmt;
var c;
for(i = 0; i < cd0.w; i++) {
Expand All @@ -142,7 +100,12 @@ module.exports.plot = function(gd, plotinfo, cdimage, imageLayer) {
var jpx0 = jpx(j); var jpx1 = jpx(j + 1);
if(jpx1 === jpx0 || isNaN(jpx1) || isNaN(jpx0) || !z[j][i]) continue;
c = trace._scaler(z[j][i]);
context.fillStyle = trace.colormodel + '(' + fmt(c).join(',') + ')';
if(c) {
context.fillStyle = trace.colormodel + '(' + fmt(c).join(',') + ')';
} else {
// Return a transparent pixel
context.fillStyle = 'rgba(0,0,0,0)';
}
context.fillRect(ipx0, jpx0, ipx1 - ipx0, jpx1 - jpx0);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/traces/image/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var d3 = require('d3');
Expand Down
Binary file added test/image/baselines/image_non_numeric.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions test/image/mocks/image_non_numeric.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": [{
"type": "image",
"hoverinfo": "all",
"z": [
[
[true, true, true],
[false, false, false]
]
]
}],
"layout": {
"width": 400,
"height": 400
}
}