Skip to content

Add support for div id as 1st arg to Plotly.makeTemplate #3375

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 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/plot_api/template_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ var dfltConfig = require('./plot_config');
* Note: separated from the rest of templates because otherwise we get circular
* references due to PlotSchema.
*
* @param {object|DOM element} figure: The figure to base the template on
* @param {object|DOM element|string} figure: The figure to base the template on
* should contain a trace array `figure.data`
* and a layout object `figure.layout`
* @returns {object} template: the extracted template - can then be used as
* `layout.template` in another figure.
*/
exports.makeTemplate = function(figure) {
figure = Lib.isPlainObject(figure) ? figure : Lib.getGraphDiv(figure);
figure = Lib.extendDeep({_context: dfltConfig}, {data: figure.data, layout: figure.layout});
Plots.supplyDefaults(figure);
var data = figure.data || [];
Expand Down
34 changes: 34 additions & 0 deletions test/jasmine/tests/template_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,40 @@ describe('makeTemplate', function() {
.then(destroyGraphDiv)
.then(done);
});

it('works with div id', function(done) {
var mock = Lib.extendDeep({}, scatterFillMock);

var gd = document.createElement('div');
gd.id = 'myDiv';
document.body.appendChild(gd);

Plotly.newPlot('myDiv', mock)
.then(function() {
var template = Plotly.makeTemplate('myDiv');
delete(template.layout.xaxis);
delete(template.layout.yaxis);
expect(template).toEqual({
data: {scatter: [
{fill: 'tonext', line: {shape: 'spline'}},
{fill: 'tonext'},
{fill: 'toself'}
] },
layout: {
title: {
text: 'Fill toself and tonext'
},
width: 400,
height: 400
}
});
})
.catch(failTest)
.then(function() {
document.body.removeChild(gd);
})
.then(done);
});
});

// statics of template application are all covered by the template mock
Expand Down