A Bar Graph in Alpha Five?

By Jack Wheeler

Some time ago Dan Blank showed us how he creates horizontal bar graphs in Alpha Five. Jack Wheeler ups the ante by creating vertically-aligned graphs for his business. You can reach Jack at jwl1@jps.net.

To be honest, I thought I would never see the day Alpha Five would allow Bar Graphs (or any graphing utility) within its realm. However, after reading the famous article by Peter Wayne on Towers of Hanoi, a flash hit me. If Alpha will allow object manipulation (location, size, style, font color, background color) then Graphing should be possible.

Figure4

Figure 1. Example graph.

This was the game plan:

  1. Base graph on two tables, my receipts journal table (Monies received) and my Invoicing table (monies anticipated).
  2. Find statistical reference via the graph for 15 months. This way I could see what was billed each month and what was received each month. I should be able to see what were busy months and what were slow months when I did not do well on receiving money.
  3. Develop a form with 15 variable field and name them D1 through D15. It must be consecutive numbering. These would be used as the BARS.
  4. Develop 15 text items that I would use to give me the total amounts for each month. This would have to be number consecutively as well. Here I numbered them text3 through text17. (As long as there is some kind of pattern to the numbering sequence I could use xbasic to call each object.)
  5. Use the Y bar as the consistent bar and the X bar would need to be changed depending on the months involved. (How do I find the months to start or end with?)

Figure2

Figure 2. See the text fields

Here is the form with the variables shown:

Figure3

Figure 3. See all the fields and variable display

Now I am ready to begin.

I created a button on the form called Invoicing by month. This is where I put the code for the actual graph. Here is the Code:

ui_freeze(.t.)
dim h as n
dim i as n
dim filename as p
dim value as c
dim txt as c
dim txtname as p
dim J as n
DIM shared DATE AS D
dim idx as p
dim nrec as n
DATE=(tablemax("e:\a5v4\jw\Invoice2.dbf",".t.","Date"))
var->mnth=month(date)
var->yrth=year(date)
CONSTANT SHARED mon=month(date)
CONSTANT SHARED yr=year(date)
frm=parentform.this
i=tablesum("E:\a5v4\jw\invoice2.dbf",\
"var->mnth=month(date).and.var->yrth=year(date)","subtotal")
frm:text3.text=str(i)
value="d1"
FILENAME=OBJ(VALUE)
FILENAME.FILL.FORECOLOR="BLUE"
H=I/52000*3.25
if h<=3.15 then 
filename.object.height=h
filename.object.top=3.25-h
frm:text3.object.top=3.25-h
else
frm:text3.object.top=0
end if
'debug(1)
for j=1 to 14
value="d"+alltrim(str(J+1))
var->mnth=graph(J)
var->yrth=graphyr(date,J)
i=tablesum("E:\a5v4\jw\invoice2.dbf",\
"var->mnth=month(date).and.var->yrth=year(date)","subtotal")
txt="text"+alltrim(str(J+3))
txtname=obj(txt)
txtname.text=str(i)
filename=obj(value)
filename.fill.forecolor="Blue"
h=i/52000 *3.15
If h<=3.15 then 
filename.object.height=h
filename.object.top=3.25-h
txtname.object.top=3.25-h
else
filename.object.height=h
filename.object.top=3.25-h
txtname.object.top=0
end if
next
frm:text1.text=" 50K 48K 46K 44K 42K 40K 38K 36K 34K 32K 30K 28K 26K
24K 22K 20K 18K 16K 14K 12K 10K 8K 6K" 
frm:text2.text=" "+str(graph(14),2,0)+"
"+str(graph(13),2,0)+" "+str(graph(12),2,0)+\
" "+str(graph(11),2,0)+" "+str(graph(10),2,0)+"
"+str(graph(9),2,0)+" "+\
str(graph(8),2,0)+" "+str(graph(7),2,0)+"
"+str(graph(6),2,0)+" "+\
str(graph(5),2,0)+" "+str(graph(4),2,0)+"
"+str(graph(3),2,0)+" "+\
str(graph(2),2,0)+" "+str(graph(1),2,0)+"
"+str(graph(0),2,0) 
frm.resynch()
ui_freeze(.f.)
end

If you look at this code carefully you will see that it is quite simple. This illustration helps with the numbers:

Figure1

Figure 4. Size of field objects

As you can see the top of the object is at 0 inches and the bottom of the object is at 3.15. The maximum Y-value of the graph is 52,000. If I started doing sales of 100,000 dollars per month, I would change the 52,000 to 100,000. I would have to play around with the text object to get the numbers to work right. It would seem that placing the numbers with 2 spaces between each instead of 4 would space them out okay.

You will notice that I have called two functions in this script. Here they are.

(I used them to save time in repeat typing)

GRAPHYR
function Graphyr as N(date as d,Place as N)
dim place as n
dim yrly as n
DIM DATE1 AS D
cmon=mon-place
DATE1=ADDMONTHS(DATE,-PLACE)
YRLY=YEAR(DATE1) 
Graphyr=yrly
end function
'GRAPH Function-----
dim cmon as n
dim mont as n
DIM DATE AS D
DIM DATE1 AS D
function Graph as N(Place as N)
DATE1=ADDMONTHS(DATE,-PLACE)
MONT=MONTH(DATE1)
graph=mont
end function
END

Well where do you go from here?

This is just the beginning. From here you can create a variable to select a base table (in which you ask the user to select from a list: after all you do not want to use your calendar program as a graph choice) and then ask for a date to start the graph with: presto you have a flexible embeddable graph, all within Alpha Five.

Click here to download the graphs, scripts and sample data for this article!

12/11/99

Don't forget, we need your feedback to make this site better!

Return to home