-2

The Y axis will be scaled automatically, depending on the values coming fallowing function.

void mouseHandleCordinate(double val){
  // include graph function.
}

So I want to create the plot chart against the time. X axis represent time and Y represent the value coming above function. How I create above graph function.

Always pass the data into void mouseHandleCordinate(double val) function. As example:

val >>> 2.1,3,1,6,7,5.5,0,9,5,6,7,3.6,2,5,6,7,8,1,2,3,4 >> represent double val
Time>>> 21,20,19,18,17,......., 4,3,2,1 second
Spektre
  • 49,595
  • 11
  • 110
  • 380
Wpiumi pabasara
  • 79
  • 1
  • 1
  • 6

1 Answers1

0

not sure I got what you asking but looks like you want to create continuous function following table of sampled points like:

const int N=21; // number of samples
const double t0=21.0,t1=1.0; // start,end times
const double val[N]={ 2.1,3,1,6,7,5.5,0,9,5,6,7,3.6,2,5,6,7,8,1,2,3,4 };

double f(double t) // nearest
 {
 t = (t-t0)/(t1-t0); // time scaled to <0,1>
 t*= (N-1); // time scaled to <0,N) .. index in table
 int ix=t; // convert to closest index in val[]
 if ((ix<0)||(ix>=N)) return 0.0; // handle undefined times
 return val[ix]; // return closest point in val[]
 }

This will give you the nearest neighbor style function value. If you need something better use linear or cubic or better interpolation for example:

double f(double t) // linear
 {
 t = (t-t0)/(t1-t0); // time scaled to <0,1>
 t*= (N-1); // time scaled to <0,N) .. index in table
 int ix=t; // convert to closest index in val[]
 if ((ix<0)||(ix>=N)) return 0.0; // handle undefined times
 if (ix==N-1) return val[ix]; // return closest point in val[] if on edge
 // linear interpolation
 t = t-floor(t); // distance of time between ix and ix+1 points scaled to <0,1>
 return val[ix]+(val[ix+1]-val[ix])*t; // return linear interpolated value
 }

Now to your problem:

void mouseHandleCordinate(double mx) // mouse x coordinate in [pixels] I assume
 {
 double t,x,y,x0,y0

 // plot
 x=0;
 y=f(view_start_time)
 for (t=view_start_time;t<=view_end_time;t+=(view_end_time-view_start_time)/view_size_in_pixels,x0=x,x++)
  {
  y0=y; y=f(t); 
  // render line x0,y0,x,y
  }

 // mouse highlight
 t = view_start_time+((view_end_time-view_start_time)*mx/view_size_in_pixels);
 x = mx;
 y = f(t);
 // render point x,y ... for example with circle r = 16 pixels
 }

where:
view_start_time is time of the left most pixel in your plot view
view_end_time is time of the right most pixel in your plot view
view_size_in_pixels is the x resolution in [pixels] of your plot view

[Notes]

I code the stuff directly in SO editor so there may be typos ... Hoping you are calling the mouseHandleCordinate in some Paint event instead of on mouse movement which should only schedule repaint order... Also you should add y view scale in similar manner to x scale I used ...

For more info see:

Spektre
  • 49,595
  • 11
  • 110
  • 380