[vtkusers] Updating data in a vtkChartXY

Nicolas Bigaouette nbigaouette at gmail.com
Tue Dec 1 13:29:44 EST 2015


Hi all,

I'm using vtkChartXY to plot some data.

I now need to update the data that is displayed but I'm having trouble
doing so.


Some people were able to call ClearPlots() to recreate the plot. This is a
bit problematic since I have multiple lines on my figure. I was able to
refactor my code to store the line properties (color, width, etc.) and
re-apply them after they are added back to the figure. See for example
those:

http://public.kitware.com/pipermail/vtkusers/2012-March/073136.html
http://vtk.1045678.n5.nabble.com/Cannot-update-vtkChartXY-td5734681.html
http://vtk.1045678.n5.nabble.com/How-to-update-a-2d-plot-vtkChartXY-with-new-data-online-using-C-td5725677.html

Since I have multiple lines, I found it easier for now to store a
std::vector<vtkSmartPointer<vtkTable> > and a vector of my line class. So
to update line index "tableToUpdate", I do something like:

const int prevNbRows = table[tableToUpdate]->GetNumberOfRows();

// FIXME: Don't insert one row at the time!

size_t j = prevNbRows;

for (int i = 0 ; i < x.size() ; i++) {

    table[tableToUpdate]->InsertNextBlankRow();

    table[tableToUpdate]->SetValue(j, 0, x[i]);

    table[tableToUpdate]->SetValue(j, 1, y[i]);

    j++;

}

chart->ClearPlots();

for (int k = 0 ; k < lines.size() ; k++)

{

    lines[k].linePtr = chart->AddPlot(vtkChart::LINE);

    lines[k].RestoreLineProperties();

    lines[k].linePtr->SetInputData(table[k], 0, 1);

}


I have three questions.

1) Does the ClearPlots() frees all memory used? What happens to the
previous linePtr? Are they free? If I update the figure thousands of time
for a "large" number of lines, is there a risk of a memory leak somewhere?
How to properly free the resources?

2) Is there a better way to update a single line out of many? Without
memory leaks?

3) Most importantly, this breaks setting the axis range. For example, doing
this after a line update:

chart->GetAxis(vtkAxis::BOTTOM)->SetRange(minval, maxval);

will change the axis values, but the lines look exactly the same as before
this "zooming".

Maybe it has something to do with how I put everything together:

vtkSmartPointer<vtkRenderer> vRenderer =
> vtkSmartPointer<vtkRenderer>::New();
> vtkGenericOpenGLRenderWindow *renderWindow = GetRenderWindow();
> renderWindow->AddRenderer(renderer);
> vtkSmartPointer<vtkContextScene> chartScene =
> vtkSmartPointer<vtkContextScene>::New();
> vtkSmartPointer<vtkContextActor> chartActor =
> vtkSmartPointer<vtkContextActor>::New();
> vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
> chartScene->AddItem(chart);
> chartActor->SetScene(chartScene);
> renderer->AddActor(chartActor);



Any ideas or suggestions?

Thanks!

Nicolas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20151201/580bd565/attachment.html>


More information about the vtkusers mailing list