Skip to content

Commit 99fc02f

Browse files
authored
Merge pull request #4289 from plotly/pr-image
image trace type
2 parents 07ef000 + e4cac27 commit 99fc02f

36 files changed

+1270
-6
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
@@ -23,6 +23,7 @@ Plotly.register([
2323
require('./violin'),
2424
require('./funnel'),
2525
require('./waterfall'),
26+
require('./image'),
2627

2728
require('./pie'),
2829
require('./sunburst'),

src/plots/cartesian/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ module.exports = {
6363
// Layers to keep trace types in the right order
6464
// N.B. each 'unique' plot method must have its own layer
6565
traceLayerClasses: [
66+
'imagelayer',
6667
'heatmaplayer',
6768
'contourcarpetlayer', 'contourlayer',
6869
'funnellayer', 'waterfalllayer', 'barlayer',

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/carpet/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
moduleType: 'trace',
2020
name: 'carpet',
2121
basePlotModule: require('../../plots/cartesian'),
22-
categories: ['cartesian', 'svg', 'carpet', 'carpetAxis', 'notLegendIsolatable', 'noMultiCategory', 'noHover'],
22+
categories: ['cartesian', 'svg', 'carpet', 'carpetAxis', 'notLegendIsolatable', 'noMultiCategory', 'noHover', 'noSortingByValue'],
2323
meta: {
2424
description: [
2525
'The data describing carpet axis layout is set in `y` and (optionally)',

src/traces/contourcarpet/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
moduleType: 'trace',
2020
name: 'contourcarpet',
2121
basePlotModule: require('../../plots/cartesian'),
22-
categories: ['cartesian', 'svg', 'carpet', 'contour', 'symbols', 'showLegend', 'hasLines', 'carpetDependent', 'noHover'],
22+
categories: ['cartesian', 'svg', 'carpet', 'contour', 'symbols', 'showLegend', 'hasLines', 'carpetDependent', 'noHover', 'noSortingByValue'],
2323
meta: {
2424
hrName: 'contour_carpet',
2525
description: [

src/traces/image/attributes.js

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 plotAttrs = require('../../plots/attributes');
12+
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
13+
var extendFlat = require('../../lib/extend').extendFlat;
14+
var colormodel = require('./constants').colormodel;
15+
16+
var cm = ['rgb', 'rgba', 'hsl', 'hsla'];
17+
var zminDesc = [];
18+
var zmaxDesc = [];
19+
for(var i = 0; i < cm.length; i++) {
20+
zminDesc.push('For the `' + cm[i] + '` colormodel, it is [' + colormodel[cm[i]].min.join(', ') + '].');
21+
zmaxDesc.push('For the `' + cm[i] + '` colormodel, it is [' + colormodel[cm[i]].max.join(', ') + '].');
22+
}
23+
24+
module.exports = extendFlat({
25+
z: {
26+
valType: 'data_array',
27+
role: 'info',
28+
editType: 'calc',
29+
description: [
30+
'A 2-dimensional array in which each element is an array of 3 or 4 numbers representing a color.',
31+
].join(' ')
32+
},
33+
colormodel: {
34+
valType: 'enumerated',
35+
values: cm,
36+
dflt: 'rgb',
37+
role: 'info',
38+
editType: 'plot',
39+
description: 'Color model used to map the numerical color components described in `z` into colors.'
40+
},
41+
zmin: {
42+
valType: 'info_array',
43+
items: [
44+
{valType: 'number', editType: 'plot'},
45+
{valType: 'number', editType: 'plot'},
46+
{valType: 'number', editType: 'plot'},
47+
{valType: 'number', editType: 'plot'}
48+
],
49+
role: 'info',
50+
editType: 'plot',
51+
description: [
52+
'Array defining the lower bound for each color component.',
53+
'Note that the default value will depend on the colormodel.',
54+
zminDesc.join(' ')
55+
].join(' ')
56+
},
57+
zmax: {
58+
valType: 'info_array',
59+
items: [
60+
{valType: 'number', editType: 'plot'},
61+
{valType: 'number', editType: 'plot'},
62+
{valType: 'number', editType: 'plot'},
63+
{valType: 'number', editType: 'plot'}
64+
],
65+
role: 'info',
66+
editType: 'plot',
67+
description: [
68+
'Array defining the higher bound for each color component.',
69+
'Note that the default value will depend on the colormodel.',
70+
zmaxDesc.join(' ')
71+
].join(' ')
72+
},
73+
x0: {
74+
valType: 'any',
75+
dflt: 0,
76+
role: 'info',
77+
editType: 'calc+clearAxisTypes',
78+
description: 'Set the image\'s x position.'
79+
},
80+
y0: {
81+
valType: 'any',
82+
dflt: 0,
83+
role: 'info',
84+
editType: 'calc+clearAxisTypes',
85+
description: 'Set the image\'s y position.'
86+
},
87+
dx: {
88+
valType: 'number',
89+
dflt: 1,
90+
role: 'info',
91+
editType: 'calc',
92+
description: 'Set the pixel\'s horizontal size.'
93+
},
94+
dy: {
95+
valType: 'number',
96+
dflt: 1,
97+
role: 'info',
98+
editType: 'calc',
99+
description: 'Set the pixel\'s vertical size'
100+
},
101+
text: {
102+
valType: 'data_array',
103+
editType: 'plot',
104+
description: 'Sets the text elements associated with each z value.'
105+
},
106+
hovertext: {
107+
valType: 'data_array',
108+
editType: 'plot',
109+
description: 'Same as `text`.'
110+
},
111+
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
112+
flags: ['x', 'y', 'z', 'color', 'name', 'text'],
113+
dflt: 'x+y+z+text+name'
114+
}),
115+
hovertemplate: hovertemplateAttrs({}, {
116+
keys: ['z', 'color', 'colormodel']
117+
})
118+
});

src/traces/image/calc.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 Axes = require('../../plots/cartesian/axes');
12+
var maxRowLength = require('../../lib').maxRowLength;
13+
14+
module.exports = function calc(gd, trace) {
15+
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
16+
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
17+
18+
var x0 = xa.d2c(trace.x0) - trace.dx / 2;
19+
var y0 = ya.d2c(trace.y0) - trace.dy / 2;
20+
var h = trace.z.length;
21+
var w = maxRowLength(trace.z);
22+
23+
// Set axis range
24+
var i;
25+
var xrange = [x0, x0 + w * trace.dx];
26+
var yrange = [y0, y0 + h * trace.dy];
27+
if(xa && xa.type === 'log') for(i = 0; i < w; i++) xrange.push(x0 + i * trace.dx);
28+
if(ya && ya.type === 'log') for(i = 0; i < h; i++) yrange.push(y0 + i * trace.dy);
29+
trace._extremes[xa._id] = Axes.findExtremes(xa, xrange);
30+
trace._extremes[ya._id] = Axes.findExtremes(ya, yrange);
31+
32+
var cd0 = {
33+
x0: x0,
34+
y0: y0,
35+
z: trace.z,
36+
w: w,
37+
h: h
38+
};
39+
return [cd0];
40+
};

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

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 || !z[0] || !z[0].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('text');
36+
coerce('hovertext');
37+
coerce('hovertemplate');
38+
};

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+
if('xVal' in pt) out.x = pt.xVal;
13+
if('yVal' in pt) out.y = pt.yVal;
14+
if(pt.xa) out.xaxis = pt.xa;
15+
if(pt.ya) out.yaxis = pt.ya;
16+
out.color = pt.color;
17+
out.colormodel = pt.trace.colormodel;
18+
return out;
19+
};

0 commit comments

Comments
 (0)