Skip to content

Commit 0176770

Browse files
Add JSON download modebar button
1 parent 21da158 commit 0176770

6 files changed

Lines changed: 40 additions & 4 deletions

File tree

src/components/modebar/buttons.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ modeBarButtons.toImage = {
6868
}
6969
};
7070

71+
modeBarButtons.downloadJson = {
72+
name: 'downloadJson',
73+
title: function(gd) { return _(gd, 'Download plot as JSON'); },
74+
icon: Icons.disk,
75+
click: function(gd) {
76+
Registry.call('downloadImage', gd, {format: 'full-json'})
77+
.catch(function() {
78+
Lib.notifier(_(gd, 'Sorry, there was a problem downloading your JSON file!'), 'long', gd);
79+
});
80+
}
81+
};
82+
7183
modeBarButtons.sendChartToCloud = {
7284
name: 'sendChartToCloud',
7385
title: function(gd) { return _(gd, 'Share with Plotly Cloud'); },

src/components/modebar/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var backButtons = [
1818
'hovercompare',
1919
'togglehover',
2020
'togglespikelines'
21-
].concat(DRAW_MODES);
21+
].concat(DRAW_MODES, ['downloadJson']);
2222

2323
var foreButtons = [];
2424
var addToForeButtons = function(b) {

src/components/modebar/manage.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ function getButtonGroups(gd) {
258258
enableHover('hoverClosestGeo');
259259
enableHover('hoverClosest3d');
260260
enableHover('hoverClosestPie');
261+
} else if(b === 'downloadjson') {
262+
newList.push(modeBarButtons.downloadJson);
261263
}
262264
} else newList.push(b);
263265
}

src/plot_api/plot_config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ var configAttributes = {
303303
'To enable predefined modebar buttons e.g. shape drawing, hover and spikelines,',
304304
'simply provide their string name(s). This could include:',
305305
'*v1hovermode*, *hoverclosest*, *hovercompare*, *togglehover*, *togglespikelines*,',
306-
'*drawline*, *drawopenpath*, *drawclosedpath*, *drawcircle*, *drawrect* and *eraseshape*.',
306+
'*drawline*, *drawopenpath*, *drawclosedpath*, *drawcircle*, *drawrect*, *eraseshape*',
307+
'and *downloadJson*.',
307308
'Please note that these predefined buttons will only be shown if they are compatible',
308309
'with all trace types used in a graph.'
309310
].join(' ')

test/jasmine/tests/modebar_test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,27 @@ describe('ModeBar', function() {
11021102
});
11031103
});
11041104

1105+
describe('downloadJson handler', function() {
1106+
beforeEach(function() {
1107+
spyOn(Registry, 'call').and.callFake(function() {
1108+
return Promise.resolve();
1109+
});
1110+
gd = createGraphDiv();
1111+
});
1112+
1113+
it('should request a full JSON download', function(done) {
1114+
Plotly.newPlot(gd, {data: [], layout: {}, config: {
1115+
modeBarButtonsToAdd: ['downloadJson']
1116+
}})
1117+
.then(function() {
1118+
selectButton(gd._fullLayout._modeBar, 'downloadJson').click();
1119+
expect(Registry.call)
1120+
.toHaveBeenCalledWith('downloadImage', gd, {format: 'full-json'});
1121+
})
1122+
.then(done, done.fail);
1123+
});
1124+
});
1125+
11051126
describe('cartesian handlers', function() {
11061127
beforeEach(function(done) {
11071128
var mockData = [{

test/plot-schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@
255255
"valType": "any"
256256
},
257257
"modeBarButtonsToAdd": {
258-
"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.",
258+
"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.",
259259
"dflt": [],
260260
"valType": "any"
261261
},
@@ -4606,7 +4606,7 @@
46064606
},
46074607
"add": {
46084608
"arrayOk": true,
4609-
"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*.",
4609+
"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*.",
46104610
"dflt": "",
46114611
"editType": "modebar",
46124612
"valType": "string"

0 commit comments

Comments
 (0)