Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 971e3b5

Browse files
committed
add toImage *scale* tests and 📚
1 parent f58c670 commit 971e3b5

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/plot_api/to_image.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ var attrs = {
4545
min: 0,
4646
dflt: 1,
4747
description: [
48-
'...'
48+
'Sets a scaling for the generated image.',
49+
'If set, all features of a graphs (e.g. text, line width)',
50+
'are scaled, unlike simply setting',
51+
'a bigger *width* and *height*.'
4952
].join(' ')
5053
},
5154
setBackground: {
@@ -137,7 +140,6 @@ function toImage(gd, opts) {
137140
// extend config for static plot
138141
var configImage = Lib.extendFlat({}, config, {
139142
staticPlot: true,
140-
plotGlPixelRatio: config.plotGlPixelRatio || 2,
141143
setBackground: setBackground
142144
});
143145

test/jasmine/tests/snapshot_test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,25 @@ describe('Plotly.Snapshot', function() {
301301
.catch(fail)
302302
.then(done);
303303
});
304+
305+
it('should adapt *viewBox* attribute under *scale* option', function(done) {
306+
Plotly.plot(gd, [{
307+
y: [1, 2, 1]
308+
}], {
309+
width: 300,
310+
height: 400
311+
})
312+
.then(function() {
313+
var str = Plotly.Snapshot.toSVG(gd, 'svg', 2.5);
314+
var dom = parser.parseFromString(str, 'image/svg+xml');
315+
var el = dom.getElementsByTagName('svg')[0];
316+
317+
expect(el.getAttribute('width')).toBe('750', 'width');
318+
expect(el.getAttribute('height')).toBe('1000', 'height');
319+
expect(el.getAttribute('viewBox')).toBe('0 0 300 400', 'viewbox');
320+
})
321+
.catch(fail)
322+
.then(done);
323+
});
304324
});
305325
});

test/jasmine/tests/toimage_test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var fail = require('../assets/fail_test');
77
var customMatchers = require('../assets/custom_matchers');
88
var subplotMock = require('@mocks/multiple_subplots.json');
99

10+
var FORMATS = ['png', 'jpeg', 'webp', 'svg'];
11+
1012
describe('Plotly.toImage', function() {
1113
'use strict';
1214

@@ -31,6 +33,19 @@ describe('Plotly.toImage', function() {
3133
});
3234
}
3335

36+
function assertSize(url, width, height) {
37+
return new Promise(function(resolve, reject) {
38+
var img = new Image();
39+
img.onload = function() {
40+
expect(img.width).toBe(width, 'image width');
41+
expect(img.height).toBe(height, 'image height');
42+
resolve(url);
43+
};
44+
img.onerror = reject;
45+
img.src = url;
46+
});
47+
}
48+
3449
it('should be attached to Plotly', function() {
3550
expect(Plotly.toImage).toBeDefined();
3651
});
@@ -109,18 +124,22 @@ describe('Plotly.toImage', function() {
109124

110125
Plotly.plot(gd, fig.data, fig.layout)
111126
.then(function() { return Plotly.toImage(gd, {format: 'png'}); })
127+
.then(function(url) { return assertSize(url, 700, 450); })
112128
.then(function(url) {
113129
expect(url.split('png')[0]).toBe('data:image/');
114130
})
115131
.then(function() { return Plotly.toImage(gd, {format: 'jpeg'}); })
132+
.then(function(url) { return assertSize(url, 700, 450); })
116133
.then(function(url) {
117134
expect(url.split('jpeg')[0]).toBe('data:image/');
118135
})
119136
.then(function() { return Plotly.toImage(gd, {format: 'svg'}); })
137+
.then(function(url) { return assertSize(url, 700, 450); })
120138
.then(function(url) {
121139
expect(url.split('svg')[0]).toBe('data:image/');
122140
})
123141
.then(function() { return Plotly.toImage(gd, {format: 'webp'}); })
142+
.then(function(url) { return assertSize(url, 700, 450); })
124143
.then(function(url) {
125144
expect(url.split('webp')[0]).toBe('data:image/');
126145
})
@@ -156,6 +175,20 @@ describe('Plotly.toImage', function() {
156175
.then(done);
157176
});
158177

178+
FORMATS.forEach(function(f) {
179+
it('should respond to *scale* option ( format ' + f + ')', function(done) {
180+
var fig = Lib.extendDeep({}, subplotMock);
181+
182+
Plotly.plot(gd, fig.data, fig.layout)
183+
.then(function() { return Plotly.toImage(gd, {format: f, scale: 2}); })
184+
.then(function(url) { return assertSize(url, 1400, 900); })
185+
.then(function() { return Plotly.toImage(gd, {format: f, scale: 0.5}); })
186+
.then(function(url) { return assertSize(url, 350, 225); })
187+
.catch(fail)
188+
.then(done);
189+
});
190+
});
191+
159192
it('should accept data/layout/config figure object as input', function(done) {
160193
var fig = Lib.extendDeep({}, subplotMock);
161194

0 commit comments

Comments
 (0)