Post by tsh73 on Sept 9, 2023 20:19:28 GMT
By the request on LB forum
Here's code based on
Graphics 101 – plotting a function
alycesrestaurant.com/lbpe/Graphics101plotting.html
It produces this
Might be tuned more - this is BASIC.
I would like to create a dynamic bar graph with the months placed horizontally and the values vertically.
Here's code based on
Graphics 101 – plotting a function
alycesrestaurant.com/lbpe/Graphics101plotting.html
It produces this
Might be tuned more - this is BASIC.
'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 small 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