From 9094f2a53ed902e4b0f8db1e434c04bc25e7ea12 Mon Sep 17 00:00:00 2001 From: drderiv Date: Mon, 24 Jul 2023 11:44:59 -0700 Subject: [PATCH 1/8] Add optional redraw parameter to extendTraces/prependTraces An optional parameter has been added to extendTraces and prependTraces that specifies whether the graph should be redrawn after a new group of traces has been appended or prepended, as the case may be. This can be useful when the time to redraw cannot keep up with the frequency in which new data arrives, or when data arrives so fast that updating the graph with each trace addition is distracting. Importantly, this change is backward compatible with any prior version, as the optional redraw parameter defaults to true, thus, unless explicitly set to false, graphs will continue to be redrawn after each trace addition. --- src/plot_api/plot_api.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 7088f08eb3d..f3397987b1e 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -883,10 +883,11 @@ function concatTypedArray(arr0, arr1) { * @param {Object|HTMLDivElement} gd The graph div * @param {Object} update The key:array map of target attributes to extend * @param {Number|Number[]} indices The locations of traces to be extended - * @param {Number|Object} [maxPoints] Number of points for trace window after lengthening. + * @param {Number|Object} [maxPoints] Number of points for trace window after lengthening + * @param {Boolean} [redrawGraph] Redraw the graph after adding traces * */ -function extendTraces(gd, update, indices, maxPoints) { +function extendTraces(gd, update, indices, maxPoints, redrawGraph = true) { gd = Lib.getGraphDiv(gd); function updateArray(target, insert, maxp) { @@ -937,14 +938,14 @@ function extendTraces(gd, update, indices, maxPoints) { } var undo = spliceTraces(gd, update, indices, maxPoints, updateArray); - var promise = exports.redraw(gd); + var promise = redrawGraph ? exports.redraw(gd) : gd; var undoArgs = [gd, undo.update, indices, undo.maxPoints]; Queue.add(gd, exports.prependTraces, undoArgs, extendTraces, arguments); return promise; } -function prependTraces(gd, update, indices, maxPoints) { +function prependTraces(gd, update, indices, maxPoints, redrawGraph = true) { gd = Lib.getGraphDiv(gd); function updateArray(target, insert, maxp) { @@ -994,7 +995,7 @@ function prependTraces(gd, update, indices, maxPoints) { } var undo = spliceTraces(gd, update, indices, maxPoints, updateArray); - var promise = exports.redraw(gd); + var promise = redrawGraph ? exports.redraw(gd) : gd; var undoArgs = [gd, undo.update, indices, undo.maxPoints]; Queue.add(gd, exports.extendTraces, undoArgs, prependTraces, arguments); From 674ffbd27a9aebbcd0cd5606c735edea9f7443c5 Mon Sep 17 00:00:00 2001 From: drderiv Date: Mon, 24 Jul 2023 11:52:38 -0700 Subject: [PATCH 2/8] Create 6682_add.md --- draftlog/6682_add.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 draftlog/6682_add.md diff --git a/draftlog/6682_add.md b/draftlog/6682_add.md new file mode 100644 index 00000000000..21d72e34da1 --- /dev/null +++ b/draftlog/6682_add.md @@ -0,0 +1 @@ +Add optional redrawGraph parameter to extendTraces/prependTraces [[#6682](https://github.com/plotly/plotly.js/pull/6682)] From 87863477b04cad74c2b65efca4fe8187767f5447 Mon Sep 17 00:00:00 2001 From: drderiv Date: Mon, 24 Jul 2023 12:59:51 -0700 Subject: [PATCH 3/8] Fix syntax of prior change to meet requirements --- src/plot_api/plot_api.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index f3397987b1e..92c6c2f6717 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -887,7 +887,8 @@ function concatTypedArray(arr0, arr1) { * @param {Boolean} [redrawGraph] Redraw the graph after adding traces * */ -function extendTraces(gd, update, indices, maxPoints, redrawGraph = true) { +function extendTraces(gd, update, indices, maxPoints, redrawGraph) { + redrawGraph = typeof redrawGraph !== "undefined" ? redrawGraph : true; gd = Lib.getGraphDiv(gd); function updateArray(target, insert, maxp) { @@ -945,7 +946,8 @@ function extendTraces(gd, update, indices, maxPoints, redrawGraph = true) { return promise; } -function prependTraces(gd, update, indices, maxPoints, redrawGraph = true) { +function prependTraces(gd, update, indices, maxPoints, redrawGraph) { + redrawGraph = typeof redrawGraph !== "undefined" ? redrawGraph : true; gd = Lib.getGraphDiv(gd); function updateArray(target, insert, maxp) { From ca3a5117ea8a657a02772ea82d650f0cc96214ec Mon Sep 17 00:00:00 2001 From: drderiv Date: Mon, 24 Jul 2023 13:39:25 -0700 Subject: [PATCH 4/8] Fix syntax of prior change to meet requirements (again) --- src/plot_api/plot_api.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 92c6c2f6717..4c7bd1e0c5c 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -888,7 +888,6 @@ function concatTypedArray(arr0, arr1) { * */ function extendTraces(gd, update, indices, maxPoints, redrawGraph) { - redrawGraph = typeof redrawGraph !== "undefined" ? redrawGraph : true; gd = Lib.getGraphDiv(gd); function updateArray(target, insert, maxp) { @@ -939,7 +938,7 @@ function extendTraces(gd, update, indices, maxPoints, redrawGraph) { } var undo = spliceTraces(gd, update, indices, maxPoints, updateArray); - var promise = redrawGraph ? exports.redraw(gd) : gd; + var promise = typeof redrawGraph === 'undefined' || redrawGraph ? exports.redraw(gd) : gd; var undoArgs = [gd, undo.update, indices, undo.maxPoints]; Queue.add(gd, exports.prependTraces, undoArgs, extendTraces, arguments); @@ -947,7 +946,6 @@ function extendTraces(gd, update, indices, maxPoints, redrawGraph) { } function prependTraces(gd, update, indices, maxPoints, redrawGraph) { - redrawGraph = typeof redrawGraph !== "undefined" ? redrawGraph : true; gd = Lib.getGraphDiv(gd); function updateArray(target, insert, maxp) { @@ -997,7 +995,7 @@ function prependTraces(gd, update, indices, maxPoints, redrawGraph) { } var undo = spliceTraces(gd, update, indices, maxPoints, updateArray); - var promise = redrawGraph ? exports.redraw(gd) : gd; + var promise = typeof redrawGraph === 'undefined' || redrawGraph ? exports.redraw(gd) : gd; var undoArgs = [gd, undo.update, indices, undo.maxPoints]; Queue.add(gd, exports.extendTraces, undoArgs, prependTraces, arguments); From ac2cf820214f588a2b2cdd0828a24878d228fefe Mon Sep 17 00:00:00 2001 From: drderiv Date: Mon, 31 Jul 2023 12:39:32 -0700 Subject: [PATCH 5/8] Update plot_config.js Add redrawMinimumInterval parameter to config for use with extendTraces/prependTraces. Fully backwards compatible as default is zero. --- src/plot_api/plot_config.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/plot_api/plot_config.js b/src/plot_api/plot_config.js index 577710ac3ac..1e2cf5aa5d9 100644 --- a/src/plot_api/plot_config.js +++ b/src/plot_api/plot_config.js @@ -438,6 +438,13 @@ var configAttributes = { ].join(' ') }, + redrawMinimumInterval: { + valType: 'integer', + min: 0, + dflt: 0, + description: 'Sets the minimum redraw interval in ms for use with extendTraces/prependTraces.' + }, + locale: { valType: 'string', dflt: 'en-US', From 1b22d022f75e8ac72b525475521106c2fffa296a Mon Sep 17 00:00:00 2001 From: drderiv Date: Mon, 31 Jul 2023 12:39:36 -0700 Subject: [PATCH 6/8] Update plot_api.js Revert previous changes and attempt to implement suggestion of @alexcjohnson to use throttle functionality for extendTraces/prependTraces minimum redraw interval. --- src/plot_api/plot_api.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 1562e1c923a..38894399d07 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -884,10 +884,9 @@ function concatTypedArray(arr0, arr1) { * @param {Object} update The key:array map of target attributes to extend * @param {Number|Number[]} indices The locations of traces to be extended * @param {Number|Object} [maxPoints] Number of points for trace window after lengthening - * @param {Boolean} [redrawGraph] Redraw the graph after adding traces * */ -function extendTraces(gd, update, indices, maxPoints, redrawGraph) { +function extendTraces(gd, update, indices, maxPoints) { gd = Lib.getGraphDiv(gd); function updateArray(target, insert, maxp) { @@ -938,14 +937,23 @@ function extendTraces(gd, update, indices, maxPoints, redrawGraph) { } var undo = spliceTraces(gd, update, indices, maxPoints, updateArray); - var promise = typeof redrawGraph === 'undefined' || redrawGraph ? exports.redraw(gd) : gd; + var promise = gd; + if (gd._context.redrawMin > 0) { + Lib.throttle( + gd._fullLayout._uid + '-redraw', + gd._context.redrawMinimumInterval, + function() { promise = exports.redraw(gd); } + ); + } else { + promise = exports.redraw(gd); + } var undoArgs = [gd, undo.update, indices, undo.maxPoints]; Queue.add(gd, exports.prependTraces, undoArgs, extendTraces, arguments); return promise; } -function prependTraces(gd, update, indices, maxPoints, redrawGraph) { +function prependTraces(gd, update, indices, maxPoints) { gd = Lib.getGraphDiv(gd); function updateArray(target, insert, maxp) { @@ -995,7 +1003,16 @@ function prependTraces(gd, update, indices, maxPoints, redrawGraph) { } var undo = spliceTraces(gd, update, indices, maxPoints, updateArray); - var promise = typeof redrawGraph === 'undefined' || redrawGraph ? exports.redraw(gd) : gd; + var promise = gd; + if (gd._context.redrawMin > 0) { + Lib.throttle( + gd._fullLayout._uid + '-redraw', + gd._context.redrawMinimumInterval, + function() { promise = exports.redraw(gd); } + ); + } else { + promise = exports.redraw(gd); + } var undoArgs = [gd, undo.update, indices, undo.maxPoints]; Queue.add(gd, exports.extendTraces, undoArgs, prependTraces, arguments); From 7e172375b52d6933c3136fcd8f336f705f940c9a Mon Sep 17 00:00:00 2001 From: drderiv Date: Mon, 31 Jul 2023 12:39:40 -0700 Subject: [PATCH 7/8] Update 6682_add.md Update to reflect change in approach suggested by @alexcjohnson. --- draftlog/6682_add.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/draftlog/6682_add.md b/draftlog/6682_add.md index 21d72e34da1..e7163bb0d77 100644 --- a/draftlog/6682_add.md +++ b/draftlog/6682_add.md @@ -1 +1 @@ -Add optional redrawGraph parameter to extendTraces/prependTraces [[#6682](https://github.com/plotly/plotly.js/pull/6682)] +Add optional redrawMinimumInterval parameter to config for use with extendTraces/prependTraces [[#6682](https://github.com/plotly/plotly.js/pull/6682)] From 03c2b4fbb7b55f648d3e035c5ad4a3de1a3cefe9 Mon Sep 17 00:00:00 2001 From: drderiv Date: Mon, 31 Jul 2023 13:08:58 -0700 Subject: [PATCH 8/8] Fix plot_api.js to conform to correct config parameter name. --- src/plot_api/plot_api.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 38894399d07..1bd4c82e778 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -938,7 +938,7 @@ function extendTraces(gd, update, indices, maxPoints) { var undo = spliceTraces(gd, update, indices, maxPoints, updateArray); var promise = gd; - if (gd._context.redrawMin > 0) { + if (gd._context.redrawMinimumInterval > 0) { Lib.throttle( gd._fullLayout._uid + '-redraw', gd._context.redrawMinimumInterval, @@ -1004,7 +1004,7 @@ function prependTraces(gd, update, indices, maxPoints) { var undo = spliceTraces(gd, update, indices, maxPoints, updateArray); var promise = gd; - if (gd._context.redrawMin > 0) { + if (gd._context.redrawMinimumInterval > 0) { Lib.throttle( gd._fullLayout._uid + '-redraw', gd._context.redrawMinimumInterval,