Android Question Draw two lines on graph.

Discussion in 'Android Questions' started by medsport2012, Jul 31, 2019.

Tags:
  1. medsport2012

    medsport2012 New Member Licensed User

    Attached Files:

  2. emexes

    emexes Well-Known Member Licensed User

    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:
    Code:
    '***** 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.
     
  3. medsport2012

    medsport2012 New Member Licensed User

  4. emexes

    emexes Well-Known Member Licensed User

    I suspect you'll soon be in the market for something like this:
    Code:
    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)
     
    Shelby likes this.
  5. klaus

    klaus Expert 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.
     

    Attached Files:

  6. emexes

    emexes Well-Known Member Licensed User

    +1 since Klaus' solutions are usually (ok... always) more comprehensive than mine :)
     
    José J. Aguilar and Shelby like this.
  7. klaus

    klaus Expert Licensed User

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice