Skip to content

Commit e86c95b

Browse files
committed
image: implementation
1 parent 8429245 commit e86c95b

26 files changed

+954
-1
lines changed

lib/image.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright 2012-2019, 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+
'use strict';
10+
11+
module.exports = require('../src/traces/image');

lib/index-cartesian.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Plotly.register([
1717
require('./histogram'),
1818
require('./histogram2d'),
1919
require('./histogram2dcontour'),
20+
require('./image'),
2021
require('./pie'),
2122
require('./contour'),
2223
require('./scatterternary'),

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Plotly.register([
5656

5757
require('./sankey'),
5858
require('./indicator'),
59+
require('./image'),
5960

6061
require('./table'),
6162

src/plots/cartesian/layout_defaults.js

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
8787
if(!traceIs(trace, 'carpet') || (trace.type === 'carpet' && !trace._cheater)) {
8888
if(xaName) xaMustDisplay[xaName] = true;
8989
}
90+
91+
if(trace.type === 'image') {
92+
if(yaName) yaMustForward[yaName] = false;
93+
if(yaName) yaMayBackward[yaName] = true;
94+
}
9095
}
9196

9297
// Two things trigger axis visibility:

src/traces/image/calc.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2012-2019, 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+
'use strict';
10+
11+
// var Registry = require('../../registry');
12+
// var Lib = require('../../lib');
13+
var Axes = require('../../plots/cartesian/axes');
14+
15+
module.exports = function calc(gd, trace) {
16+
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
17+
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
18+
19+
var x0 = trace.x0 - trace.dx / 2;
20+
var y0 = trace.y0 - trace.dy / 2;
21+
var h = trace.z.length;
22+
var w = trace.z[0].length;
23+
24+
trace._extremes[xa._id] = Axes.findExtremes(xa, [x0, x0 + w * trace.dx]);
25+
trace._extremes[ya._id] = Axes.findExtremes(ya, [y0, y0 + h * trace.dy]);
26+
27+
var cd0 = {
28+
x0: x0,
29+
y0: y0,
30+
z: trace.z,
31+
w: w,
32+
h: h
33+
};
34+
return [cd0];
35+
};

src/traces/image/constants.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2012-2019, 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+
module.exports = {
13+
colormodel: {
14+
rgb: {
15+
min: [0, 0, 0],
16+
max: [255, 255, 255],
17+
fmt: function(c) {return c.slice(0, 3);},
18+
suffix: ['', '', '']
19+
},
20+
rgba: {
21+
min: [0, 0, 0, 0],
22+
max: [255, 255, 255, 1],
23+
fmt: function(c) {return c.slice(0, 4);},
24+
suffix: ['', '', '', '']
25+
},
26+
hsl: {
27+
min: [0, 0, 0],
28+
max: [360, 100, 100],
29+
fmt: function(c) {
30+
var p = c.slice(0, 3);
31+
p[1] = p[1] + '%';
32+
p[2] = p[2] + '%';
33+
return p;
34+
},
35+
suffix: ['°', '%', '%']
36+
},
37+
hsla: {
38+
min: [0, 0, 0, 0],
39+
max: [360, 100, 100, 1],
40+
fmt: function(c) {
41+
var p = c.slice(0, 4);
42+
p[1] = p[1] + '%';
43+
p[2] = p[2] + '%';
44+
return p;
45+
},
46+
suffix: ['°', '%', '%', '']
47+
}
48+
}
49+
};

src/traces/image/defaults.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2012-2019, 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+
var Lib = require('../../lib');
13+
var attributes = require('./attributes');
14+
var constants = require('./constants');
15+
16+
module.exports = function supplyDefaults(traceIn, traceOut) {
17+
function coerce(attr, dflt) {
18+
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
19+
}
20+
var z = coerce('z');
21+
if(z === undefined || !z.length) {
22+
traceOut.visible = false;
23+
return;
24+
}
25+
26+
coerce('x0');
27+
coerce('y0');
28+
coerce('dx');
29+
coerce('dy');
30+
var colormodel = coerce('colormodel');
31+
32+
coerce('zmin', constants.colormodel[colormodel].min);
33+
coerce('zmax', constants.colormodel[colormodel].max);
34+
35+
coerce('hovertemplate');
36+
};

src/traces/image/event_data.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2012-2019, 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+
'use strict';
10+
11+
module.exports = function eventData(out, pt) {
12+
out.colormodel = pt.trace.colormodel;
13+
if('xVal' in pt) out.x = pt.xVal;
14+
if('yVal' in pt) out.y = pt.yVal;
15+
if(pt.xa) out.xaxis = pt.xa;
16+
if(pt.ya) out.yaxis = pt.ya;
17+
out.c = pt.c;
18+
return out;
19+
};

src/traces/image/hover.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright 2012-2019, 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+
'use strict';
10+
11+
var Fx = require('../../components/fx');
12+
var Lib = require('../../lib');
13+
var constants = require('./constants');
14+
15+
// var Axes = require('../../plots/cartesian/axes');
16+
17+
module.exports = function hoverPoints(pointData, xval, yval) {
18+
var cd0 = pointData.cd[0];
19+
var trace = cd0.trace;
20+
var xa = pointData.xa;
21+
var ya = pointData.ya;
22+
23+
// Return early if not on image
24+
if(Fx.inbox(xval - cd0.x0, xval - (cd0.x0 + cd0.w * trace.dx), 0) > 0 ||
25+
Fx.inbox(yval - cd0.y0, yval - (cd0.y0 + cd0.h * trace.dy), 0) > 0) {
26+
return;
27+
}
28+
29+
// Find nearest pixel's index and pixel center
30+
var nx = Math.floor((xval - cd0.x0) / trace.dx);
31+
var ny = Math.floor(Math.abs(yval - cd0.y0) / trace.dy);
32+
33+
var hoverinfo = cd0.hi || trace.hoverinfo;
34+
var fmtColor;
35+
if(hoverinfo) {
36+
var parts = hoverinfo.split('+');
37+
if(parts.indexOf('all') !== -1) parts = ['color'];
38+
if(parts.indexOf('color') !== -1) fmtColor = true;
39+
}
40+
41+
var colormodel = trace.colormodel;
42+
var dims = colormodel.length;
43+
var c = trace._scaler(cd0.z[ny][nx]);
44+
var s = constants.colormodel[colormodel].suffix;
45+
46+
var colorstring = [];
47+
if(trace.hovertemplate || fmtColor) {
48+
colorstring.push('[' + [c[0] + s[0], c[1] + s[1], c[2] + s[2]].join(', '));
49+
if(dims === 4) colorstring.push(', ' + c[3] + s[3]);
50+
colorstring.push(']');
51+
colorstring = colorstring.join('');
52+
pointData.extraText = '<span style="text-transform:uppercase">' + colormodel + '</span>: ' + colorstring;
53+
}
54+
55+
var py = ya.c2p(cd0.y0 + (ny + 0.5) * trace.dy);
56+
var xVal = cd0.x0 + (nx + 0.5) * trace.dx;
57+
var yVal = cd0.y0 + (ny + 0.5) * trace.dy;
58+
var zLabel = '[' + cd0.z[ny][nx].slice(0, trace.colormodel.length).join(', ') + ']';
59+
return [Lib.extendFlat(pointData, {
60+
index: [ny, nx],
61+
x0: xa.c2p(cd0.x0 + nx * trace.dx),
62+
x1: xa.c2p(cd0.x0 + (nx + 1) * trace.dx),
63+
y0: py,
64+
y1: py,
65+
c: c,
66+
xVal: xVal,
67+
xLabelVal: xVal,
68+
yVal: yVal,
69+
yLabelVal: yVal,
70+
zLabelVal: zLabel,
71+
hovertemplateLabels: {
72+
'zLabel': zLabel,
73+
'cLabel': colorstring,
74+
'c[0]Label': c[0] + s[0],
75+
'c[1]Label': c[1] + s[1],
76+
'c[2]Label': c[2] + s[2]
77+
}
78+
})];
79+
};

src/traces/image/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2012-2019, 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+
'use strict';
10+
11+
module.exports = {
12+
attributes: require('./attributes'),
13+
supplyDefaults: require('./defaults'),
14+
calc: require('./calc'),
15+
plot: require('./plot').plot,
16+
style: require('./style'),
17+
hoverPoints: require('./hover'),
18+
eventData: require('./event_data'),
19+
20+
moduleType: 'trace',
21+
name: 'image',
22+
basePlotModule: require('../../plots/cartesian'),
23+
categories: ['cartesian', 'svg', '2dMap'],
24+
animatable: false,
25+
meta: {
26+
description: [
27+
'Display an image, i.e. data on a 2D regular raster.'
28+
].join(' ')
29+
}
30+
};

0 commit comments

Comments
 (0)