Android Question Draw two lines on graph.

emexes

Well-Known Member
Licensed User
Please help.
Why I can not draw lines by timer in this code.
You can, but a large part of the problem is: each timer tick, you call DrawGraph, which proceeds to reinitialize (ie, clear) the graph and line, and begin anew with only the current point to show.

So far, what I have done is:
B4X:
'***** move LD and G to Globals, where they will retain their values between Sub calls *****

Sub Globals
    ...
    Dim LD As LineData
    Dim G As Graph

'***** split GraphDraw into two separate Subs *****

Sub GraphInit

    Dim LD As LineData
    LD.Initialize
    LD.Target = PanelChart
    Charts.AddLineColor(LD, Colors.Red) 'First line color
    Charts.AddLineColor(LD, Colors.Blue) 'Second line color
  
    Dim G As Graph
    G.Initialize
    G.Title = "2 Lines Chart)"
    G.XAxis = "Degrees"
    G.YAxis = "Values"
    G.YStart = 0
    G.YEnd = 1000
    G.YInterval = 100
    G.AxisColor = Colors.Black

End Sub

Sub GraphAdd
    'In this case we are adding an array of two values. One for each line.
    'Make sure to create an array for each point. You cannot reuse a single array for all points.
  
    Charts.AddLineMultiplePoints(LD, i, Array As Float(Val, Val1), i Mod 90 = 0)
    Charts.DrawLineChart(G, LD, Colors.White)
End Sub

'***** call GraphInit when the Activity is started *****

Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("1")

    GraphInit

    Timer1.Initialize("Timer1",1000)

'***** call GraphAdd each timer tick *****

Sub TImer1_Tick
  
    Val = Rnd(200,1000)
    Val1 = Rnd(200,1000)
  
    LabelValue.Text = "Random Value : " & Val
  
    i = i + 1

    GraphAdd
  
End Sub
so give that a burl. At this end, I have chart lines appearing.
 

emexes

Well-Known Member
Licensed User
I suspect you'll soon be in the market for something like this:
B4X:
Charts.AddLineMultiplePoints(LD, i, Array As Float(Val, Val1), i Mod 90 = 0)
   
'limit chart to 12 points wide        'ADD
Do While LD.Points.Size > 12          'THESE
    LD.Points.RemoveAt(0)             'FOUR
Loop                                  'LINES
   
Charts.DrawLineChart(G, LD, Colors.White)
 
Top