-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Feature: Plot layout images #520
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fa0e49b
Shapes/Images: Reorganize above/below/subplot layers
mdtusz 3319387
Images: Create image attributes and defaults
mdtusz dd15bfd
Images: Add draw method
mdtusz b94065f
Images: Call Images.draw in plot step and while dragging
mdtusz 00742e8
Images: Add mock
mdtusz cc742fa
Images: Add jasmine tests
mdtusz 7b2859d
Images: Add baseline test images
8e75905
Shapes: Update shapes tests for reorganized layers
mdtusz 83592c1
Images: Return promise for image loading
mdtusz 466a770
Images: Reject non-array images attribute
mdtusz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Images: Add jasmine tests
- Loading branch information
commit cc742faf74d2a4504ccaea34e44c8084d3bb0698
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| var Plotly = require('@lib/index'); | ||
| var Images = require('@src/components/images'); | ||
| var createGraphDiv = require('../assets/create_graph_div'); | ||
| var destroyGraphDiv = require('../assets/destroy_graph_div'); | ||
| var mouseEvent = require('../assets/mouse_event'); | ||
|
|
||
| describe('Layout images', function() { | ||
|
|
||
| describe('supplyLayoutDefaults', function() { | ||
|
|
||
| var layoutIn, | ||
| layoutOut; | ||
|
|
||
| beforeEach(function() { | ||
| layoutIn = { images: [] }; | ||
| layoutOut = {}; | ||
| }); | ||
|
|
||
| it('should reject when there is no `source`', function() { | ||
| layoutIn.images[0] = { opacity: 0.5, width: 0.2, height: 0.2 }; | ||
|
|
||
| Images.supplyLayoutDefaults(layoutIn, layoutOut); | ||
|
|
||
| expect(layoutOut.images.length).toEqual(0); | ||
| }); | ||
|
|
||
| it('should coerce the correct defaults', function() { | ||
| layoutIn.images[0] = { source: 'http://www.someimagesource.com' }; | ||
|
|
||
| var expected = { | ||
| source: 'http://www.someimagesource.com', | ||
| layer: 'above', | ||
| x: 0, | ||
| y: 0, | ||
| xanchor: 'left', | ||
| yanchor: 'top', | ||
| width: 0, | ||
| height: 0, | ||
| sizing: 'contain', | ||
| opacity: 1, | ||
| xref: 'paper', | ||
| yref: 'paper' | ||
| }; | ||
|
|
||
| Images.supplyLayoutDefaults(layoutIn, layoutOut); | ||
|
|
||
| expect(layoutOut.images[0]).toEqual(expected); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| describe('drawing', function() { | ||
|
|
||
| var gd, | ||
| data = [{ x: [1,2,3], y: [1,2,3] }]; | ||
|
|
||
| beforeEach(function() { | ||
| gd = createGraphDiv(); | ||
| }); | ||
|
|
||
| afterEach(destroyGraphDiv); | ||
|
|
||
| it('should draw images on the right layers', function() { | ||
|
|
||
| var layer; | ||
|
|
||
| Plotly.plot(gd, data, { images: [{ | ||
| source: 'imageabove', | ||
| layer: 'above' | ||
| }]}); | ||
|
|
||
| layer = gd._fullLayout._imageUpperLayer; | ||
| expect(layer.length).toBe(1); | ||
|
|
||
| destroyGraphDiv(); | ||
| gd = createGraphDiv(); | ||
| Plotly.plot(gd, data, { images: [{ | ||
| source: 'imagebelow', | ||
| layer: 'below' | ||
| }]}); | ||
|
|
||
| layer = gd._fullLayout._imageLowerLayer; | ||
| expect(layer.length).toBe(1); | ||
|
|
||
| destroyGraphDiv(); | ||
| gd = createGraphDiv(); | ||
| Plotly.plot(gd, data, { images: [{ | ||
| source: 'imagesubplot', | ||
| layer: 'below', | ||
| xref: 'x', | ||
| yref: 'y' | ||
| }]}); | ||
|
|
||
| layer = gd._fullLayout._imageSubplotLayer; | ||
| expect(layer.length).toBe(1); | ||
| }); | ||
|
|
||
| describe('with anchors and sizing', function() { | ||
|
|
||
| function testAspectRatio(xAnchor, yAnchor, sizing, expected) { | ||
| var anchorName = xAnchor + yAnchor; | ||
| Plotly.plot(gd, data, { images: [{ | ||
| source: anchorName, | ||
| xanchor: xAnchor, | ||
| yanchor: yAnchor, | ||
| sizing: sizing | ||
| }]}); | ||
|
|
||
| var image = Plotly.d3.select('[href="' + anchorName + '"]'), | ||
| parValue = image.attr('preserveAspectRatio'); | ||
|
|
||
| expect(parValue).toBe(expected); | ||
| } | ||
|
|
||
| it('should work for center middle', function() { | ||
| testAspectRatio('center', 'middle', undefined, 'xMidYMid'); | ||
| }); | ||
|
|
||
| it('should work for left top', function() { | ||
| testAspectRatio('left', 'top', undefined, 'xMinYMin'); | ||
| }); | ||
|
|
||
| it('should work for right bottom', function() { | ||
| testAspectRatio('right', 'bottom', undefined, 'xMaxYMax'); | ||
| }); | ||
|
|
||
| it('should work for stretch sizing', function() { | ||
| testAspectRatio('middle', 'center', 'stretch', 'none'); | ||
| }); | ||
|
|
||
| it('should work for fill sizing', function() { | ||
| testAspectRatio('invalid', 'invalid', 'fill', 'xMinYMin slice'); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| }); | ||
|
|
||
| describe('when the plot is dragged', function() { | ||
| var gd, | ||
| data = [{ x: [1,2,3], y: [1,2,3] }]; | ||
|
|
||
| beforeEach(function() { | ||
| gd = createGraphDiv(); | ||
| }); | ||
|
|
||
| afterEach(destroyGraphDiv); | ||
|
|
||
| it('should not move when referencing the paper', function(done) { | ||
| var source = 'http://www.placekitten.com/200', | ||
| image = { | ||
| source: source, | ||
| xref: 'paper', | ||
| yref: 'paper', | ||
| x: 0, | ||
| y: 0, | ||
| width: 0.1, | ||
| height: 0.1 | ||
| }; | ||
|
|
||
| Plotly.plot(gd, data, { | ||
| images: [image], | ||
| dragmode: 'pan', | ||
| width: 600, | ||
| height: 400 | ||
| }).then(function() { | ||
| var img = Plotly.d3.select('[href="' + source + '"]').node(), | ||
| oldPos = img.getBoundingClientRect(); | ||
|
|
||
| mouseEvent('mousedown', 250, 200); | ||
| mouseEvent('mousemove', 300, 250); | ||
|
|
||
| var newPos = img.getBoundingClientRect(); | ||
|
|
||
| expect(newPos.left).toBe(oldPos.left); | ||
| expect(newPos.top).toBe(oldPos.top); | ||
|
|
||
| mouseEvent('mouseup', 300, 250); | ||
| }).then(done); | ||
| }); | ||
|
|
||
| it('should move when referencing axes', function(done) { | ||
| var source = 'http://www.placekitten.com/200', | ||
| image = { | ||
| source: source, | ||
| xref: 'x', | ||
| yref: 'y', | ||
| x: 2, | ||
| y: 2, | ||
| width: 1, | ||
| height: 1 | ||
| }; | ||
|
|
||
| Plotly.plot(gd, data, { | ||
| images: [image], | ||
| dragmode: 'pan', | ||
| width: 600, | ||
| height: 400 | ||
| }).then(function() { | ||
| var img = Plotly.d3.select('[href="' + source + '"]').node(), | ||
| oldPos = img.getBoundingClientRect(); | ||
|
|
||
| mouseEvent('mousedown', 250, 200); | ||
| mouseEvent('mousemove', 300, 250); | ||
|
|
||
| var newPos = img.getBoundingClientRect(); | ||
|
|
||
| expect(newPos.left).toBe(oldPos.left + 50); | ||
| expect(newPos.top).toBe(oldPos.top + 50); | ||
|
|
||
| mouseEvent('mouseup', 300, 250); | ||
| }).then(done); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great tests so far.
Let's make sure that
relayoutworks as expected in a similar way toshapes.