Android Question How do I draw graphics?

Itila Tumer

Active Member
Licensed User
Longtime User
Hi,
According to the data from the text every 10 seconds I need to write data to the chart. I've looked at examples from the forum, but it's complicated and I don't understand. How do I do it more simply.
 

emexes

Expert
Licensed User
Klaus beat me to it, but... first step might be to modify the code that's generating the demo data: go to the Modules tab on the right-side of the IDE, use it to navigate yourself to the CreateLineChart1Data Sub, then muck around with the highlighted lines, change the amplitude or period or feed in your own data.

I expect that adding data on the fly would be a process of: get data, LineChart1.AddLineMultiplePoints(), LineChart1.DrawChart, repeat until no more data.

xchartdata.png
 
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
my dynamic graphics boot code


B4X:
Private Sub DrawDynamicLinePoints
    Private Speed, Direction As Double
    
    
    DateTime.TimeFormat = "ss"
        
    Private ElapsedTime As String
    ElapsedTime=DateTime.Time( DateTime.Now)        'time in seconds
                
    Speed = Rnd(0, 60)
    
    Dim TestArray() As Double = Array As Double(100, 150 ,80,250,130,140,80,130 )
    Direction =  TestArray
    'Dim CardArray2() As Double = Array As Double(80, 150 ,80,140,130,50,80,130 )
    'Direction = Listem
    
    
    If DynamicLines1.NbPoints = ReadingsToShow Then
        DynamicLines1.RemovePointData(0)
    End If
        
    DynamicLines1.AddLineMultiplePoints(ElapsedTime, Array As Double(Speed,Direction), True)
    DynamicLines1.DrawChart
End Sub


error I received


B4A line: 64
Direction = TestArray
javac 1.8.0_201
src\b4a\example\main.java:484: error: incompatible types: double[] cannot be converted to double
_direction = (double)(_testarray);
^
 
Upvote 0

emexes

Expert
Licensed User
Righto, decoding the error message is easy... you have:

Private Speed, Direction As Double
Dim TestArray() As Double = Array As Double(100, 150 ,80,250,130,140,80,130 )


and then when you write:

Direction = TestArray

you are trying to cram an array of doubles (TestArray) into just one double (Direction) which is never going to end happily.

In the line:

DynamicLines1.AddLineMultiplePoints(ElapsedTime, Array As Double(Speed, Direction), True)

I expect that ElapsedTime is the X value, and Speed and Direction are the Y values for that X point (which are bunched into an array so that they can be passed as one parameter, since B4A/Java doesn't handle variable-number-of-parameters as well as some other languages).

Don't know what True is, but if that's what Klaus is using, use that.

So you'd do something like:

B4X:
For I = 0 to NumberOfDataPointsToAdd - 1
    'get the next data reading (one x and two corresponding y values)
    'read it from file or sensor, or just make it up eg:

    ElapsedTime = 17 + I * 0.345
    Speed = Sin(I / 12) * 50 + 50
    Direction = Cos(I / 14) * 180

    'add that reading (both y channels) to the graph
    DynamicLines1.AddLineMultiplePoints(ElapsedTime, Array As Double(Speed, Direction), True)

    'update chart on screen
    DynamicLines1.DrawChart

    'wait a bit to make it look realistic
    Sleep(400)    '400 ms = 2.5 readings per second
Next

See how you go with that :)
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I want to send data from a list or array.
You should show us how you get your data.
What kind of date?
How many Y values?
What does the array or list contain?
In your code, what is this supposed to be?
Dim TestArray() As Double = Array As Double(100, 150 ,80,250,130,140,80,130 )
Are these 8 values for one X point?

@emexes
Don't know what True is, but if that's what Klaus is using, use that.
Explained in post#20.
 
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
You should show us how you get your data.
What kind of date?
How many Y values?
What does the array or list contain?
In your code, what is this supposed to be?
Dim TestArray() As Double = Array As Double(100, 150 ,80,250,130,140,80,130 )
Are these 8 values for one X point?

@emexes
Explained in post#20.

I'm drawing dynamic graphics here.

B4X:
Private Sub DrawDynamicLinePoints
    Private Speed, Direction As Double
    
    DateTime.TimeFormat = "HH:mm:ss"
        
    Private ElapsedTime As String
    ElapsedTime=DateTime.Time( DateTime.Now)        'time in seconds
                
    Speed = Rnd(0, 60)
    Direction = Rnd(0, 700)
    
    If DynamicLines1.NbPoints = ReadingsToShow Then
        DynamicLines1.RemovePointData(0)
    End If
        
    DynamicLines1.AddLineMultiplePoints(ElapsedTime, Array As Double(Speed,Direction), True)
    DynamicLines1.DrawChart
End Sub.DrawChart


Direction sends random values for the blue dynamic chart.
I want to send an array with Direction.
These array elements can be n pieces.
 
Upvote 0

emexes

Expert
Licensed User
Direction sends random values for the blue dynamic chart.
I want to send an array with Direction.
These array elements can be n pieces.

Itila, could you post a screenshot? I can't compile this at the moment (am away from home for a few days).

What I think the above code should be producing is two graph lines, one showing speed (0 to 60) and the other showing direction (0 to 700) (what unit is that direction in? 0 to 360 or 0 to 400 or maybe 0 to 628 I could understand, but... 700?)

What it sounds like you're getting is perhaps one line, alternating between speed (0 to 60) and direction (0 to 700) values.

Either that, or something is being lost in translation. A screenshot is worth 1000 Google translations.

:)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You must be much more precise on what exactly you want to do and answer our questions.
I asked this:
In your code, what is this supposed to be?
Dim TestArray() As Double = Array As Double(100, 150 ,80,250,130,140,80,130 )

you haven't even answered it, why?

How many lines do you want to display?

Attached a small test project.
It uses the B4XLibrary xChart.b4xlib.
You can also use the xChart.bas customview module.
 

Attachments

  • ChartTest.zip
    9.7 KB · Views: 167
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
You must be much more precise on what exactly you want to do and answer our questions.
I asked this:
In your code, what is this supposed to be?
Dim TestArray() As Double = Array As Double(100, 150 ,80,250,130,140,80,130 )

you haven't even answered it, why?

How many lines do you want to display?

Attached a small test project.
It uses the B4XLibrary xChart.b4xlib.
You can also use the xChart.bas customview module.

I'm sorry for the late reply.

I reviewed the example.
Value(0) = Rnd(0, 60)
Value(1) = Rnd(0, 360)
Value(2) = Rnd(0, 180)

They generate random graphic lines.
I want to read chart line values from the list myself.
Dim TestArray() As Double = Array As Double(100, 150 ,80,250,130,140,80,130 ) <--- I wanted to do it here
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, but I still don't understand what you want to do.
Dim TestArray() As Double = Array As Double(100, 150 ,80,250,130,140,80,130 ) <--- I wanted to do it here
What exactly do you want to do here ????
How many lines do you want to display ????
What does Array As Double(100, 150 ,80,250,130,140,80,130 ) exactly mean ????
Does it mean points for different lines at the same X coordinate.
Where do the data come from ????
In the first post you spoke about:
According to the data from the text every 10 seconds I need to write data to the chart.
I have never seen any reference to this in your code examples nor in the further questions.
Where do you call DrawDynamicLinePoints from.
A call to DrawDynamicLinePoints adds a new X coordinat value with the correspondant Y coordinates for the different lines in the Array.
This is done in the Timer_Tick event routine in the demo program.
Did you study the example programs to understand how it works.

How many times do I need to ask the same questions?
If you don't want to help me that I am able to help you, sorry, but I cannot help more.
 
Upvote 0
Top