Skip to content

How to update partial data #73

Open

Description

@miyanyan

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

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;
}

Activity

  1. sebsjames commented on Sep 15, 2025

    @sebsjames
    Owner

    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

  2. miyanyan commented on Sep 15, 2025

    @miyanyan
    ContributorAuthor

    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

    Hi I read the append function
    For append it seems there are some limits:

    • I want to update the point in middle, not only the point at the end
    • I want to keep the x range fixed, while there's append function, seems no function like pop_front
  3. miyanyan commented on Sep 15, 2025

    @miyanyan
    ContributorAuthor
  4. sebsjames commented on Sep 15, 2025

    @sebsjames
    Owner

    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.

  5. sebsjames commented on Sep 15, 2025

    @sebsjames
    Owner

    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.

  6. miyanyan commented on Sep 17, 2025

    @miyanyan
    ContributorAuthor

    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.

    Yeah I can try this

  7. miyanyan commented on Sep 18, 2025

    @miyanyan
    ContributorAuthor

    Seems need to update VisualModelImpl? like adding a updateVBO_subdata function here and call glBufferSubData to update partial data, then call this function in GraphVisual

  8. sebsjames commented on Sep 19, 2025

    @sebsjames
    Owner

    Yes, if the solution is to use additional OpenGL function calls such as glBufferSubData, then these changes will need to be made in VisualModelImpl.

  9. sebsjames commented on Sep 19, 2025

    @sebsjames
    Owner

    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.

  10. sebsjames commented on Dec 18, 2025

    @sebsjames
    Owner

    For info, some work I'm doing at the moment on instanced rendering in VisualModels may make this issue easier to solve.

  11. miyanyan commented on Dec 18, 2025

    @miyanyan
    ContributorAuthor

    I'm really looking forward to this. Previously, I found that adding the glBufferSubData function will bring a lot of changes in other functions

  12. sebsjames commented on Dec 18, 2025

    @sebsjames
    Owner

    Yes, making GL changes is very involved. See also #101 where I intend to reduce the code complexity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions