I've been wanting to do some graphs in DB for awhile. I guess you just motivated me to make some.
Here's a simple 1D line graph and a bar graph. I added a little animation to the bar graph when the values change. Just hit the spacebar to update the graph values.
randomize timer()
Type _DataBar
value as integer
oldValue as integer
targetValue as integer
EndType
dim dataPoints(50)
for i = 1 to 50
dataPoints(i) = rnd(400)
next i
dim dataBars(8) as _DataBar
for i = 1 to 8
dataBars(i).value = rnd(400)
next i
sync on
do
cls
plotLineGraph(50, 50, 200, 100, 0, 500, 6)
drawBarGraph(300, 50, 300, 300, 0, 500, 6, 24)
if spacekey() = 1 and flag = 0
flag = 1
for i = 1 to 8
dataBars(i).oldValue = dataBars(i).value
dataBars(i).targetValue = rnd(400)
next i
for i = 1 to 50
dataPoints(i) = rnd(400)
next i
endif
if spacekey() = 0 then flag = 0
ink rgb(255,255,255),0
set cursor 0,0 : print "FPS: ",screen fps()
sync
loop
function drawBarGraph(x, y, width, height, minVal, maxVal, segments, barWidth)
ink rgb(48,48,48),0
box x, y, x+width, y+height
range = maxVal - minVal
segOff# = height / (segments+0.0)
ink rgb(96,96,96),0
for i = 0 to segments
yVal = y+segOff#*i
line x, yVal, x+width, yVal
value = maxVal - (segOff#*i*range)/height
w = text width(str$(value))
text x-w-4, yVal-8, str$(value)
next i
c = array count(dataBars())
leftover = width - (barWidth*c)
gap = leftover / c
offsetX = (width - ((c-1)*(barWidth + gap)+barWidth)) / 2
ink rgb(0,34,54), 0
base = y+height
for i = 1 to c
rem this is only to show a smooth transition when
rem bar data is shifted to new values
shiftSpeed = 2
if dataBars(i).value <> dataBars(i).targetValue
if dataBars(i).value > dataBars(i).targetValue
dataBars(i).value = dataBars(i).value - shiftSpeed
if dataBars(i).value < dataBars(i).targetValue then dataBars(i).value = dataBars(i).targetValue
else
dataBars(i).value = dataBars(i).value + shiftSpeed
if dataBars(i).value > dataBars(i).targetValue then dataBars(i).value = dataBars(i).targetValue
endif
endif
xVal = x+(i-1)*(barWidth + gap) + offsetX
yVal = base - (dataBars(i).value*height) / maxVal
box xVal, yVal, xVal+barWidth, base, rgb(125,205,255), rgb(125,205,255), rgb(0,119,195), rgb(0,119,195)
ty = yVal
if ty > y+height-text height(" ") then ty = y+height-text height(" ")
center text xVal+barWidth/2, ty, str$(dataBars(i).value)
next i
endfunction
function plotLineGraph(x, y, width, height, minVal, maxVal, segments)
ink rgb(48,48,48),0
box x, y, x+width, y+height
range = maxVal - minVal
segOff# = height / (segments+0.0)
ink rgb(96,96,96),0
for i = 0 to segments
yVal = y+segOff#*i
line x, yVal, x+width, yVal
value = maxVal - (segOff#*i*range)/height
w = text width(str$(value))
text x-w-4, yVal-8, str$(value)
next i
c = array count(dataPoints())
intX# = width / (c+0.0)
offsetX = intX#/2
r = 2
base = y+height
ink rgb(255,0,0),0
for i = 1 to c
xVal = x+intX#*i - offsetX
yVal = base - (dataPoints(i)*height) / maxVal
box xVal-r, yVal-r, xVal+r, yVal+r
if i > 1
line xVal, yVal, oldValX, oldValY
endif
oldValX = xVal
oldValY = yVal
next i
endfunction