Can TGraph do X,Y plots?

Can TGraph do X,Y plots?

Postby James Bott » Thu Oct 08, 2015 7:37 pm

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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Can TGraph do X,Y plots?

Postby Antonio Linares » Thu Oct 08, 2015 10:30 pm

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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Can TGraph do X,Y plots?

Postby Antonio Linares » Thu Oct 08, 2015 10:33 pm

This is the right way to use it. Using samples\graph.prg example:

Code: Select all  Expand view  RUN

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


Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Can TGraph do X,Y plots?

Postby James Bott » Thu Oct 08, 2015 10:57 pm

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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Can TGraph do X,Y plots?

Postby Antonio Linares » Thu Oct 08, 2015 11:04 pm

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.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Can TGraph do X,Y plots?

Postby James Bott » Thu Oct 08, 2015 11:24 pm

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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Can TGraph do X,Y plots?

Postby alvaro533 » Mon Mar 05, 2018 1:04 pm

Hi James,

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

Thank you

Alvaro
alvaro533
 
Posts: 206
Joined: Sat Apr 19, 2008 10:28 pm
Location: Madrid, España

Re: Can TGraph do X,Y plots?

Postby James Bott » Tue Mar 06, 2018 5:27 am

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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Can TGraph do X,Y plots?

Postby alvaro533 » Tue Mar 06, 2018 8:53 am

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  RUN

#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  RUN

   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  RUN

   ::XY_Values :={}
 


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

Code: Select all  Expand view  RUN

::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  RUN

   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  RUN

   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  RUN
 



Regards
alvaro533
 
Posts: 206
Joined: Sat Apr 19, 2008 10:28 pm
Location: Madrid, España

Re: Can TGraph do X,Y plots?

Postby James Bott » Tue Mar 06, 2018 4:54 pm

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.
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Can TGraph do X,Y plots?

Postby alvaro533 » Tue Mar 06, 2018 5:08 pm

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
alvaro533
 
Posts: 206
Joined: Sat Apr 19, 2008 10:28 pm
Location: Madrid, España

Re: Can TGraph do X,Y plots?

Postby Antonio Linares » Tue Mar 06, 2018 5:10 pm

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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Can TGraph do X,Y plots?

Postby James Bott » Tue Mar 06, 2018 5:26 pm

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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Can TGraph do X,Y plots?

Postby alvaro533 » Tue Mar 06, 2018 6:48 pm

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  RUN

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


Regards
alvaro533
 
Posts: 206
Joined: Sat Apr 19, 2008 10:28 pm
Location: Madrid, España

Re: Can TGraph do X,Y plots?

Postby James Bott » Wed Mar 07, 2018 1:05 am

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?
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 56 guests