Skip to content

Commit 7752eb4

Browse files
committed
add Plotly.update method:
- reuse flag-finding methods called in restyle & relayout - combine flags into subroutine sequences
1 parent 822cf8c commit 7752eb4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/core.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ exports.newPlot = Plotly.newPlot;
3232
exports.restyle = Plotly.restyle;
3333
exports.relayout = Plotly.relayout;
3434
exports.redraw = Plotly.redraw;
35+
exports.update = Plotly.update;
3536
exports.extendTraces = Plotly.extendTraces;
3637
exports.prependTraces = Plotly.prependTraces;
3738
exports.addTraces = Plotly.addTraces;

src/plot_api/plot_api.js

+61
Original file line numberDiff line numberDiff line change
@@ -2069,24 +2069,85 @@ function _relayout(gd, aobj) {
20692069
};
20702070
}
20712071

2072+
/**
2073+
* update: update trace and layout attributes of an existing plot
2074+
*
2075+
* @param {string id or DOM element} gd
2076+
* the id or DOM element of the graph container div
2077+
* @param {object} traceUpdate
2078+
* attribute object `{astr1: val1, astr2: val2 ...}`
2079+
* corresponding to updates in the plot's traces
2080+
* @param {object} layoutUpdate
2081+
* attribute object `{astr1: val1, astr2: val2 ...}`
2082+
* corresponding to updates in the plot's layout
2083+
* @param {number or array} traces (optional)
2084+
* integer or array of integers for the traces to alter (all if omitted)
2085+
*
2086+
*/
2087+
Plotly.update = function update(gd, traceUpdate, layoutUpdate, indices) {
2088+
gd = helpers.getGraphDiv(gd);
2089+
helpers.clearPromiseQueue(gd);
20722090

2091+
if(gd.framework && gd.framework.isPolar) {
2092+
return Promise.resolve(gd);
20732093
}
20742094

2095+
if(!Lib.isPlainObject(traceUpdate)) traceUpdate = {};
2096+
if(!Lib.isPlainObject(layoutUpdate)) layoutUpdate = {};
20752097

2098+
if(Object.keys(traceUpdate).length) gd.changed = true;
2099+
if(Object.keys(layoutUpdate).length) gd.changed = true;
20762100

2101+
var restyleSpecs = _restyle(gd, traceUpdate, indices),
2102+
restyleFlags = restyleSpecs.flags;
20772103

2104+
var relayoutSpecs = _relayout(gd, layoutUpdate),
2105+
relayoutFlags = relayoutSpecs.flags;
20782106

2107+
// clear calcdata if required
2108+
if(restyleFlags.clearCalc || relayoutFlags.docalc) gd.calcdata = undefined;
20792109

2110+
// fill in redraw sequence
2111+
var seq = [];
20802112

2113+
if(restyleFlags.fullReplot && relayoutFlags.layoutReplot) {
2114+
var layout = gd.layout;
2115+
gd.layout = undefined;
2116+
seq.push(function() { return Plotly.plot(gd, gd.data, layout); });
2117+
}
2118+
else if(restyleFlags.fullReplot) {
2119+
seq.push(Plotly.plot);
2120+
}
2121+
else if(relayoutFlags.layoutReplot) {
2122+
seq.push(subroutines.layoutReplot);
20812123
}
2124+
else {
2125+
seq.push(Plots.previousPromises);
2126+
Plots.supplyDefaults(gd);
20822127

2128+
if(restyleFlags.dostyle) seq.push(subroutines.doTraceStyle);
2129+
if(restyleFlags.docolorbars) seq.push(subroutines.doColorBars);
2130+
if(relayoutFlags.dolegend) seq.push(subroutines.doLegend);
2131+
if(relayoutFlags.dolayoutstyle) seq.push(subroutines.layoutStyles);
2132+
if(relayoutFlags.doticks) seq.push(subroutines.doTicksRelayout);
2133+
if(relayoutFlags.domodebar) seq.push(subroutines.doModeBar);
20832134
}
20842135

2136+
Queue.add(gd,
2137+
update, [gd, restyleSpecs.undoit, relayoutSpecs.undoit, restyleSpecs.traces],
2138+
update, [gd, restyleSpecs.redoit, relayoutSpecs.redoit, restyleSpecs.traces]
2139+
);
20852140

2141+
var plotDone = Lib.syncOrAsync(seq, gd);
20862142
if(!plotDone || !plotDone.then) plotDone = Promise.resolve(gd);
20872143

20882144
return plotDone.then(function() {
2145+
subroutines.setRangeSliderRange(gd, relayoutSpecs.eventData);
20892146

2147+
gd.emit('plotly_update', {
2148+
data: restyleSpecs.eventData,
2149+
layout: relayoutSpecs.eventData
2150+
});
20902151

20912152
return gd;
20922153
});

0 commit comments

Comments
 (0)