Skip to content
Open
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
1 change: 1 addition & 0 deletions draftlogs/7963_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add an opt-in modebar button for downloading Plotly figures as JSON [[#7963](https://github.com/plotly/plotly.js/pull/7963)]
12 changes: 12 additions & 0 deletions src/components/modebar/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ modeBarButtons.toImage = {
}
};

modeBarButtons.downloadJson = {
name: 'downloadJson',
title: function(gd) { return _(gd, 'Download plot as JSON'); },
icon: Icons.disk,
click: function(gd) {
Registry.call('downloadImage', gd, {format: 'full-json'})
.catch(function() {
Lib.notifier(_(gd, 'Sorry, there was a problem downloading your JSON file!'), 'long', gd);
});
}
};

modeBarButtons.sendChartToCloud = {
name: 'sendChartToCloud',
title: function(gd) { return _(gd, 'Share with Plotly Cloud'); },
Expand Down
2 changes: 1 addition & 1 deletion src/components/modebar/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var backButtons = [
'hovercompare',
'togglehover',
'togglespikelines'
].concat(DRAW_MODES);
].concat(DRAW_MODES, ['downloadJson']);

var foreButtons = [];
var addToForeButtons = function(b) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/modebar/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ function getButtonGroups(gd) {
enableHover('hoverClosestGeo');
enableHover('hoverClosest3d');
enableHover('hoverClosestPie');
} else if(b === 'downloadjson') {
newList.push(modeBarButtons.downloadJson);
}
} else newList.push(b);
}
Expand Down
3 changes: 2 additions & 1 deletion src/plot_api/plot_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ var configAttributes = {
'To enable predefined modebar buttons e.g. shape drawing, hover and spikelines,',
'simply provide their string name(s). This could include:',
'*v1hovermode*, *hoverclosest*, *hovercompare*, *togglehover*, *togglespikelines*,',
'*drawline*, *drawopenpath*, *drawclosedpath*, *drawcircle*, *drawrect* and *eraseshape*.',
'*drawline*, *drawopenpath*, *drawclosedpath*, *drawcircle*, *drawrect*, *eraseshape*',
'and *downloadJson*.',
'Please note that these predefined buttons will only be shown if they are compatible',
'with all trace types used in a graph.'
].join(' ')
Expand Down
21 changes: 21 additions & 0 deletions test/jasmine/tests/modebar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,27 @@ describe('ModeBar', function() {
});
});

describe('downloadJson handler', function() {
beforeEach(function() {
spyOn(Registry, 'call').and.callFake(function() {
return Promise.resolve();
});
gd = createGraphDiv();
});

it('should request a full JSON download', function(done) {
Plotly.newPlot(gd, {data: [], layout: {}, config: {
modeBarButtonsToAdd: ['downloadJson']
}})
.then(function() {
selectButton(gd._fullLayout._modeBar, 'downloadJson').click();
expect(Registry.call)
.toHaveBeenCalledWith('downloadImage', gd, {format: 'full-json'});
})
.then(done, done.fail);
});
});

describe('cartesian handlers', function() {
beforeEach(function(done) {
var mockData = [{
Expand Down
4 changes: 2 additions & 2 deletions test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
"valType": "any"
},
"modeBarButtonsToAdd": {
"description": "Add mode bar button using config objects See ./components/modebar/buttons.js for list of arguments. To enable predefined modebar buttons e.g. shape drawing, hover and spikelines, simply provide their string name(s). This could include: *v1hovermode*, *hoverclosest*, *hovercompare*, *togglehover*, *togglespikelines*, *drawline*, *drawopenpath*, *drawclosedpath*, *drawcircle*, *drawrect* and *eraseshape*. Please note that these predefined buttons will only be shown if they are compatible with all trace types used in a graph.",
"description": "Add mode bar button using config objects See ./components/modebar/buttons.js for list of arguments. To enable predefined modebar buttons e.g. shape drawing, hover and spikelines, simply provide their string name(s). This could include: *v1hovermode*, *hoverclosest*, *hovercompare*, *togglehover*, *togglespikelines*, *drawline*, *drawopenpath*, *drawclosedpath*, *drawcircle*, *drawrect*, *eraseshape* and *downloadJson*. Please note that these predefined buttons will only be shown if they are compatible with all trace types used in a graph.",
"dflt": [],
"valType": "any"
},
Expand Down Expand Up @@ -4606,7 +4606,7 @@
},
"add": {
"arrayOk": true,
"description": "Determines which predefined modebar buttons to add. Please note that these buttons will only be shown if they are compatible with all trace types used in a graph. Similar to `config.modeBarButtonsToAdd` option. This may include *v1hovermode*, *hoverclosest*, *hovercompare*, *togglehover*, *togglespikelines*, *drawline*, *drawopenpath*, *drawclosedpath*, *drawcircle*, *drawrect*, *eraseshape*.",
"description": "Determines which predefined modebar buttons to add. Please note that these buttons will only be shown if they are compatible with all trace types used in a graph. Similar to `config.modeBarButtonsToAdd` option. This may include *v1hovermode*, *hoverclosest*, *hovercompare*, *togglehover*, *togglespikelines*, *drawline*, *drawopenpath*, *drawclosedpath*, *drawcircle*, *drawrect*, *eraseshape*, *downloadJson*.",
"dflt": "",
"editType": "modebar",
"valType": "string"
Expand Down