Bargraph Gadget

Share your advanced PureBasic knowledge/code with the community.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Bargraph Gadget

Post by Droopy »

Code updated For 5.20+

Create a Bargraph Gadget with automatic color ( medium level / high level )

Code: Select all

; PureBasic 3.93
; Idea from Localmotion34

Structure Bargraph
  x.l
  y.l
  width.l
  height.l
  medium.f
  hight.f
  full.f
  Image.l
  Gadget.l
EndStructure

ProcedureDLL BargraphH(x,y,width,height,medium.f,hight.f,full.f)
  
  ; Initialise the LinkedList the first call
  Static Init
  If Init=0
    Global NewList BargraphHLList.Bargraph()
  EndIf
  Init=1
  
  ; Fill the Structure
  AddElement(BargraphHLList())
  BargraphHLList()\x=x
  BargraphHLList()\y=y
  BargraphHLList()\width=width
  BargraphHLList()\height=height
  BargraphHLList()\medium=medium
  BargraphHLList()\hight=hight
  BargraphHLList()\full=full
  BargraphHLList()\Image=CreateImage(#PB_Any,width,height)
  
  ; create the gadget & show the image
  BargraphHLList()\Gadget=ImageGadget(#PB_Any,x,y,width,height,ImageID(BargraphHLList()\Image),#PB_Image_Border)
  
  ; Return the gadget id
  ProcedureReturn ListIndex(BargraphHLList())
  
EndProcedure

ProcedureDLL BargraphHSet(id,value.f)
  
  SelectElement(BargraphHLList(),id)
  
  Ptr.f= ( value /  BargraphHLList()\full )*BargraphHLList()\width
  
  ; Rouge
  color=RGB(255,0,0)
  
  ; Jaune
  If value > BargraphHLList()\medium
    color=RGB(255,255,0)
  EndIf
  
  ; Vert
  If value > BargraphHLList()\hight
    color=RGB(0,255,0)
  EndIf
  
  ; dessine
  
  StartDrawing(ImageOutput(BargraphHLList()\Image))
  Box(0,0,Ptr,BargraphHLList()\height,color)
  StopDrawing()
  
  SetGadgetState(BargraphHLList()\Gadget,ImageID(BargraphHLList()\Image))
  
EndProcedure


;/ Test
OpenWindow(0, 0, 0, 250, 250 , "Bargraph", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

a=BargraphH(10,10,228,50,3000,6000,9000)
b=BargraphH(10,80,228,10,1500,3000,4500)
c=BargraphH(10,110,228,50,3000,8000,18000)

For n= -5000 To 9000 Step 20
  BargraphHSet(a,n)
  BargraphHSet(b,n)
  BargraphHSet(c,n)
  Delay(10)
  If WindowEvent()= #PB_Event_CloseWindow  : End : EndIf
Next

Delay(2000)
Tommeh
Enthusiast
Enthusiast
Posts: 149
Joined: Sun Aug 29, 2004 2:25 pm
Location: United Kingdom

Post by Tommeh »

I love it when people make new gadgets, it brings out all kinds of possibiities :-)

Thanks
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

New version : You can choose color, direction, manage event .

Code: Select all

; PureBasic 3.93
; Idea from Localmotion34

; 0 Right / 1 Left / 2 Up / 3 Down
; Manage Event
; You can choose color / Direction

Structure Bargraph
  x.l
  y.l
  width.l
  height.l
  medium.f
  hight.f
  full.f
  Image.l
  Gadget.l
  value.f
  color1.l
  color2.l
  color3.l
  Direction.l
EndStructure

ProcedureDLL Bargraph(x,y,width,height,Direction,medium.f,hight.f,full.f,color1,color2,color3)
  
  ; Initialise the LinkedList the first call
  Static Init
  If Init=0
    NewList BargraphLList.Bargraph()
  EndIf
  Init=1
  
  ; Fill the Structure
  AddElement(BargraphLList())
  BargraphLList()\x=x
  BargraphLList()\y=y
  BargraphLList()\width=width
  BargraphLList()\height=height
  BargraphLList()\medium=medium
  BargraphLList()\hight=hight
  BargraphLList()\full=full
  BargraphLList()\color1=color1
  BargraphLList()\color2=color2
  BargraphLList()\color3=color3
  BargraphLList()\Direction=Direction
  BargraphLList()\Image=CreateImage(#PB_Any,width,height)
  
  ; create the gadget & show the image
  BargraphLList()\Gadget=ImageGadget(#PB_Any,x,y,width,height,UseImage(BargraphLList()\Image),#PB_Image_Border) 
  
  ; Return the gadget id
  ProcedureReturn ListIndex(BargraphLList())
  
EndProcedure

ProcedureDLL BargraphSet(id,value.f)
  
  SelectElement(BargraphLList(),id)
  
  BargraphLList()\value=value
  
  ; Rouge
  color=BargraphLList()\color1
  
  ; Jaune
  If value > BargraphLList()\medium
    color=color=BargraphLList()\color2
  EndIf
  
  ; Vert
  If value > BargraphLList()\hight
    color=color=BargraphLList()\color3
  EndIf
  
  ; dessine
  UseImage(BargraphLList()\Image)
  StartDrawing(ImageOutput())
  
  Select BargraphLList()\Direction
    Case 0 
      Ptr.f= ( value /  BargraphLList()\full )*BargraphLList()\width
      Box(0,0,Ptr,BargraphLList()\height,color)
    Case 1
      Ptr.f= ( value /  BargraphLList()\full )*BargraphLList()\width
      Box(BargraphLList()\width,0,-(Ptr),BargraphLList()\height,color)
    Case 2
      Ptr.f= ( value /  BargraphLList()\full )*BargraphLList()\height
      Box(0,BargraphLList()\height,BargraphLList()\width,-(Ptr),color)
    Case 3
      Ptr.f= ( value /  BargraphLList()\full )*BargraphLList()\height
      Box(0,0,BargraphLList()\width,Ptr,color)
  EndSelect
  StopDrawing()
  
  SetGadgetState(BargraphLList()\Gadget,UseImage(BargraphLList()\Image))
  
EndProcedure

ProcedureDLL BargraphGet(id)
  SelectElement(BargraphLList(),id)
  ProcedureReturn BargraphLList()\value
EndProcedure

Procedure BargraphEvent(id)
  SelectElement(BargraphLList(),id)
  ProcedureReturn BargraphLList()\Gadget
EndProcedure  


;/ Test
OpenWindow(0, 0, 0, 280, 360, #PB_Window_SystemMenu | #PB_Window_ScreenCentered , "Bargraph")
CreateGadgetList(WindowID())
a=Bargraph(10,10,250,25,0,2500,7000,15000,RGB(255,0,0),color=RGB(255,255,0),color=RGB(0,255,0)) 
b=Bargraph(10,50,250,25,1,2000,5000,7500,color=RGB(255,255,0),color=RGB(0,255,0),RGB(255,0,0)) 
c=Bargraph(10,90,110,250,2,1000,2000,8000,16776960,16744448,16711680) 
d=Bargraph(150,90,110,250,3,5000,7500,12000,16744703,16711935,8388863) 


For n= 0 To 10000 Step 20
  BargraphSet(a,n)
  BargraphSet(b,n)
  BargraphSet(c,n)
  BargraphSet(d,n)
  
  Delay(10)
  Event=WindowEvent()
  If Event=#PB_Event_CloseWindow  : End : EndIf
  If Event=#PB_Event_Gadget
    Select EventGadgetID()
      Case BargraphEvent(a) 
        Beep_(400,25) 
      Case BargraphEvent(b) 
        Beep_(800,25) 
      Case BargraphEvent(c) 
        Beep_(1600,25) 
      Case BargraphEvent(d) 
        Beep_(3200,25) 
    EndSelect
  EndIf     
        
  
Next

Delay(2000)
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Hi Group,

I have a question about using bar graphs in realtime apps. I need to display up to 8 different bars in realtime, with each of them being refreshed at a rate of up to 120 fps.
Can anybody point me to resources that explain how to draw/redraw bar graphs really quick? The processor needs to do more than just updating a display, hence my need for some really fast algorithms....

thanks for any input!
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

No one has a screen which updates 120 times a second, so it's basically useless to redraw so often.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

eheheh

Not even 99% of the world screens can do that!
Usually they stick use 50 / 60 / 70 / 80 hertz (refreshes per second)
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

I think most 19" CRT's can go upto and above 120Hz if you just lower the resolution..
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Trond wrote:No one has a screen which updates 120 times a second, so it's basically useless to redraw so often.
You're right - for most setup's. I'm running a modern card that's not used on the highest resolution - at that lower res I can get 120 refreshes and my monitor plays ball as well.... hence my request.

Basically, I want fast realtime bargraph updates that require as little processor power as possible, to free the chip up for other things. It has to draw eight independent bar graphs in parallel...

As to the app: I'm planning a combustion engine monitoring program that needs to display (in addition to other stuff) EGT (exhaust gas temperature) and CHT (cylinder head temperature) for each of the four cylinders... The temperatures will not change very quickly; from data acquisition point of view, 10 samples per second will do quite nicely. I just want the re-drawing to be as smooth as possible...
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Post Reply