B4J Question Is there a polar plot widget or compass available for B4J/B4A

rgarnett1955

Active Member
Licensed User
Longtime User
Hi All,

We are developing a weather station, both hardware and software using jRDC as the data server for remote devices. We need to display Wind direction and wind rose in polar form. Does anyone know of a suitable view (widget) for this?

I have googled and searched the forums, but haven't found anything suitable.

I don't have a clue on how to make one myself.

Something like the attached images is what I'm looking for.

Best regards

Rob
 

Attachments

  • 2020-05-29_09-32-49.png
    2020-05-29_09-32-49.png
    123.4 KB · Views: 190
  • 2020-05-29_09-36-04.png
    2020-05-29_09-36-04.png
    297.4 KB · Views: 202

rgarnett1955

Active Member
Licensed User
Longtime User
Thank's Erel.

I will give that a try.

Love that jRDC micro-server. Really robust, efficient and has great security.

I am using it for a power system monitor and the weather station both with SQLite. I also have it working with MS SQL Server which you can use to connect to multiple different types of DB's, Access, SQLite Oracle, mySQL etc etc.

I tried the new B4J Beta, but the "New" command didn't seem to work. Nothing seemed to happen when it's selected.

Best regards

Rob
 
Upvote 0

rgarnett1955

Active Member
Licensed User
Longtime User
Hi Erel,

I tried what you suggested using two image views and it worked great. I then tried to turn it into a class so I could use it over and over, but I couldn't get this to work.

My main program is:

Main Program:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 450
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private compass1 As compassView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
    compass1.Angle = 33
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub compass1_MouseClicked (EventData As MouseEvent)
    
End Sub

My class is:

CompassView:
Sub Class_Globals
    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    Public mBase As B4XView
    Private xui As XUI 'ignore
    Private iVcompScale As B4XView
    Private iVpointer As B4XView
    Public Tag As Object
End Sub

Sub globals
    
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
    mEventName     = EventName
    mCallBack     = Callback
End Sub

'Base type must be Object
Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base
    Tag = mBase.Tag
    mBase.Tag = Me

      ' Dim clr As Int = xui.PaintOrColorToColor(Props.Get("TextColor")) 'Example of getting a color value from Props
    Sleep(0)
    mBase.LoadLayout("compass_cv")
End Sub

public Sub setAngle(angleDeg As Double)
    iVpointer.Rotation = angleDeg
End Sub

public Sub setReciprocalAngle(angleDeg As Double)
    If angleDeg >= 180 Then   
        iVpointer.Rotation = angleDeg - 180
    Else
        iVpointer.Rotation = angleDeg + 180
    End If
End Sub


Private Sub Base_Resize (Width As Double, Height As Double)
 
End Sub

I have two layouts a main which has the compassView view and a second view called compass_cv which has two images one for the scale and one for the pointer.


The problem occurs when the iVpointer.Rotation function is executed. Of course iVpointer hasn't been set so its Null and the program crashes.

Trouble is I can't figure out out how to set the iVpointer variable and keep it set. I tried setting it using the following piece of code in the initialisation function of the class:

Tried to set iVpointer handle:
Public Sub Initialize (Callback As Object, EventName As String)
    mEventName     = EventName
    mCallBack     = Callback

    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(100, 0, 0, 600dip, 600dip)
    p.LoadLayout("compass_cv")                                       
    Log(p.NumberOfViews)       'There were 2 which is correct'
    iVpointer = p.GetView(0).Tag     
    
End sub


Sadly this didn't work either because the class variables aren't persistent and get reset to null before the setAngle function is called.

I searched high and low for a solution or example, but I didn't do any good.

What must I do to get it to work?

Best regards

Rob
 
Upvote 0

rgarnett1955

Active Member
Licensed User
Longtime User
Hi Erel,

I have attached the project.

I deleted the object folder to make the project small enough to send. I hop that's OK.

Best Regards
Rob
 

Attachments

  • WindCompassClass.zip
    78.5 KB · Views: 164
Upvote 0

Thabo Maqelepo

New Member
Hi All,

We are developing a weather station, both hardware and software using jRDC as the data server for remote devices. We need to display Wind direction and wind rose in polar form. Does anyone know of a suitable view (widget) for this?

I have googled and searched the forums, but haven't found anything suitable.

I don't have a clue on how to make one myself.

Something like the attached images is what I'm looking for.

Best regards

Rob
Rotate needle sample will help once you get the hang of it you can create your own compass png
 

Attachments

  • Screenshot_2020-06-08-05-15-30.png
    Screenshot_2020-06-08-05-15-30.png
    422.4 KB · Views: 179
Upvote 0

klaus

Expert
Licensed User
Longtime User
Two problems:
1. Remove Sleep(0) from DesignerCreateView.
2. In the Main layout you define the compassView as compassView1, but in the code you call it compass1 ?!
The name must be the same in the Designer and in the code!

Attached your modified project.
 

Attachments

  • WindCompassClass1.zip
    77.6 KB · Views: 179
Upvote 0
Top