Page 1 of 2

Can TGraph do X,Y plots?

PostPosted: Thu Oct 08, 2015 7:37 pm
by James Bott
I am in need of creating a X,Y plot chart.

It seems that TGraph can only make time-series point and/or line charts, i.e. plot the value of Y and increment the X value by 1 unit for each data point of Y. Since there is no documentation for TGraph it is hard to tell if it is capable of X,Y charts.

Does anyone know? If so, do you have an example?

James

Re: Can TGraph do X,Y plots?

PostPosted: Thu Oct 08, 2015 10:30 pm
by Antonio Linares
James,

In tgraph.prg we find:

#Define GRAPH_TYPE_BAR 1
#Define GRAPH_TYPE_LINE 2
#Define GRAPH_TYPE_POINT 3
#Define GRAPH_TYPE_PIE 4

so it seems as a point type is supported.

In order to select a type we have to do:

oGraph:nType := GRAPH_TYPE_POINT
oGraph:Refresh()

I have not tested it yet

Re: Can TGraph do X,Y plots?

PostPosted: Thu Oct 08, 2015 10:33 pm
by Antonio Linares
This is the right way to use it. Using samples\graph.prg example:

Code: Select all  Expand view

    @ 1, 1 GRAPH oGraph;
           SIZE 250, 200;
           TYPE 3; // GRAPH_TYPE_...
           YVALUES 3D XGRID YGRID XVALUES LEGENDS
 


Image

Re: Can TGraph do X,Y plots?

PostPosted: Thu Oct 08, 2015 10:57 pm
by James Bott
Antonio,

Yes, I have done that for years. But that is a Time-Series chart. You cannot specify the X value, only the Y value. So, X is always 1,2,3,4...and they are at equally spaced intervals. These are basically bar charts, using points and lines instead of bars.

I need to be able to plot user specified x and y points. E.G.:

X, Y
3.21,152
5.13, 133
7.67, 121
etc.

Does that clarify it?

There is another issue I forgot to mention. It seems that the Y-axis always starts at zero--there seems to be no way to specify the y-axis starting value. Thus, for instance, if you have a bunch of points all around 80 they are all going to appear very close together (almost in a straight line) since the Y-axis will be from 0 to 100. I need to be able to make the Y-axis start at around 70 and go to 100. I can't see a way to do this. I see what appears to be a hard-coded zero in the TGraph code for the start of the Y-axis.

James

Re: Can TGraph do X,Y plots?

PostPosted: Thu Oct 08, 2015 11:04 pm
by Antonio Linares
James,

I am afraid that you need to review Class TGraph Method Paint() and implement your own painting method for a new type.

I am not familiar with Class TGraph source code, so you may need to review it and modify it to your needs.

Re: Can TGraph do X,Y plots?

PostPosted: Thu Oct 08, 2015 11:24 pm
by James Bott
Antonio,

Yes, that was my plan and I have done it before. But, before I start, I just wanted to check to make sure that x,y plots were not possible, and/or to see if anyone had already made any such modifications.

I was just looking at the code, and I forgot about another issue. It seems that the user defined X-axis label is printed on the Y-axis and visa versa. This makes it confusing when looking at the code since you don't know if it is just the label on the chart that is wrong, or are all the code vars containing a Y actually referring to the X-axis. This will be fun...

James

Re: Can TGraph do X,Y plots?

PostPosted: Mon Mar 05, 2018 1:04 pm
by alvaro533
Hi James,

I know it is an old post, but did you implement X,Y plots in TGraph?

Thank you

Alvaro

Re: Can TGraph do X,Y plots?

PostPosted: Tue Mar 06, 2018 5:27 am
by James Bott
Alvaro,

Sorry, no I never did. Most of my graphs are time series and TGraph works great for that. I can't remember what I did for the x,y plots that I needed back then, but I can find no evidence that I ever tried it. The TGraph code is quit complex and I expect it will take quite some time to modify it.

Regards,
James

Re: Can TGraph do X,Y plots?

PostPosted: Tue Mar 06, 2018 8:53 am
by alvaro533
James Bott wrote:Alvaro,

Sorry, no I never did. Most of my graphs are time series and TGraph works great for that. I can't remember what I did for the x,y plots that I needed back then, but I can find no evidence that I ever tried it. The TGraph code is quit complex and I expect it will take quite some time to modify it.

Regards,
James



Thank you James,

I modified the class yesterday to plot XY Charts.

New data:

Code: Select all  Expand view

#Define GRAPH_TYPE_XY_AQ 6

   data    chart_top
   data    chart_bottom
   data    chart_left
   data    chart_right
   DATA    nXMaxVal         // Max Value
   DATA    nXMinVal         // Min Value
   data    XY_Values


new methods

Code: Select all  Expand view

   METHOD  aq_DrawLine( nY, nX, nHigh, nWidth, nColor, lDotted  , nPenwidth )
   METHOD  aq_DrawPoint( nY, nX,  nColor, lDotted , nPixels  , nPenwidth )

// --------------------------------------------------------------------------- //
METHOD aq_DrawLine( nY1, nX1, nY2, nX2, nColor, lDotted , nPenwidth) CLASS TGraph
local x1,x2,y1,y2
local era_nPenwidth

default nPenwidth := 3

era_nPenwidth := ::nPenwidth
::nPenWidth :=  nPenwidth


x1 := ::chart_left +   (nX1-::nXMinVal ) / (::nXMaxval-::nXMinVal) * ( ::chart_right - ::chart_left )
y1 := ::chart_bottom - (nY1-::nMinVal) / (::nMaxval-::nMinVal) * ( ::chart_bottom - ::chart_top )
x2 := ::chart_left +   (nX2-::nXMinVal ) / (::nXMaxval-::nXMinVal) * ( ::chart_right - ::chart_left )
y2 := ::chart_bottom - (nY2-::nMinVal) / (::nMaxval-::nMinVal) * ( ::chart_bottom - ::chart_top )

::DrawLine( y1, x1, y2, x2, nColor, lDotted )

::nPenWidth := era_nPenwidth

return nil
// --------------------------------------------------------------------------- //
METHOD aq_DrawPoint( nY1, nX1, nColor, lDotted , npixels  , nPenwidth ) CLASS TGraph
local x1,y1
local era_nPenwidth


default nPenwidth := nPenwidth
default nPixels := 7

era_nPenwidth := ::nPenwidth
::nPenWidth := 4


nPixels := nPixels /2

x1 := ::chart_left +   (nX1-::nXMinVal ) / (::nXMaxval-::nXMinVal) * ( ::chart_right - ::chart_left )
y1 := ::chart_bottom - (nY1-::nMinVal) / (::nMaxval-::nMinVal) * ( ::chart_bottom - ::chart_top )

::DrawLine( y1 , x1-nPixels , y1+nPixels, x1, nColor, lDotted )
::DrawLine( y1+nPixels , x1 , y1, x1+nPixels, nColor, lDotted )
::DrawLine( y1 , x1+nPixels , y1-nPixels, x1, nColor, lDotted )
::DrawLine( y1-nPixels , x1 , y1 , x1-nPixels , nColor, lDotted )

::nPenWidth := era_nPenwidth

return nil

 


This line at the begining of method new and method redefine

Code: Select all  Expand view

   ::XY_Values :={}
 


This lines in the Paint method after the tag // Graph borders

Code: Select all  Expand view

::chart_top := nTop
::chart_bottom := nBottom
::chart_left := nLeft
::chart_right := nRight
 



And finally this lines in the Paint method before the tag // legends or // Max, Min values

Code: Select all  Expand view

   IF ::nType == GRAPH_TYPE_XY_AQ
      FOR nJ := 1 TO Len( ::aSeries )
         for nI := 1 to len( ::XY_Values[nJ] ) -1
          ::aq_Drawline( ::XY_Values[nJ, nI,2] , ::XY_Values[nJ,nI,1] , ;
                           ::XY_Values[nJ, nI+1,2] , ::XY_Values[nJ, nI+1,1], ::aSeries[nj,2] )
         next nI

         if len( ::XY_Values[nJ] ) == 1
            ::aq_DrawPoint( ::XY_Values[nJ, 1,2] , ::XY_Values[nJ,1,1] , ::aSeries[nj,2] ,,  )
         endif
      next nJ
      if ::lYVal
            ::Say( 325 , 80 , "Hola" , ::aFont[3], rgb(0,0,0) , ::nTLeft )
      endif
   endif
 



Then in your prg code you have to put

Code: Select all  Expand view

   oChart:nType    := GRAPH_TYPE_XY_AQ

 oChart:nMaxVal   := max_value //  your calculated max value for Y axis
 oChart:nMinVal   := min_value  //  your calculated min value for Y axis
 oChart:nXMaxVal  := max_Xvalue  //  your calculated max value for X axis
 oChart:nXMinVal  := min_Xvalue    //  your calculated min value for X axis

 oChart:AddSerie(  {} , "name_or_the_serie1" , RGB(128,128,255) )  // empty array
 aadd( oChart:XY_Values , aArray1  )  // aArray1 is an array with the XY points  i.e. {   { 1.25 , 9.25} , { 1.5  , 9.452 } , ......}

 oChart:AddSerie(  {} , "name_or_the_serie2" , RGB(128,128,255) )  // empty array
 aadd( oChart:XY_Values , aArray2  )  // aArray2 is an array with the XY points  i.e. {   { 3.25 , 7.25} , { 2.5  , 8.452 } , ......}

you may add more series

 


Code: Select all  Expand view
 



Regards

Re: Can TGraph do X,Y plots?

PostPosted: Tue Mar 06, 2018 4:54 pm
by James Bott
Alvaro,

Wow, very impressive. May I get a copy of your new tgraph.prg?

You can find my email address on my website http://gointellitech.com.

Re: Can TGraph do X,Y plots?

PostPosted: Tue Mar 06, 2018 5:08 pm
by alvaro533
James Bott wrote:Alvaro,

Wow, very impressive. May I get a copy of your new tgraph.prg?

You can find my email address on my website http://gointellitech.com.


Sent, regards
Alvaro

Re: Can TGraph do X,Y plots?

PostPosted: Tue Mar 06, 2018 5:10 pm
by Antonio Linares
Alvaro,

Would you mind to post it here or send it to me by email, so we can include it in next FWH 18.02 ?

many thanks

Re: Can TGraph do X,Y plots?

PostPosted: Tue Mar 06, 2018 5:26 pm
by James Bott
Alvaro,

It is not clear to me if you are automatically finding the min and max for each axis in the class, or if you must do the calculation outside the class and pass those values.

One of the first programs I wrote (way back in the BASIC era, circa 1980), I wrote code to find those values. I discovered that it was a challenge to find those values since they needed to be rounded up (or down) to a reasonable number that made the chart look right. E.G. rounding 10.3 to 11 or 15 or whatever.

Of course, being able to specify the ranges by overriding the internal calculations would also be useful.

------------------------------

May I suggest changing this line:

oChart:nType := GRAPH_TYPE_XY_AQ

To something like this:

oChart:nType := GRAPH_TYPE_XY //AQ

I am assuming AQ are your initials? The above change just keeps the naming consistent with the existing ones.

If you care to share this modified class with everyone, then I would also first make sure you have the latest copy of TGraph, and make your changes to that version. Also I would suggest adding comments at the top explaining your work and adding your name and contact info.

I am looking forward to seeing your modified TGraph class.

Regards,
James

Re: Can TGraph do X,Y plots?

PostPosted: Tue Mar 06, 2018 6:48 pm
by alvaro533
James Bott wrote:Alvaro,

It is not clear to me if you are automatically finding the min and max for each axis in the class, or if you must do the calculation outside the class and pass those values.

One of the first programs I wrote (way back in the BASIC era, circa 1980), I wrote code to find those values. I discovered that it was a challenge to find those values since they needed to be rounded up (or down) to a reasonable number that made the chart look right. E.G. rounding 10.3 to 11 or 15 or whatever.

Of course, being able to specify the ranges by overriding the internal calculations would also be useful.

------------------------------

May I suggest changing this line:

oChart:nType := GRAPH_TYPE_XY_AQ

To something like this:

oChart:nType := GRAPH_TYPE_XY //AQ

I am assuming AQ are your initials? The above change just keeps the naming consistent with the existing ones.

If you care to share this modified class with everyone, then I would also first make sure you have the latest copy of TGraph, and make your changes to that version. Also I would suggest adding comments at the top explaining your work and adding your name and contact info.

I am looking forward to seeing your modified TGraph class.

Regards,
James


I use this to calculate the min/max values, I will add to the class. Please send me the last one. I will also work on displaying the XTitles

Code: Select all  Expand view

 oChart:nXMaxVal  := max_Xvalue + ( max_Xvalue - min_Xvalue ) *.1
 oChart:nXMinVal  := min_Xvalue - ( max_Xvalue - min_Xvalue ) *.1
 


Regards

Re: Can TGraph do X,Y plots?

PostPosted: Wed Mar 07, 2018 1:05 am
by James Bott
I should clarify, in the beginning of this thread I was talking about making an x,y chart with just data points. This is also commonly called a scatter chart. For this type of chart the data points can be in any order. You can see a sample scatter chart here:

https://www.highcharts.com/docs/chart-and-series-types/scatter-chart

The line chart that TGraph has already, is a series chart where the X-axis data is in increasing order starting from the smallest point. An example might be temperature vs month-of-the-year. Here is a sample line chart:

https://www.highcharts.com/docs/chart-and-series-types/line-chart

Alvaro, which type of chart are you looking for? What type of data are you working with?