|
Post by tsh73 on Sept 9, 2023 15:17:08 GMT -5
Here's code based on Graphics 101 – plotting a function alycesrestaurant.com/lbpe/Graphics101plotting.htmlIt produces this Hopefully you can tune it to your needs. 'bar graph with the months placed horizontally and the values vertically.
global width, minX, maxX, height, minY, maxY
WindowWidth = 500 'default is about 300, too cmall for 12 months labels 'WindowHeight = 500 'to see more labels along Y open "barChart" for graphics_nsb_nf as #grFunc print #grFunc, "trapclose [trapcloseLabel]" print #grFunc, "home ; down ; posxy x y" 'x, y give us width, height width = 2*x : height = 2*y #grFunc, "place 0 0" #grFunc, "\ " #grFunc, "posxy dummy charH" 'notice charH '16
'sample interval a = 1 b = 12 'months
'randomly create data global y$ randomize 0.5 maxVal =200 minVal =50
y$="" for i = 1 to 12 y$=y$+" "+using("####", rnd(0)*(maxVal-minVal) +minVal) next
print y$ 'or you can enter it by yourself, like ' y$=" 215 102 97 81 238 114 114 135 114 84 218 189" 'or read in from somewhere ' lbl$="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
n = 12 'nPoints h = 1 '=(b-a)/n, but not here maxX = b+h 'X range so axes are on a graph minX = a-5*h ', and there is a place for Y labels
'get y bounds minY = 1e10 maxY = -1e10 for x = a to b step h y = func(x) if y< minY then minY = y if y> maxY then maxY = y next x print "ymin, ymax ", minY, maxY 'fix it so axes are on the graph maxY = maxVal minY = 0-minVal 'and there is a place for X labels print "ymin, ymax ", minY, maxY
'axes 'xxmin = sx(a) : xxmax = sx(b) xxmin = sx(minX) : xxmax = sx(maxX) y0 = sy(0) ' #grFunc "color red" print #grFunc, "line ";xxmin;" ";y0;" "; xxmax;" ";y0 'horiz from A to B print #grFunc, "place ";xxmax-15;" ";y0-5 print #grFunc, "\X" ' #grFunc "color blue" yymin = sy(minY) : yymax = sy(maxY) : x0 = sx(0) print #grFunc, "line ";x0;" ";yymin;" "; x0;" ";yymax 'vert ymin to ymax print #grFunc, "place ";x0-15;" ";yymax+15 print #grFunc, "\Y" 'need ticks on vert axe dy=1 while 1 dy=dy*5 'notice sy(0)-sy(dy) if sy(0)-sy(dy)>charH then exit while dy=dy*2 if sy(0)-sy(dy)>charH then exit while 'this ends up with possible values like 5, 10, 50, 100, 500, 1000 wend for y = dy to maxY step dy print #grFunc, "line ";x0;" ";sy(y);" "; x0-10;" ";sy(y) print #grFunc, "place ";x0-15-3*10;" ";sy(y)+charH/2 print #grFunc, "\";y next
#grFunc "color black" y = sy(func(a)) print #grFunc, "place a "; y
for x1 = a to b step h x=int(x1+.5) 'make sure it is integer y = sy(func(x)) xx = sx(x) xx2 = sx(x-h) 'print x, func(x), xx, y #grFunc "backcolor ";rainbow$(x1/12) #grFunc "color black" print #grFunc, "place "; xx; " "; sy(0) print #grFunc, "boxfilled "; xx2; " "; y 'draw label (supposed to be month) #grFunc "backcolor white" #grFunc "color black" lb$ = word$(lbl$, x) print #grFunc, "place "; xx2+5; " "; sy(0)+15 for j = 1 to len(lb$) print #grFunc, "\";mid$(lb$, j, 1) next next
print #grFunc, "flush" wait end
[trapcloseLabel] close #grFunc end
'****************************************** 'sample function function func(x) 'func = sin(x)-3*cos(3*x) 'func = sin(x) func = val(word$(y$, x)) 'y$ is space delimited list of numbers. X is from 1 end function
'conversions (logical coords to screen) function sx(x) 'screen X. Depends on width, minX, maxX sx = (x- minX)/(maxX-minX) * width end function
function sy(y) 'screen Y. Depends on height, minY, maxY. Upside down. sy = (1-(y- minY)/(maxY-minY)) * height end function
'--------------------------------------------- ' 0..1 into red-green-blue-red continuous colors function rainbow$(x) hi = int((x*6) mod 6)+ 5*(x<0) 'fixed to 0..5 f = (x*6) mod 1 + (x<0) 'frac, 0..1 q = (1-f) select case hi case 0 r = 1: g = f: b = 0 case 1 r = q: g = 1: b = 0 case 2 r = 0: g = 1: b = f case 3 r = 0: g = q: b = 1 case 4 r = f: g = 0: b = 1 case 5 r = 1: g = 0: b = q end select R = int(r*255) G = int(g*255) B = int(b*255) rainbow$= R;" ";G;" ";B end function
|
|
|
Post by tsh73 on Sept 10, 2023 5:28:41 GMT -5
Somewhat cleaned code (unnecessary "print" omitted etc). I made sure it works with [a,b] not exactly [1,12]
'tsh73 Sep 2023 'bar graph with the months placed horizontally and the values vertically. 'Based on chart example at ' https://alycesrestaurant.com/lbpe/Graphics101plotting.html
global width, minX, maxX, height, minY, maxY
WindowWidth = 500 'default is about 300, for me too small for 12 months labels 'WindowHeight = 500 'to see more labels along Y open "barChart" for graphics_nsb_nf as #grFunc #grFunc "trapclose [trapcloseLabel]" #grFunc "home ; down ; posxy x y" 'x, y give us width, height width = 2*x : height = 2*y 'here we get line height (if you change font, it should be after change) #grFunc "place 0 0" #grFunc "\ " #grFunc "posxy dummy charH" 'notice charH '16
'sample interval a = 1 b = 12 '12 months, supposed use 'a = 4 'works too 'b = 5 'works too, though left empty field is too big
'randomly create data global y$ 'randomize 0.5 maxVal =200 minVal =50
y$="" for i = 1 to 12 y$=y$+" "+using("####", rnd(0)*(maxVal-minVal) +minVal) next
print y$ 'or you can enter it by yourself, like ' y$=" 215 102 97 81 238 114 114 135 114 84 218 189" 'or read in from somewhere ' lbl$="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
n = 12 'nPoints h = 1 '=(b-a)/n, but not here maxX = b+h 'X range so axes are on a graph minX = 0-3*h ', and there is a place for Y labels to the left of 0 (trial and error)
'get y bounds minY = 1e10 maxY = -1e10 for x = a to b step h y = func(x) if y< minY then minY = y if y> maxY then maxY = y next x print "computed ymin, ymax ", minY, maxY
'fix it so axes are on the graph maxY = maxVal minY = 0-minVal 'and there is a place for X labels below 0 print "but we use (so axes is visble) ymin, ymax ", minY, maxY
'axes 'xxmin = sx(a) : xxmax = sx(b) xxmin = sx(minX) : xxmax = sx(maxX) y0 = sy(0) ' #grFunc "color red" #grFunc "line ";xxmin;" ";y0;" "; xxmax;" ";y0 'horiz from A to B #grFunc "place ";xxmax-15;" ";y0-5 #grFunc "\X" ' #grFunc "color blue" yymin = sy(minY) : yymax = sy(maxY) : x0 = sx(0) #grFunc "line ";x0;" ";yymin;" "; x0;" ";yymax 'vert ymin to ymax #grFunc "place ";x0-15;" ";yymax+15 #grFunc "\Y"
'need ticks on vert axe dy=1 while 1 dy=dy*5 'notice sy(0)-sy(dy) if sy(0)-sy(dy)>charH then exit while dy=dy*2 if sy(0)-sy(dy)>charH then exit while 'this ends up with possible values like 5, 10, 50, 100, 500, 1000 wend
for y = dy to maxY step dy #grFunc "line ";x0;" ";sy(y);" "; x0-10;" ";sy(y) #grFunc "place ";x0-15-3*10;" ";sy(y)+charH/2 #grFunc "\";y next
#grFunc "color black" y = sy(func(a)) #grFunc "place a "; y
for x1 = a to b step h x=int(x1+.5) 'make sure it is integer y = sy(func(x)) xx = sx(x) xx2 = sx(x-h) 'print x, func(x), xx, y #grFunc "backcolor ";rainbow$(x1/b) #grFunc "color black" #grFunc "place "; xx; " "; sy(0) #grFunc "boxfilled "; xx2; " "; y 'draw label (supposed to be month) #grFunc "backcolor white" #grFunc "color black" lb$ = word$(lbl$, x) #grFunc "place "; xx2+5; " "; sy(0)+15 for j = 1 to len(lb$) #grFunc "\";mid$(lb$, j, 1) next next
#grFunc "flush" wait end
[trapcloseLabel] close #grFunc end
'****************************************** 'sample function function func(x) 'func = sin(x)-3*cos(3*x) 'func = sin(x) func = val(word$(y$, x)) 'y$ is space delimited list of numbers. X is from 1 end function
'conversions (logical coords to screen) function sx(x) 'screen X. Depends on width, minX, maxX sx = (x- minX)/(maxX-minX) * width end function
function sy(y) 'screen Y. Depends on height, minY, maxY. Upside down. sy = (1-(y- minY)/(maxY-minY)) * height end function
'--------------------------------------------- ' 0..1 into red-green-blue-red continuous colors function rainbow$(x) hi = int((x*6) mod 6)+ 5*(x<0) 'fixed to 0..5 f = (x*6) mod 1 + (x<0) 'frac, 0..1 q = (1-f) select case hi case 0 r = 1: g = f: b = 0 case 1 r = q: g = 1: b = 0 case 2 r = 0: g = 1: b = f case 3 r = 0: g = q: b = 1 case 4 r = f: g = 0: b = 1 case 5 r = 1: g = 0: b = q end select R = int(r*255) G = int(g*255) B = int(b*255) rainbow$= R;" ";G;" ";B end function
|
|
emy
New Member
Hello
Posts: 45
|
Post by emy on Sept 11, 2023 4:17:59 GMT -5
Hello angelo2449, Following code : other way for bargraph
'======================================================== 'BARGRAF.bas '========================================================
'Values couleur(1,u1) = 100 couleur(2,u2) = 150 couleur(3,u3) = 350 couleur(4,u4) = 180 couleur(5,u5) = 60 '320 couleur(6,u6) = 400 couleur(7,u7) = 520 couleur(8,u8) = 310 couleur(9,u9) = 30
'Choix des couleurs c$(1) = "black" : c$(2) = "blue" : c$(3) = "red" c$(4) = "green" : c$(5) = "cyan" : c$(6) = "pink" c$(7) = "brown" : c$(8) = "darkgreen" : c$(9) = "white"
Nomainwin WindowWidth = 500 : WindowHeight = 700 '565 UpperLeftX = 320 : UpperLeftY = 200 '..............................................Echelle statictext #sts.st2, "600", 15, 36, 20, 20 statictext #sts.st3, "550", 15, 86, 20, 20 statictext #sts.st4, "500", 15, 136, 20, 20 statictext #sts.st5, "450", 15, 186, 20, 20 statictext #sts.st6, "400", 15, 236, 20, 20 statictext #sts.st7, "350", 15, 286, 20, 20 statictext #sts.st8, "300", 15, 336, 20, 20 statictext #sts.st89, "250", 15, 386, 20, 20 statictext #sts.st10, "200", 15, 436, 20, 20 statictext #sts.st11, "150", 15, 486, 20, 20 statictext #sts.st12, "100", 15, 536, 20, 20 statictext #sts.st13, "50", 20, 586, 20, 20 statictext #sts.st14, "0", 25, 636, 15, 20 '.............................................. statictext #sts.stt1, "Noir", 435, 125, 40, 20 statictext #sts.stt2, "Bleu", 435, 175, 40, 20 statictext #sts.stt3, "Rouge", 435, 225, 40, 20 statictext #sts.stt4, "Vert clair", 435, 275, 40, 20 statictext #sts.stt5, "Cyan", 435, 325, 40, 20 statictext #sts.stt6, "Pink", 435, 375, 40, 20 statictext #sts.stt7, "Brown", 435, 425, 40, 20 statictext #sts.stt8, "Vert foncÈ", 435, 475, 40, 20 statictext #sts.stt9, "Blanc", 435, 525, 40, 20 '.............................................. stylebits #sts.gbs, 0, _WS_BORDER, 0, 0 graphicbox #sts.gbs, 40, 20, 340, 626 '.............................................. graphicbox #sts.g1, 400, 120, 20, 30 graphicbox #sts.g2, 400, 170, 20, 30 graphicbox #sts.g3, 400, 220, 20, 30 graphicbox #sts.g4, 400, 270, 20, 30 graphicbox #sts.g5, 400, 320, 20, 30 graphicbox #sts.g6, 400, 370, 20, 30 graphicbox #sts.g7, 400, 420, 20, 30 graphicbox #sts.g8, 400, 470, 20, 30 graphicbox #sts.g9, 400, 520, 20, 30 '....................................... open "Statistique" for window_nf as #sts #sts, "trapclose [quitsts]" '....................................... #sts.gbs, "cls" #sts.gbs, "down; size 1; fill 255 255 170" 'white #sts.gbs, "color 100 100 100" 'Gray color '....................................... #sts.gbs, "line 0 0 0 624" 'Ligne verticale du dÈbut #sts.gbs, "line 339 0 339 624" 'Ligne horizontale de la fin '....................................... 'Black lines for y = 625 to 0 step -50 #sts.gbs, "line ";"0";" ";y;" ";340;" ";y next y #sts.gbs, "line ";"0";" ";"0";" ";340;" ";"0" 'Color Bars #sts.gbs, "color black" for i = 1 to 20 #sts.gbs, "line ";20+i;" ";625 - couleur(1,u1);" ";20+i;" ";625 next i
#sts.gbs, "color blue" for i = 1 to 20 #sts.gbs, "line ";55+i;" ";625 - couleur(2,u2);" ";55+i;" ";625 next i
#sts.gbs, "color red" for i = 1 to 20 #sts.gbs, "line ";90+i;" ";625 - couleur(3,u3);" ";90+i;" ";625 next i
#sts.gbs, "color green" for i = 1 to 20 #sts.gbs, "line ";125+i;" ";625 - couleur(4,u4);" ";125+i;" ";625 next i
#sts.gbs, "color cyan" for i = 1 to 20 #sts.gbs, "line ";160+i;" ";625 - couleur(5,u5);" ";160+i;" ";625 next i
#sts.gbs, "color pink" for i = 1 to 20 #sts.gbs, "line ";195+i;" ";625 - couleur(6,u6);" ";195+i;" ";625 next i
#sts.gbs, "color brown" for i = 1 to 20 #sts.gbs, "line ";230+i;" ";625 - couleur(7,u7);" ";230+i;" ";625 next i
#sts.gbs, "color darkgreen" for i = 1 to 20 #sts.gbs, "line ";265+i;" ";625 - couleur(8,u8);" ";265+i;" ";625 next i
#sts.gbs, "color black" for i = 1 to 20 #sts.gbs, "line ";300+i;" ";625 - couleur(9,u9);" ";300+i;" ";625 next i
#sts.gbs, "color white" for i = 1 to 18 #sts.gbs, "line ";301+i;" ";625 - couleur(9,u9);" ";301+i;" ";625 next i
#sts.gbs, "color black" #sts.gbs, "line ";301;" ";625 - couleur(9,u9)-1;" ";300+21;" ";625 - couleur(9,u9)-1
#sts.gbs, "flush"
#sts.g1, "fill black;flush" #sts.g2, "fill blue;flush" #sts.g3, "fill red;flush" #sts.g4, "fill green;flush" #sts.g5, "fill cyan;flush" #sts.g6, "fill pink;flush" #sts.g7, "fill brown;flush" #sts.g8, "fill darkgreen;flush" #sts.g9, "fill white;flush"
for i = 1 to 9 var1$ = "#sts.stt" + str$(i) #var1$, "!font Comic_Sans_MS 12" next i
#sts.stt1, using("###",couleur(1,u1)) #sts.stt2, using("###",couleur(2,u2)) #sts.stt3, using("###",couleur(3,u3)) #sts.stt4, using("###",couleur(4,u4)) #sts.stt5, using("###",couleur(5,u5)) #sts.stt6, using("###",couleur(6,u6)) #sts.stt7, using("###",couleur(7,u7)) #sts.stt8, using("###",couleur(8,u8)) #sts.stt9, using("###",couleur(9,u9)) wait '--------------- [quitsts] close #sts wait
|
|
|
Post by meerkat on Sept 11, 2023 7:52:53 GMT -5
In RB the easy way is to simply create tables and fill the cells with data, tell it to align, height, width, color etc. To do the same thing in LB you can write it to a html file and then click on it.
open "c:\data\temp\bar.html" for output as #f
print #f,"<HTML><HEAD><TITLE>Bar Chart</TITLE></HEAD><BODY>" barNames$ = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov" incr$ = "200 150 100 50" y$ = "215 102 97 81 238 114 114 135 114 84 218 189" maxy = 215 ' The big table
print #f,"<TABLE BORDER='1' CELLPADDING='0' CELLSPACING='0' WIDTH='100%'> <TR><TD WIDTH='10%' valign=bottom> <TABLE BORDER='1' CELLPADDING='0' CELLSPACING='0' HEIGHT='200'>" ' table for percents for i = 10 to 1 step -1 print #f, "<TR><TD><TD ALIGN=CENTER width=100%>";i * 10;"</TD></TR>" next i print #f,"</TABLE></TD>"
' cell for the 12 values for i = 1 to 12 yVal = val(word$(y$,i)) pcnt = (yVal / maxy) * 100
print #f, "<TD WIDTH='30' VALIGN='BOTTOM'> <TABLE BORDER='0' CELLPADDING='0' CELLSPACING='1' WIDTH='100%' HEIGHT='";pcnt * 2;"' > <TR><TD align=center WIDTH='100%' style='background-color:rgb(";yVal;",0,0)'><FONT COLOR='white'>";yVal;"<BR>";int(pcnt);"%</TD></TR></TABLE></TD>" NEXT i
'cell for the months print #f, "<TR><TD WIDTH='10%' ALIGN='RIGHT'>Month</TD>" for i = 1 to 12 print #f, "<TD align=center>";word$(barNames$,i);"</TD>" next i print #f, "</TR></TABLE> </BODY> </HTML>" close #f end
|
|
|
Post by xxgeek on Sept 12, 2023 7:14:35 GMT -5
meerkatIt even works in LB if you join long strings like print #f, "</TR></TABLE> </BODY> </HTML>" And change the file path to a path that exists and you have rights to. It will work in JB, LB, and Pro - Linux/WINE too. With a line added to open it in the users default browser. page$ = DefaultDir$;"\bar.html" open page$ for output as #f 'open "c:\data\temp\bar.html" for output as #f print #f,"<HTML><HEAD><TITLE>Bar Chart</TITLE></HEAD><BODY>" barNames$ = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" incr$ = "200 150 100 50" y$ = "215 102 97 81 238 114 114 135 114 84 218 189" maxy = 215 ' The big table
print #f,"<TABLE BORDER='1' CELLPADDING='0' CELLSPACING='0' WIDTH='100%'><TR><TD WIDTH='10%' valign=bottom><TABLE BORDER='1' CELLPADDING='0' CELLSPACING='0' HEIGHT='200'>" ' table for percents for i = 10 to 1 step -1 print #f, "<TR><TD><TD ALIGN=CENTER width=100%>";i * 10;"</TD></TR>" next i print #f,"</TABLE></TD>"
' cell for the 12 values for i = 1 to 12 yVal = val(word$(y$,i)) pcnt = (yVal / maxy) * 100
print #f, "<TD WIDTH='30' VALIGN='BOTTOM'> <TABLE BORDER='0' CELLPADDING='0' CELLSPACING='1' WIDTH='100%' HEIGHT='";pcnt * 2;"' ><TR><TD align=center WIDTH='100%' style='background-color:rgb(";yVal;",0,0)'><FONT COLOR='white'>";yVal;"<BR>";int(pcnt);"%</TD></TR></TABLE></TD>" NEXT i
'cell for the months print #f, "<TR><TD WIDTH='10%' ALIGN='RIGHT'>Month</TD>" for i = 1 to 12 print #f, "<TD align=center>";word$(barNames$,i);"</TD>" next i print #f, "</TR></TABLE></BODY></HTML>" close #f run "explorer ";page$ end
|
|
|
Post by badbug on Sept 12, 2023 15:26:39 GMT -5
The code I posted was just to show 'how to' I cleaned it up a little and made more random RGB colors. I also made the bars to fill 80% instead of the 100%. So narrower bars.
page$ = DefaultDir$;"\bar.html" 'open page$ for output as #f 'open "c:\data\temp\bar.html" for output as #f print #f,"<HTML><HEAD><TITLE>Bar Chart</TITLE></HEAD><BODY>" barNames$ = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" incr$ = "200 150 100 50" y$ = "215 102 97 81 238 114 114 135 114 84 218 189" maxy = 215 ' The big table
print #f,"<TABLE BORDER=0 CELLPADDING='0' CELLSPACING='0' WIDTH='100%'><TR><TD WIDTH='10%' valign=bottom><TABLE BORDER='1' CELLPADDING='0' CELLSPACING='0' HEIGHT='200'>" ' table for percents for i = 10 to 1 step -1 print #f, "<TR><TD><TD bgcolor=silver ALIGN=CENTER width=100%>";i * 10;"</TD></TR>" next i print #f,"</TABLE></TD>"
' cell for the 12 values for i = 1 to 12 yVal = val(word$(y$,i)) pcnt = int((yVal / maxy) * 100) r = int(rnd(0) * yVal) g = int(rnd(0) * (pcnt *2)) b = int(rnd(0) * 200) print #f, "<TD VALIGN='BOTTOM'> <TABLE BORDER='0' CELLPADDING='0' CELLSPACING='1' WIDTH=80% HEIGHT='";pcnt * 2;"' ><TR><TD align=center WIDTH='100%' style='background-color:rgb(";r;",";g;",";b;")'><FONT COLOR='white'>";yVal;"<BR>";int(pcnt);"%</TD></TR></TABLE></TD>" NEXT i
'cell for the months print #f, "<TR align=center BGCOLOR=silver><TD WIDTH=30 ALIGN=RIGHT>Month</TD> <TD width=30 >";replstr$(barNames$," ","</TD><TD width=30>");"</TD> </TR></TABLE></BODY></HTML>" close #f run "explorer ";page$ end
|
|