made a simple bar chart thing to display values with their description. values and description are passed by arrays.
included a testing-program. just remove the commentary signs around TEST-BEGIN and TEST-END.
Code: Select all
'b'
/*
'*******TEST-BEGIN*******
'/Bibliothek/maths
INPUT test
ON test GOSUB test1
END
test1:
OPTION BASE 1
INPUT num_bars
DIM legend$(num_bars)
DIM values(num_bars)
FOR n= 1 TO num_bars
legend$(n)=str_rnd$(i_rnd(10,15))
values(n)=r_rnd(0,1)
NEXT n
'PAUSE 2
GRAPHICS
GRAPHICS CLEAR 1,1,1
bar_chart(legend$,values,10,10,SCREEN_WIDTH()*.4,SCREEN_HEIGHT()*.8)
PAUSE 1
bar_chart.complete_bar=1
bar_chart(legend$,values,10,10,SCREEN_WIDTH()*.4,SCREEN_HEIGHT()*.8)
PAUSE 1
bar_chart.framed=1
bar_chart(legend$,values,10,10,SCREEN_WIDTH()*.4,SCREEN_HEIGHT()*.8)
PAUSE 1
GRAPHICS CLEAR 1,1,1
FOR n=1 TO num_bars-1 STEP 5
bar_chart2(legend$,values,10,10,SCREEN_WIDTH()*.4,SCREEN_HEIGHT()*.8,n,n+4)
PAUSE 1
GRAPHICS CLEAR 1,1,1
NEXT n
RETURN 'of test1
'********TEST-END********
*/
'c'
DEF bar_chart(legend$(),values(),pos_x,pos_y,width,height) 'array$(index,1:name of category, 2: value [0...1]
'legend$():array that contains the legend / description of each bar
'values():array that contains the corresponding values(range:[0...1]) of each bar.
'pos_x,pos_y upper left position of diagram, width, height selfexplanatory
'complete_bar=1 'completes the bar to 100% with red colour
'fraimed: 'draws a rectangle around the bars
GET DIM legend$ XSIZE num_legends
GET DIM values XSIZE num_values
IF num_values<>num_legends THEN RETURN -1
num_bars=num_values
OPTION SORT DESCENDING
SORT values AS ordered_values
'***VARIABLES***
category_fontsize=25 'fontsize legende
category_font$="Avenir-Book" 'fontname of legende
v_dist_bar=bar_height*0.25 'gab between bars
h_dist_bar=width*0.01 'gab before bar
rel_dist_bar=.25 'relation dist/bar -> v_dist_bar=bar_height*rel_dist_bar
FILL COLOR 0,1,0
DRAW COLOR 0,0,0
IF framed=1 THEN DRAW RECT pos_x,pos_y TO pos_x+width+10,pos_y+height
'height=num_bars*bar_height+num_bars*v_dist_bar distance between bars + 2*1/2 on both end
bar_height=height/((1+rel_dist_bar)*num_bars) 'height of each bar
v_dist_bar=bar_height*rel_dist_bar 'distance between each bar, 1/2 distance at both ends
'width=max_text_length+max_bar_length
'calculating max_length of text
DRAW FONT SIZE category_fontsize
DRAW FONT NAME category_font$
'checking text_height
IF TEXT_HEIGHT("asdfghj") > bar_height THEN
FOR n=category_fontsize TO 0 STEP -.1
DRAW FONT SIZE n
IF TEXT_HEIGHT("asdfghj") < bar_height THEN BREAK
NEXT n
ENDIF
max_text_length=0
FOR n= 1 TO num_bars
IF TEXT_WIDTH(legend$(n))>max_text_length THEN max_text_length=TEXT_WIDTH(legend$(n))
NEXT n
'calculationg bars
max_bar_length = width-max_text_length-h_dist_bar
pos_x_bar=x+max_text_length+h_dist_bar 'upper left position of bars
p_x=pos_x+max_text_length+h_dist_bar
IF complete_bar THEN
FILL COLOR 1,0,0
n=0
FOR p_y= pos_y+v_dist_bar/2 TO pos_y+height STEP bar_height+v_dist_bar 'more or less
n+=1
FILL RECT p_x+max_bar_length*values(Ordered_values(N)),p_y TO p_x+max_bar_length,p_y+bar_height
NEXT p_y
ENDIF
'drawing bars
FILL COLOR 0,1,0
n=0
FOR p_y= pos_y+v_dist_bar/2 TO pos_y+height STEP bar_height+v_dist_bar 'more or less
n+=1
FILL RECT p_x,p_y TO p_x+max_bar_length*values(Ordered_values(N)),p_y+bar_height
percentage$=STR$(INT(values(ordered_values(n))*100))&"%"
IF p_x+max_bar_length*values(Ordered_values(N))-TEXT_WIDTH(percentage$)>p_x THEN
DRAW TEXT percentage$ AT p_x+max_bar_length*values(Ordered_values(N))-TEXT_WIDTH(percentage$),p_y
ELSE
DRAW TEXT percentage$ AT p_x,p_y
ENDIF
DRAW TEXT legend$(Ordered_values(N)) AT p_x-TEXT_WIDTH(legend$(Ordered_values(N)))-h_dist_bar,p_y+bar_height/2-TEXT_HEIGHT(legend$(Ordered_values(N)))/2
NEXT p_y
END DEF
'**************************
'**************************
DEF bar_chart2(legend$(),values(),pos_x,pos_y,width,height,data1,data2) 'array$(index,1:name of category, 2: value [0...1]
'same as above
'plus:
'data1: the first datum to be displayed
'data2: the last datum to be displayed
IF framed THEN DRAW RECT pos_x,pos_y TO pos_x+width+10,pos_y+height
GET DIM legend$ XSIZE num_legends
GET DIM values XSIZE num_values
IF num_values<>num_legends THEN RETURN -1
IF data2>num_values THEN data2=num_values
IF data1>data2 THEN RETURN -1
num_bars=data2-data1+1
OPTION SORT DESCENDING
SORT values AS ordered_values
'***VARIABLES***
category_fontsize=25 'fontsize legende
category_font$="Avenir-Book" 'fontname of legende
v_dist_bar=bar_height*0.25 'gab between bars
h_dist_bar=width*0.01 'gab before bar
rel_dist_bar=.25 'relation dist/bar -> v_dist_bar=bar_height*rel_dist_bar
FILL COLOR 0,1,0
DRAW COLOR 0,0,0
'height=num_bars*bar_height+num_bars*v_dist_bar distance between bars + 2*1/2 on both end
bar_height=height/((1+rel_dist_bar)*num_bars) 'height of each bar
v_dist_bar=bar_height*rel_dist_bar 'distance between each bar, 1/2 distance at both ends
'width=max_text_length+max_bar_length
'calculating max_length of text
DRAW FONT SIZE category_fontsize
DRAW FONT NAME category_font$
'checking text_height
IF TEXT_HEIGHT("asdfghj") > bar_height THEN
FOR n=category_fontsize TO 0 STEP -.1
DRAW FONT SIZE n
IF TEXT_HEIGHT("asdfghj") < bar_height THEN BREAK
NEXT n
ENDIF
max_text_length=0
FOR n= 1 TO num_bars
IF TEXT_WIDTH(legend$(n))>max_text_length THEN max_text_length=TEXT_WIDTH(legend$(n))
NEXT n
'calculationg bars
max_bar_length = width-max_text_length-h_dist_bar
pos_x_bar=x+max_text_length+h_dist_bar 'upper left position of bars
p_x=pos_x+max_text_length+h_dist_bar
IF complete_bar THEN
FILL COLOR 1,0,0
n=data1-1
FOR p_y= pos_y+v_dist_bar/2 TO pos_y+height STEP bar_height+v_dist_bar 'more or less
n+=1
FILL RECT p_x+max_bar_length*values(Ordered_values(N)),p_y TO p_x+max_bar_length,p_y+bar_height
IF n=data2 THEN BREAK
NEXT p_y
ENDIF
'drawing bars
FILL COLOR 0,1,0
n=data1-1
FOR p_y= pos_y+v_dist_bar/2 TO pos_y+height STEP bar_height+v_dist_bar 'more or less
n+=1
FILL RECT p_x,p_y TO p_x+max_bar_length*values(Ordered_values(N)),p_y+bar_height
percentage$=STR$(INT(values(ordered_values(n))*100))&"%"
IF p_x+max_bar_length*values(Ordered_values(N))-TEXT_WIDTH(percentage$)>p_x THEN
DRAW TEXT percentage$ AT p_x+max_bar_length*values(Ordered_values(N))-TEXT_WIDTH(percentage$),p_y
ELSE
DRAW TEXT percentage$ AT p_x,p_y
ENDIF
DRAW TEXT legend$(Ordered_values(N)) AT p_x-TEXT_WIDTH(legend$(Ordered_values(N)))-h_dist_bar,p_y+bar_height/2-TEXT_HEIGHT(legend$(Ordered_values(N)))/2
IF n=data2 THEN BREAK
NEXT p_y
END DEF
''
'excerpt from maths-library
DEF str_rnd$(L)
letter$=""
FOR n=1 TO l
letter$= letter$&CHR$(i_rnd(ASC("a"),ASC("z")))
NEXT n
RETURN letter$
END DEF
DEF i_rnd(a,b) 'returns random integer between two given numbers [a,b]
i_rnd=FLOOR((b+1-a) *RND(1)+a)
END DEF
DEF r_rnd(a,b) 'returns random real number between two given real numbers (a,b)
r_rnd=(b-a)*RND(1)+a
END DEF