Page 1 of 1

Vertical Bar Chart Example

Posted: Thu May 24, 2007 7:37 am
by electrochrisso
A vertical bar chart example with various settings that can be adjusted.

Code: Select all

;
; Bar Chart Vertical V1.1 - By Electrochrisso
;
; Free to use/modify at your own risk
;
; Using GadgetToolTip to display actual month totals when mouse pointer over Bar Titles
; and the Mean Line gadget on the right of the chart.
;
; Many adjustments can be made, plenty of scope for more additions.
;
; Last Change: 18 July 2007
;

CreateImage(200,300,30)
LoadFont(0,"Arial",10) ; Change Font Here
Dim ChartData(1,11)     ;Chart Amounts and Bar Color
Dim BarTitles.s(11)     ;Bar Text Titles (x-axis)
Dim AmountTitles.s(4,5) ;Amounts Text    (y-axis)

Procedure Chart_Bar_Virtical(WindowTitle.s,ChartTitle.s,Divisor,AmountRange,Bars,BarWidth,BarGap,ChartWidth)
  Dim TextBar(11)
  Shared ChartData(),BarTitles(),AmountTitles()
  If OpenWindow(1, 0, 0, ChartWidth+95, 330, WindowTitle,#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    If CreateImage(0,ChartWidth+BarGap+5,260)
      StartDrawing(ImageOutput(0))
        DrawingFont(FontID(0))
        Box(0,0,ChartWidth+BarGap+5,260,RGB(120,120,120))
        Box(0,0,ChartWidth+BarGap,255,RGB(180,160,255))
        x1=0
        For Loop=0 To Bars-1
          a=ChartData(0,Loop)/Divisor
          If ChartData(0,Loop)>0
            Box(x1,255-a,BarWidth,a,ChartData(1,Loop))
            If a<TextHeight(Str(ChartData(0,Loop)))
              Box(x1,255-TextHeight(Str(ChartData(0,Loop))),BarWidth,TextHeight(Str(ChartData(0,Loop))),RGB(180,180,180))
              DrawText(x1+(BarWidth-TextWidth(Str(ChartData(0,Loop))))/2,255-TextHeight(Str(ChartData(0,Loop))),Str(ChartData(0,Loop)),RGB(235,235,235),RGB(180,180,180))
              Box(x1+BarWidth,255-TextHeight(Str(ChartData(0,Loop))),2,TextHeight(Str(ChartData(0,Loop))),RGB(50,50,50))
            Else
              Box(x1,255-a,BarWidth,TextHeight(Str(ChartData(0,Loop))),RGB(180,180,180))
              DrawText(x1+(BarWidth-TextWidth(Str(ChartData(0,Loop))))/2,255-a,Str(ChartData(0,Loop)),RGB(235,235,235),RGB(180,180,180))
              Box(x1+BarWidth,255-a,2,a,RGB(50,50,50))
            EndIf
          EndIf
          x1+BarWidth+BarGap:MeanBar+a:Mean+ChartData(0,Loop):sum+ChartData(0,Loop)
        Next
        MeanBar=MeanBar/Bars
        Line(0,255-MeanBar,ChartWidth+BarGap,0,RGB(130,130,255))
      StopDrawing()
      CreateImage(1,38,18)
      StartDrawing(ImageOutput(1))
        DrawingFont(FontID(0))
        Box(1,1,36,16,RGB(200,160,120))
        DrawText(2,1,Str(Mean/Bars),$FF0000,RGB(200,160,120))
      StopDrawing()
      CreateImage(2,38,18)
      StartDrawing(ImageOutput(2))
        DrawingFont(FontID(0))
        Box(1,1,36,16,RGB(160,200,120))
        DrawText(2,1,Str(sum),$FF0000,RGB(160,200,120))
      StopDrawing()

      If CreateGadgetList(WindowID(1))
        Text_ChartTitle=TextGadget(#PB_Any,0,10,ChartWidth+80,24,ChartTitle,#PB_Text_Center)
        SetGadgetFont(Text_ChartTitle,LoadFont(1,"Courier",14,#PB_Font_Bold))
        x1=42
        For Loop=0 To Bars-1
          Variable=StringGadget(#PB_Any,x1,305,BarWidth,15,BarTitles(Loop),#PB_String_ReadOnly | #PB_String_BorderLess | #PB_Text_Center)
          TextBar(Loop)=Variable:GadgetToolTip(TextBar(Loop),Str(ChartData(0,Loop))):x1+BarWidth+BarGap
        Next
        TextGadget(#PB_Any,10,38, 30,15,AmountTitles(AmountRange,0))
        TextGadget(#PB_Any,10,88, 30,15,AmountTitles(AmountRange,1))
        TextGadget(#PB_Any,10,138,30,15,AmountTitles(AmountRange,2))
        TextGadget(#PB_Any,10,188,30,15,AmountTitles(AmountRange,3))
        TextGadget(#PB_Any,10,238,30,15,AmountTitles(AmountRange,4))
        TextGadget(#PB_Any,10,288,30,15,AmountTitles(AmountRange,5))
        ImageGadget(#PB_Any,40,40,ChartWidth+BarGap+5,256,ImageID(0))
        MeanLine=ImageGadget(#PB_Any,ChartWidth+56,(255-MeanBar+31),30,20,ImageID(1))
        GadgetToolTip(MeanLine,"Mean Value")
        ChartTotal=ImageGadget(#PB_Any,ChartWidth+56,302,30,20,ImageID(2))
        GadgetToolTip(ChartTotal,"Total Value")

        Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
      EndIf
    EndIf
  CloseWindow(1)
  EndIf
EndProcedure


; Randomize Chart Data and set Bar Color

UpperLimit=1499:LowerLimit=0
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,0)=Number:ChartData(1,0)=RGB(200,100,0)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,1)=Number:ChartData(1,1)=RGB(0,200,0)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,2)=Number:ChartData(1,2)=RGB(0,230,200)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,3)=Number:ChartData(1,3)=RGB(200,100,0)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,4)=Number:ChartData(1,4)=RGB(0,200,0)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,5)=Number:ChartData(1,5)=RGB(0,230,200)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,6)=Number:ChartData(1,6)=RGB(200,100,0)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,7)=Number:ChartData(1,7)=RGB(0,200,0)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,8)=Number:ChartData(1,8)=RGB(0,230,200)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,9)=Number:ChartData(1,9)=RGB(200,100,0)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,10)=Number:ChartData(1,10)=RGB(0,200,0)
Number=Random(UpperLimit-LowerLimit)+LowerLimit:ChartData(0,11)=Number:ChartData(1,11)=RGB(0,230,200)


; Set Amount Titles Text

AmountTitles(0,0)="6000 -":AmountTitles(0,1)="4800 -":AmountTitles(0,2)="3600 -":AmountTitles(0,3)="2400 -":AmountTitles(0,4)="1200 -":AmountTitles(0,5)="      0 -"      ;Divisor=24
AmountTitles(1,0)="3000 -":AmountTitles(1,1)="2400 -":AmountTitles(1,2)="1800 -":AmountTitles(1,3)="1200 -":AmountTitles(1,4)="  600 -":AmountTitles(1,5)="      0 -"     ;Divisor=12
AmountTitles(2,0)="1500 -":AmountTitles(2,1)="1200 -":AmountTitles(2,2)="  900 -":AmountTitles(2,3)="  600 -":AmountTitles(2,4)="  300 -":AmountTitles(2,5)="      0 -"   ;Divisor=6
AmountTitles(3,0)="  750 -":AmountTitles(3,1)="  600 -":AmountTitles(3,2)="  450 -":AmountTitles(3,3)="  300 -":AmountTitles(3,4)="  150 -":AmountTitles(3,5)="      0 -" ;Divisor=3
AmountTitles(4,0)="  250 -":AmountTitles(4,1)="  200 -":AmountTitles(4,2)="  150 -":AmountTitles(4,3)="  100 -":AmountTitles(4,4)="   50 -":AmountTitles(4,5)="      0 -" ;Divisor=1


; Set Bar Titles Text

BarTitles(0)="January"
BarTitles(1)="Febuary"
BarTitles(2)="March"
BarTitles(3)="April"
BarTitles(4)="May"
BarTitles(5)="June"
BarTitles(6)="July"
BarTitles(7)="August"
BarTitles(8)="September"
BarTitles(9)="October"
BarTitles(10)="November"
BarTitles(11)="December"


; Adjust BarWidth to largest BarTitles text width (in pixels)

StartDrawing(ImageOutput(200))
For Loop=0 To 11
  If TextWidth(BarTitles(Loop))>BarWidth:BarWidth=TextWidth(BarTitles(Loop)):EndIf
Next
StopDrawing()
BarWidth-15 ;Adjustent For Given Font


; Set up data

WindowTitle.s="Chart Example By Electrochrisso"
ChartTitle.s="2006 Monthly Totals"
Divisor=6                                             ;Matches Bar Height in pixels to Amount values
AmountRange=2                                         ;Choose a range from AmountTitles Array to fit max Bar value
Bars=12                                               ;Number of Bars to be displayed
BarGap=10                                             ;Space Between the Bars in pixels
For Loop=0 To Bars-1:ChartWidth+BarWidth+BarGap:Next ;Calculate Width of Chart in pixels (ChartWidth)


; Call the Procedure

If Bars>0
  Chart_Bar_Virtical(WindowTitle,ChartTitle,Divisor,AmountRange,Bars,BarWidth,BarGap,ChartWidth)
EndIf

End

Posted: Thu May 24, 2007 8:21 am
by Kaeru Gaman
a hint on that:

Code: Select all

Repeat:Number=Random(UpperLimit):Until Number>LowerLimit
you don't need a loop if you write it that way:

Code: Select all

Number=Random(UpperLimit-LowerLimit)+LowerLimit
:wink:

Posted: Thu May 24, 2007 6:16 pm
by Pantcho!!
Very nice i like it

thanks 8)

Posted: Fri May 25, 2007 1:41 am
by electrochrisso
Thanks Kaeru, a much better way to do the job

Posted: Fri May 25, 2007 4:09 pm
by Psychophanta
Nice ! 8)
Each bar could have the color of the fruit or vegetable it belongs to :wink:

Posted: Sat May 26, 2007 2:01 pm
by electrochrisso
I actually did think of that, unfortunately lazyness got the better of me.

Posted: Sat May 26, 2007 5:35 pm
by rsts
More pears picked than apples? More lemons than oranges? Something seems off here :)

Seriously though, very nice. Thanks for sharing with us.

cheers

Posted: Tue Jul 24, 2007 1:53 am
by electrochrisso
Made a couple of improvements I hope.
Changes made to first post.