Page 1 of 2

Using Graph

Posted: Tue Mar 24, 2009 2:43 pm
by sudip
Hi All,

I just wrote a program to show the output in graph (from the data generated from my good-old Clipper/xHarbour+gtwvw program!!!). So my clients can use it as an add-on. :) Here is the code for it.

I created two arrays aSer which is array of series, ay which contains y values.

I started to write the code from Graph_3 of samples folder. So, it's basically a modified demo. But, modification is so much that I want to share it with all of you.
bargraph.jpg
bargraph.jpg (87.31 KiB) Viewed 7742 times

Code: Select all

Function GenerateGraph(aSer, ay, mGraphTitle)

	Define Window GraphTest ;
		At 0,0 ;
		Width 640 ;
		Height 480 ;
		Title "Graph" ;
		MODAL ;
		BackColor { 255 , 255 , 255 } ;
		On Init DrawGraph ( 1, aSer, ay, mGraphTitle )
		
		@ 415, 20 label lblGraphtype value "Graph Type:" TRANSPARENT
		@ 415, 120 combobox cboGraphtype ;
			items {"Bars", "Lines", "Points"} ;
			value 1 ;
			on change DrawGraph(this.value, aSer, ay, mGraphTitle) 

		@ 415, 500 button cmdPrint caption "Print" ;
			Action PRINT GRAPH OF GraphTest PREVIEW DIALOG

	End Window

	GraphTest.Center

	Activate Window GraphTest

Return


Procedure DrawGraph ( mGraphType, aSer, ay, mGraphTitle ) 

	ERASE WINDOW GraphTest
	
	if mGraphtype = 1
		DRAW GRAPH							;
			IN WINDOW GraphTest					;
			AT 20,20						;
			TO 400,610						;
			TITLE mGraphTitle				;
			TYPE BARS						;
			SERIES aSer					;
			YVALUES ay			;
			DEPTH 15						;
			BARWIDTH 15						;
			HVALUES 5						;
			SERIENAMES {"Sale"}		;
			COLORS { {128,128,255} }	;
			3DVIEW    						;
			SHOWGRID                        			;
			SHOWXVALUES                     			;
			SHOWYVALUES                     			;
			; //SHOWLEGENDS 						;
		NOBORDER
	elseif mGraphtype = 2
		DRAW GRAPH							;
			IN WINDOW GraphTest					;
			AT 20,20						;
			TO 400,610						;
			TITLE mGraphTitle				;
			TYPE LINES					;
			SERIES aSer					;
			YVALUES ay			;
			DEPTH 15						;
			BARWIDTH 15						;
			HVALUES 5						;
			SERIENAMES {"Sale"}		;
			COLORS { {128,128,255} }	;
			3DVIEW    						;
			SHOWGRID                        			;
			SHOWXVALUES                     			;
			SHOWYVALUES                     			;
			; //SHOWLEGENDS 						;
			NOBORDER
	else
			DRAW GRAPH							;
			IN WINDOW GraphTest					;
			AT 20,20						;
			TO 400,610						;
			TITLE mGraphTitle				;
			TYPE POINTS						;
			SERIES aSer					;
			YVALUES ay			;
			DEPTH 15						;
			BARWIDTH 15						;
			HVALUES 5						;
			SERIENAMES {"Sale"}		;
			COLORS { {128,128,255} }	;
			3DVIEW    						;
			SHOWGRID                        			;
			SHOWXVALUES                     			;
			SHOWYVALUES                     			;
			; //SHOWLEGENDS 						;
			NOBORDER
	endif
	
Return
One thing more. In HMG, if the values are larger than 999,999,99, SHOWXVALUES will show you "*"s. So, I just divided my values with 1000 :)

Please advise how to improve the graph generation software. :)

With best regards.

Sudip

Re: Using Graph

Posted: Tue Mar 24, 2009 4:21 pm
by esgici
Hi Sudip

Nice work, thanks to sharing.

Regards

--

Esgici

Re: Using Graph

Posted: Tue Mar 24, 2009 5:00 pm
by sudip
Hi Esgici,

Your words always inspire me to do a better job :)

Thanks a lot for reviewing a very basic program of a learner. :)

With best regards.

Sudip

Re: Using Graph

Posted: Wed Mar 25, 2009 12:41 am
by esgici
Hi Sudip

Could you add "main" proc/func to your code, for making it a "working sample".

I want run and seeing while running samples instead of simply reading code.

Regards

--

Esgici

Re: Using Graph

Posted: Wed Mar 25, 2009 11:49 am
by sudip
Hi Esgici,

Thank you for your interest! :)

I am sending a working sample for creating graph. Please compile and run :) (And please don't forget to send your advice) :)

Code: Select all

#include "minigui.ch"

Function Main

   Define Window frmsg ;
      at 0, 0 ;
      width 470 ;
      height 350 ;
      title "Sale Graph (Created with HMG)" ;
      main ;
      nomaximize

      @ 240, 350 button cmdRun caption "Generate Graph" ;
         action CalculateGraph()

   end window

   frmSG.center
   frmSG.activate
return nil



STATIC function CalculateGraph()
   local i, n, t, ax, ay, aSer, mGraphTitle, aMonth

   ax := {}
   ay := {}

   aMonth := {{"Apr", 0.00}, {"May", 0.00}, {"Jun", 0.00}, ;
      {"Jul", 0.00}, {"Aug", 0.00}, {"Sep", 0.00}, ;
      {"Oct", 0.00}, {"Nov", 0.00}, {"Dec", 0.00}, ;
      {"Jan", 0.00}, {"Feb", 0.00}, {"Mar", 0.00}}

   for i := 1 to len(aMonth)
      aMonth[i, 2] = random(99999.99)
   next

   for i := 1 to len(aMonth)
      aadd(ax, round(aMonth[i, 2], 2))
      aadd(ay, aMonth[i, 1])
   next

   aSer := {}
   aadd(aSer, ax)

   mGraphTitle := "Sale"

   GenerateGraph(aSer, ay, mGraphTitle)

return nil




Function GenerateGraph(aSer, ay, mGraphTitle)

   Define Window GraphTest ;
      At 0,0 ;
      Width 640 ;
      Height 480 ;
      Title "Graph with HMG" ;
      MODAL ;
      BackColor { 255 , 255 , 255 } ;
      On Init DrawGraph ( 1, aSer, ay, mGraphTitle )

      @ 415, 20 label lblGraphtype value "Graph Type:" TRANSPARENT
      @ 415, 120 combobox cboGraphtype ;
         items {"Bars", "Lines", "Points"} ;
         value 1 ;
         on change DrawGraph(this.value, aSer, ay, mGraphTitle)

      @ 415, 500 button cmdPrint caption "Print" ;
         Action PRINT GRAPH OF GraphTest PREVIEW DIALOG

   End Window

   GraphTest.Center

   Activate Window GraphTest

Return




Procedure DrawGraph ( mGraphType, aSer, ay, mGraphTitle )

   ERASE WINDOW GraphTest

   if mGraphtype = 1
      DRAW GRAPH							;
         IN WINDOW GraphTest					;
         AT 20,20						;
         TO 400,610						;
         TITLE mGraphTitle				;
         TYPE BARS						;
         SERIES aSer					;
         YVALUES ay			;
         DEPTH 15						;
         BARWIDTH 15						;
         HVALUES 5						;
         SERIENAMES {""}		;
         COLORS { {128,128,255} }	;
         3DVIEW    						;
         SHOWGRID                        			;
         SHOWXVALUES                     			;
         SHOWYVALUES                     			;
         ;                                          //SHOWLEGENDS 						;
         NOBORDER
   elseif mGraphtype = 2
      DRAW GRAPH							;
         IN WINDOW GraphTest					;
         AT 20,20						;
         TO 400,610						;
         TITLE mGraphTitle				;
         TYPE LINES					;
         SERIES aSer					;
         YVALUES ay			;
         DEPTH 15						;
         BARWIDTH 15						;
         HVALUES 5						;
         SERIENAMES {""}		;
         COLORS { {128,128,255} }	;
         3DVIEW    						;
         SHOWGRID                        			;
         SHOWXVALUES                     			;
         SHOWYVALUES                     			;
         ;                                          //SHOWLEGENDS 						;
         NOBORDER
   else
      DRAW GRAPH							;
         IN WINDOW GraphTest					;
         AT 20,20						;
         TO 400,610						;
         TITLE mGraphTitle				;
         TYPE POINTS						;
         SERIES aSer					;
         YVALUES ay			;
         DEPTH 15						;
         BARWIDTH 15						;
         HVALUES 5						;
         SERIENAMES {""}		;
         COLORS { {128,128,255} }	;
         3DVIEW    						;
         SHOWGRID                        			;
         SHOWXVALUES                     			;
         SHOWYVALUES                     			;
         ;                                          //SHOWLEGENDS 						;
         NOBORDER
   endif

   Return
With best regards.

Sudip

Re: Using Graph

Posted: Wed Mar 25, 2009 12:48 pm
by esgici
Hi Sudip

Thanks a lot.

I had save it into my "Informal HMG Samples"
sudip wrote:
... don't forget to send your advice
You know, improvement is a very very long way :)

Only some random thoughts :

- You can eliminate unused main (frmsg) window, such as

Code: Select all

Function Main
   CalculateGraph()
return nil

( Don't forget changing MODAL property of GraphTest window to MAIN )

Or my be better, extract CalculateGraph() function and move its content to Main()

- You can acquire graph data from a table, instead of producing by code.

- You don't use SERIES possibility. You can use it f.e. by defining "Cost", "Sales", "Profits".

But don't expand it very much. Samples must be compact, simple and easy to understand as far as possible.

Best Regards

--

Esgici

Re: Using Graph

Posted: Wed Mar 25, 2009 4:00 pm
by sudip
Hi Esgici,

Thanks a lot for your interest about a very small project :)
I had save it into my "Informal HMG Samples"
I am honored! :)

I copied the code from one of my latest add-on project for good-old clipper (now xharbour+gtwvw) apps. Yesterday I started this project and as soon as I created my first graph, I wanted to share it with everybody and posted in the forum!

Mr. Grigory Filatov helped me in solving one problem in graph (value greater than 99999.99 shows "*"s). Thank you Grigory Filatov. :)

Today, I finished my small add-on project and it is created with HMG. It can create Bar, Line, Point graphs and Pie charts from tables of my old apps :)

With best regards.

Sudip

Re: Using Graph

Posted: Wed Mar 25, 2009 6:32 pm
by Rathinagiri
Nice presentation. Thank you Sudip.

Re: Using Graph

Posted: Thu Mar 26, 2009 3:16 am
by sudip
Hi,

Now I shall create add-on for VFP apps. :)

This add-on include graph (without using MS-Graph), transfer to Excel etc.

BTW, is there any code available like Grid2Excel? If it's not there I shall create it :) I already had my own dbf2excel (created with xHarbour).

Hi Rathi, now I shall use your Grid2Print. Very soon I shall need your help for MySql. :)

Thanks a lot to you all. Again, I am feeling fun in programming :)

Regards.

Sudip

Re: Using Graph

Posted: Wed Dec 08, 2010 5:34 pm
by nguyenchiduc
I think if we can draw each point. HMG graphics can help us draw diagrams, data analysis, time series. Mathematical statistics

if the precision is not important, it is only for illustration

Image

Code: Select all

#include "hmg.ch"

function main

	define window mathematica at 0,0 ;
		width 800 ;
		height 600 ;
		title "Shapes Demo" ;
		main on init drawshapes()
    @ 10, 410 LABEL LABEL_function_1 Value "y = x*x/30" font '.vntime' size 17 bold
    @ 300, 600 LABEL LABEL_function_2 Value "y = x" font '.vntime' size 17 bold
    @ 370, 650 LABEL LABEL_function_3 Value "x = y*y/25" font '.vntime' size 17 bold
    @ 460, 650 LABEL LABEL_function_4 Value "y = 10*sin(x)" width 150 font '.vntime' size 17 bold
	end window

	mathematica.center

	mathematica.activate

return nil

function drawshapes
   LOCAL x1 := 0 , y1:= 0 , x2 := 0 , y2:= 0      
   draw line in window mathematica at 500,0 to 500,800 pencolor {0,0,0} penwidth 1
   draw line in window mathematica at 0,400 to 600,400 pencolor {0,0,0} penwidth 1
   
   FOR i := -200 to 200
      x1 := i - 1
      y1 := (i - 1)*(i - 1)/30
      x2 := i
      y2 := i*i/30   
      
      draw line in window mathematica at 500-y1,400+x1 to 500-y2,400+x2 pencolor {255,0,0} penwidth 2
        
   ENDFOR
   
   FOR i := 0 to 400
      x1 := i - 1
      y1 := sqrt(i - 1)*5
      x2 := i
      y2 := sqrt(i)*5   
      
      draw line in window mathematica at 500-y1,400+x1 to 500-y2,400+x2 pencolor {0,0,255} penwidth 2
        
   ENDFOR
   
   FOR i := -100 to 100
      x1 := 10*(i - 1)
      y1 := 10*sin(i - 1)
      x2 := 10*i
      y2 := 10*sin(i)   
      
      draw line in window mathematica at 500-y1,400+x1 to 500-y2,400+x2 pencolor {0,0,255} penwidth 2
        
   ENDFOR
   
   
   FOR i := 0 to 400
      x1 := i - 1
      y1 := i - 1
      x2 := i
      y2 := i   
      
      draw line in window mathematica at 500-y1,400+x1 to 500-y2,400+x2 pencolor {255,113,255} penwidth 2
   ENDFOR
        

return nil