SFGraphing
SFGraphing copied to clipboard
Store SFPlot datasets by reference
Currently, _plotDataSets
is stored as a vector of value, meaning data sets are actually copies of their original object. This creates the somewhat surprising behavior that modifying the underlying PlotDataSet
instantiation has no effect on the graph when redrawn.
It seem like this is circumvented in the example by creating a new SFPlot
object on every loop, but that felt somewhat wasteful. Now the library can be used for continuously updating graphs like so:
// One-time initialization
SFPlot plot(...);
PlotDataSet data(...);
plot.AddDataSet(data);
// Render loop
while (window.isOpen()) {
// ...
// Modify dataset - add, remove, or replace
data.PushPair(...)
// Drawing the plot now picks up these data changes
plot.GenerateVertices()
window.draw(plot)
// ...
}
Because the datasets are now stored by reference, updating them externally will cause the graph to also update - and the plot object can be reused across renders. Up to you whether you think this aligns with the intended usage pattern for the project - happy to drop this is you prefer the current approach.