Re: Using fixed-size data buffer with GtkPlot

Al Hooton <[email protected]> Sat, 25 Feb 2006 06:28:55 -0800
Newsgroups gmane.comp.scigraphica.gtkextra
Message-ID <[email protected]>
	Nickolai,

On Wed, 2006-02-22 at 23:42 -0600, Nickolai Dobrynin wrote:
> I would like to be able to append points and plot them one at a time,
> which is pretty much
> what the 'testrealtime' example does.  The additional constraint I
> have is that, unlike 'testrealtime', I
> cannot store data points in a dynamically allocated buffer and let
> this buffer grow indefinitely.

	So, if I understand you correctly, you want to have an ever-growing
number of points plotted, but every so often you want to throw away the
points you've already plotted and start over with a clean plot?

	Or, do you want to have an unknown number of points plotted as they
come in, but you do not want to manage the memory for them dynamically?

	Or... ?   I'm not sure I understand the question...
dynamically-managing the dataset memory is easy, but are you concerned
about performance?  If this is the case, don't worry -- the plotting
machinery will take far more cycles than dynamically reallocating
arrays, etc.

> 
> Here is the 'update' function I call to add a single point to the
> plot, while using two C-style arrays:
> 
> #define NPOINTS 5000
> gint runningCount;
> gdouble px[NPOINTS];
> gdouble py[NPOINTS];
> 
> gboolean update(gpointer)
> {
>   gdouble x,y;
>   getPoint(runningCount,x,y);
> 
>   gdouble xmin, xmax; /* [xmin,xmax) */
>   gtk_plot_get_xrange(plot, &xmin, &xmax);
>   if (runningCount >= NPOINTS) {
>     runningCount = 0;
> 
>     px[0] = x; py[0] = y;
>     gtk_plot_set_xrange(plot, xmin + 2*M_PI, xmax + 2*M_PI);
>     gtk_plot_data_set_numpoints(dataset, 1);
>     gtk_plot_data_dimension_set_points(dataset, "x", px);
>     gtk_plot_data_dimension_set_points(dataset, "y", py);



	It's been a while since I looked at my code that uses gtkplot, but try
this here and see what happens:

      gtk_plot_data_set_numpoints(dataset, 0);
      gtk_plot_refresh(plot, NULL);


> 
>     gtk_plot_data_draw_points(dataset, 1);
>     gtk_plot_canvas_paint(GTK_PLOT_CANVAS(canvas));
> 
>     gtk_widget_queue_draw(GTK_WIDGET(canvas));
>   }
>   else {
>     px[runningCount] = x; py[runningCount] = y;
>     gtk_plot_data_set_numpoints(dataset, dataset->num_points+1);
> 
>     gtk_plot_data_draw_points(dataset, 1);
>     gtk_plot_refresh(plot, NULL);
>   }
> 
>   ++runningCount;
> 
>   return TRUE;
> }
> 
> Unfortunately, I only get meaningful results until the predicate in
> the if-statement
> becomes true.  After that, I don't see anything being plotted at all.
> Is there any
> way to force the widget to "forget" the previous data I fed to it and
> start from
> scratch?  Once again, my goal is to use a fixed-size array to store
> data points,
> so I wouldn't have to worry about allocating memory on demand.
> 
> 
> Many thanks,
> 
> Nickolai
> !DSPAM:43fd4bb636596333719912!



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642