B4A Library androidplot - Real Time Line Chart with data passed from B4A project

Johan Schoeman

Expert
Licensed User
Longtime User
This has been
Dear Johan,
forget multilinechart (it's working fine) and take only a look to sensor chart, here it's a bug or 2 ;-)
This has been sorted out in a PM.
 

rbghongade

Active Member
Licensed User
Longtime User
Dear Johan,
There seems to be no way of clearing the plot. Can you please clarify?
Thanks for the great library.
rajesh
 

Johan Schoeman

Expert
Licensed User
Longtime User
Dear Johan,
There seems to be no way of clearing the plot. Can you please clarify?
Thanks for the great library.
rajesh
Replace the sample code with the below and see what it does (same library files). It will clear all the values once the plots reach the right hand y-axis and then start drawing from scratch...

B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aAndroidPlot_RealTimeLineChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim t As Timer
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim a As Int = 0
    Dim b As Int = 30
    Dim c As Int = 60
    Dim d As Int = 90
    Dim e As Int = 121
    Dim bufsize As Int = 360                                   'set the data buffer size here i.e how many values to display in the plot area
    Dim xlab(bufsize) As String  
    Private lc1 As RealTimeLineChart

    Dim xlabeltrack As Int = 0
   
    Private btnSave As Button
    Private ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
   
    t.Initialize("t", 20)
   
    lc1.GraphTitleColor = Colors.White
    lc1.GraphTitleSkewX = -0.15
    lc1.GraphTitleBold = True
    lc1.GraphTitleTextSize = 20.0
    lc1.GraphPlotAreaBackgroundColor = Colors.Black          'this will paint the plotting area DrakGray regardless of what GraphBackgroundColor has been set to
    lc1.GraphBackgroundColor = Colors.Transparent               'this will paint everything within the outer frame to be white
    lc1.GraphFrameColor = Colors.Red                            'this adjusts only the outer frame color
    lc1.GraphFrameWidth = 2.0
    lc1.GraphBufferSize = bufsize
    lc1.GraphTitle = "REAL TIME LINE CHART - POWERED BY B4A"  

    lc1.DomainLabelColor = Colors.Cyan
    lc1.DomainLabelTextSize = 20
    lc1.DomianLabel = "Degrees"
   
    lc1.YaxisRangeMode = lc1.YaxisMode_FIXED                    'the other option is FIXED
    lc1.YaxisRange(-1.1, 1.1)
    lc1.YaxisDivisions = 10
    lc1.YaxisLabelTicks = 1
    lc1.YaxisShowZero = True
    lc1.YaxisTitleTextSize = 20.0
    lc1.YaxisTitleColor = Colors.Green
    lc1.YaxisGridLineColor = Colors.Yellow
    lc1.YaxisLabelTextSize = 15
    lc1.YaxisLabelColor = Colors.Green
    lc1.YaxisLabelOrientation = 0
    lc1.YaxisTitle = "F(x)"
   
'************************ If you comment this code then the x-axis labels will be the index value of the buffer  
    For i = 0 To bufsize - 1
        xlab(i) = "x-" & i
    Next
    lc1.XAxisLabels = xlab
'*************************************************************************************************************  
       
    lc1.XaxisLabelTextSize = 20
    lc1.XaxisLabelTextColor = Colors.Cyan
    lc1.XaxisGridLineColor = Colors.Yellow
    lc1.XaxisLabelOrientation = 0
    lc1.XaxisDivisions = 30
    lc1.XaxisLabelTicks = 1

   
    lc1.LegendTextSize = 15.0
    lc1.LegendTextColor = Colors.White
    lc1.LegendBackgroundColor = Colors.Transparent
   
    'setup for Line 1  
    lc1.Line_1_LineColor = Colors.Red
    lc1.Line_1_LineWidth = 2.0
    lc1.Line_1_DrawDash = True
    lc1.Line_1_LegendText = "Sin(x)"

    'setup for Line 2  
    lc1.Line_2_LineColor = Colors.Green
    lc1.Line_2_LineWidth = 2.0
    lc1.Line_2_DrawDash = False
    lc1.Line_2_LegendText = "Sin(x + 30)"

    'setup for Line 3
    lc1.Line_3_LineColor = Colors.Yellow
    lc1.Line_3_LineWidth = 2.0
    lc1.Line_3_DrawDash = True
    lc1.Line_3_LegendText = "Sin(x + 60)"
   
    'setup for Line 4
    lc1.Line_4_LineColor = Colors.Cyan
    lc1.Line_4_LineWidth = 2.0
    lc1.Line_4_DrawDash = False
    lc1.Line_4_LegendText = "Sin(x + 90)"
   
    'setup for Line 5
    lc1.Line_5_LineColor = Colors.Magenta
    lc1.Line_5_LineWidth = 2.0
    lc1.Line_5_DrawDash = True
    lc1.Line_5_LegendText = "Sin(x + 120)"  
   
    lc1.NumberOfLineCharts = 5
    lc1.DrawTheGraphs
   
End Sub

Sub Activity_Resume
    t.Enabled = True
    lc1.START
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
    t.Enabled = False
   
End Sub

Sub t_tick
   
   lc1.addData(SinD(a), SinD(b), SinD(c), SinD(d), SinD(e))
   a = a + 1
   b = b + 1
   c = c + 1
   d = d + 1
   e = e + 1
   If a = 361 Then a = 0
   If b = 361 Then b = 0
   If c = 361 Then c = 0
   If d = 361 Then d = 0
   If e = 361 Then e = 0  
 
   If xlabeltrack = bufsize + 1 Then
           t.Enabled = False
           xlabeltrack = 0
        lc1.ClearAllData                        'THIS WILL CLEAR THE DATA
        a = 0
        b = 30
        c = 60
        d = 90
        e = 121
        For i = 0 To bufsize - 1
               xlab(i) = "x-" & i
        Next
        lc1.XAxisLabels = xlab
        lc1.DrawTheGraphs
        t.Enabled = True
        lc1.START
   End If
 
 '************************ If you comment this code then the x-axis labels will be the index of the buffer
   xlabeltrack = xlabeltrack + 1
'   If xlabeltrack > bufsize Then
'         xlab = shiftarray(xlab)
'      xlab(bufsize - 1) = "x-" & xlabeltrack
'      lc1.XAxisLabels = xlab
'   End If
   xlab(bufsize - 1) = "x-" & xlabeltrack
'**************************************************************************************************************
 
End Sub
 
Last edited:

Itila Tumer

Active Member
Licensed User
Longtime User





Can we create graphics in a dynamic way?

For example, we will graph the data from the array every 2 seconds.

Our series will consist of numerical data.
 

Johan Schoeman

Expert
Licensed User
Longtime User

AymanA

Active Member
Licensed User
Hi Johan, thank you for this great library, it is so simple to understand and to implement, I have a question regarding the livechart at post #6, I think this one doesn't provide the Line_1_point text and shape feature, is that correct?

if incorrect then how to pass point data to the live line chart? post 2 use the lc1.Line_1_Data to present the points so not sure how to enable the point in the live graph/chart (post 6 use lc1.addData).

I am using exactly the same as in post 6 but added the below which doesn't show the points:

lc1.Line_1_PointLabelTextSize = 15.0
lc1.Line_1_PointShape = lc1.SHAPE_ROUND
lc1.Line_1_PointColor = Colors.RGB(177,194,216)
lc1.Line_1_PointSize=5
lc1.Line_1_PointLabelTextColor = Colors.Black

Note: Apologies as I am new to the forum so I assume it is ok to ask question with relation to the feature of the library, if that is not ok, let me know and I can create a new thread.
 

BarryW

Active Member
Licensed User
Longtime User
How to change the color and the thickness of line beside yaxis labels and the line above the xaxis labels. Also after changing the font size of yaxis labels it seems that the top value display has been cut.

Please help. Thanks
 

sasetcolombia

Member
Licensed User
Longtime User

Hi;
How to clear all data and load new data in the same LC1(LineChart)?.In RealTimeLineChart is ClearAllData but this function is not available in Linechart.
I have to show the data of different variables (temperature, humidity, soil, precipitation) when the user selects from a listView
Thanks for your help!
 

sasetcolombia

Member
Licensed User
Longtime User
I only needed to display one graph at a time (lc1.Line_1) when clicking on an item in the listview.
lc1 was added from the designer.
If I changed the values and reloaded the chart, it kept showing the old values and overwriting the new values.
The solution was to call "lc2.RemoveView" in the ItemClick event of the listview and call another method to reload the LineChart to the Activity.
Thanks!

Setting lc1.Line_1_Data() or/and lc1.Line_2_Data() with the new values and so on?
B4X:
Private Sub lvGraficas_ItemClick (Position As Int, Value As Object)
    Select Case Position
        Case 0'Humedad Aire
            LegendText="hum_aire"
            GraphTitle="HUMEDAD DEL AIRE"
            YaxisTitle="%"
            Data= dataHumAire' Float array
        Case 1'Temperatura
            LegendText="temp_ambiente"
            GraphTitle="TEMPERATURA AMBIENTE"
            YaxisTitle="°C"
            Data= dataTempAmbiente' Float array
        Case 3'Velocidad Viento
            LegendText="vel_viento"
            GraphTitle="VELOCIDAD DEL VIENTO"
            YaxisTitle="m/s"
            Data= dataVelViento' Float array
        Case 4
        '....
        End select
            lc2.RemoveView ' when starting the Activity the graph of a variable is loaded
    generarGrafica
    End Sub
'Initialize and load the new graph to the Activity
Sub generarGrafica
'lc2 IS NOT ADDED FROM DESIGNER '
    lc2.Initialize("lc2")'initialize LineChart, clear data 
    Activity.AddView(lc2,0,0,100%x,100%y)'add to activity
    lc2.GraphTitle = GraphTitle
    lc2.LegendText=LegendText
    lc2.YaxisTitle=YaxisTitle
    'LineChart configuration code...
    ...
    lc2.Line_1_Data = Data
    ...
End Sub
 

f0raster0

Well-Known Member
Licensed User
Longtime User
I'm attempting to run this example but encountering the error described below:
B4X:
Compiling resources    Error
res\values\style.xml:17: error: root element must be <resources>.
res\values\style.xml: error: file failed to compile.
(i treid Jetify and then clean the project)
thanks in advance
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Delete the Objects/res folder from the B4A project once you have unzipped the B4A project - it should then run. Let me know.
 

f0raster0

Well-Known Member
Licensed User
Longtime User
Delete the Objects/res folder from the B4A project once you have unzipped the B4A project - it should then run. Let me know.
Thank you. It appears that the chart doesn't offer the ability to select a point or zoom in/out, like in your other chart here, is that correct?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…