Repository navigation
Open
Description
I want to plot realtime data with x range [0, 10 * 1000], 60pfs, and I found a function update, but seems it need all data to refresh, while I only want to update only one sample data at a time
Lines 190 to 205 in edbb4cc
| update (const Ctnr1& _abscissae, const Ctnr2& _data, const unsigned int data_idx) | |
| { | |
| unsigned int dsize = _data.size(); | |
| sm::range<Flt> datarange; | |
| if (_abscissae.size() != dsize) { | |
| throw std::runtime_error ("GraphVisual::update: size mismatch"); | |
| } | |
| if (data_idx >= this->graphDataCoords.size()) { | |
| std::cout << "Can't add data at graphDataCoords index " << data_idx << std::endl; | |
| return; | |
| } | |
| // Ensure the vector at data_idx has enough capacity for the updated data | |
| this->graphDataCoords[data_idx]->resize (dsize); |
Here's the full code:
/*
* Visualize a graph on which points are added with time.
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <array>
#include <random>
#include <sm/vec>
#include <sm/vvec>
#include <mplot/Visual.h>
#include <mplot/ColourMap.h>
#include <mplot/GraphVisual.h>
int main()
{
int fps = 60;
double dt = 1.0 / fps;
int sample_rate = 1000;
int time_window = 10;
int total_samples = time_window * sample_rate;
int nums_per_sample = sample_rate / 60;
mplot::Visual v(1024, 768, "Continuous redrawing of GraphVisual");
auto gv = std::make_unique<mplot::GraphVisual<double>> (sm::vec<float>({0,0,0}));
gv->policy = mplot::stylepolicy::lines;
gv->setlimits (0, total_samples, 0, 10);
v.bindmodel (gv);
sm::vvec<double> x;
x.linspace (0, total_samples, total_samples);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> distrib0(0, 0.8);
std::uniform_real_distribution<> distrib1(1, 1.8);
std::vector<double> y1(x.size()), y2(x.size()), y3(x.size()), y4(x.size());
gv->setdata (x, y1);
gv->setdata (x, y2);
gv->setdata (x, y3);
gv->setdata (x, y4);
gv->finalize();
auto gvp = v.addVisualModel (gv);
size_t idx = 0;
while (v.readyToFinish() == false) {
v.waitevents (dt);
for (int i = 0; i < nums_per_sample; ++i) {
y1[idx] = distrib0(gen);
y2[idx] = distrib1(gen);
y3[idx] = std::sin(idx/10.0) + 3;
y4[idx] = std::sin(idx/50.0) + 5;
idx++;
idx %= total_samples;
}
gvp->update(x, y1, 0);
gvp->update(x, y2, 1);
gvp->update(x, y3, 2);
gvp->update(x, y4, 3);
v.render();
}
return 0;
}Metadata
Metadata
Assignees
Labels
No labels
Activity
sebsjames commentedon Sep 15, 2025
There's an append method for GraphVisual. This adds a single data point, without re-drawing the whole graph.
See the examples graph_incoming_data.cpp and graph_incoming_data_rescale.cpp
miyanyan commentedon Sep 15, 2025
Hi I read the append function
For
appendit seems there are some limits:appendfunction, seems no function likepop_frontmiyanyan commentedon Sep 15, 2025
Maybe need some functions like https://doc.qt.io/qt-6/qxyseries-qtcharts.html#replace-2
sebsjames commentedon Sep 15, 2025
It's true that there's no pop_front function, or a way to replace data points, which I think is what you really want in your example.
I'd be pleased to see this feature. It would require relating the vertexPositions used to draw the graph data to the data point, so that when the data point was changed, the relevant vertices in the OpenGL model would be updated.
sebsjames commentedon Sep 15, 2025
Do you think you have the resource to do this? It's probably a bit of a task. I don't have time right now.
miyanyan commentedon Sep 17, 2025
Yeah I can try this
miyanyan commentedon Sep 18, 2025
Seems need to update
VisualModelImpl? like adding aupdateVBO_subdatafunction here and callglBufferSubDatato update partial data, then call this function inGraphVisualsebsjames commentedon Sep 19, 2025
Yes, if the solution is to use additional OpenGL function calls such as glBufferSubData, then these changes will need to be made in VisualModelImpl.
sebsjames commentedon Sep 19, 2025
Bear in mind that, if possible, any new gl function calls should be available in OpenGL 4.1, which will ensure that Mac OS can run the code.
sebsjames commentedon Dec 18, 2025
For info, some work I'm doing at the moment on instanced rendering in VisualModels may make this issue easier to solve.
miyanyan commentedon Dec 18, 2025
I'm really looking forward to this. Previously, I found that adding the
glBufferSubDatafunction will bring a lot of changes in other functionssebsjames commentedon Dec 18, 2025
Yes, making GL changes is very involved. See also #101 where I intend to reduce the code complexity