Android Question Draw two lines on graph.


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:
'***** 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.Target = PanelChart
    Charts.AddLineColor(LD, Colors.Red) 'First line color
    Charts.AddLineColor(LD, Colors.Blue) 'Second line color
    Dim G As Graph
    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)



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

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

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


Licensed User
I suspect you'll soon be in the market for something like this:
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)


Licensed User
Here you are, attached a modified version.
I replaced your Chart.bas module by a more recent one, the one Erel referenced at the bottom of the first post.

Be aware that there exist a much more recent xChart Class, which is a CustomView and also a b4xlibrary.
The demo program includes a dynamic lines example.