Android Question xChart on xCustomListView trouble

peacemaker

Well-Known Member
Licensed User
HI, All

If to use xChart on a Layout that should be loaded into items of xCustomListView - i have the error
B4X:
java.lang.IllegalArgumentException: width and height must be > 0
at
B4X:
Sub CreateListItem(Text As Map, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, Width, Height)
    p.LoadLayout("cell_vote")    'HERE DURING Initialization

'Here in the class
xcvsGraph.Initialize(xBase)
Any help ?
 

peacemaker

Well-Known Member
Licensed User
a small project
Attached.

Also tried the run-time creation of xChart, but it's imposible, as it's not a View object type.

B4X:
Sub Activity_Refresh
    clv.Clear

    For i = 0 To 5
        Dim m As Map
        m.Initialize
        Dim p As B4XView = CreateListItem(m, clv.AsView.Width, 95dip)
        clv.Add(p, i)
        Dim BarChart2 As xChart
        BarChart2.Initialize(Me, "BarChart2")
        p.AddView(BarChart2, 0, 0, p.Width, p.Height)
Error:
B4X:
Compiling generated Java code.    Error
B4A line: 45
p.AddView(BarChart2, 0, 0, p.Width, p.Height)
javac 1.8.0_151
src\xChart\xCustomListView\main.java:442: error: incompatible types: xchart cannot be converted to View
_p.AddView((android.view.View)(mostCurrent._barchart2),(int) (0),(int) (0),_p.getWidth(),_p.getHeight());
                              ^
And tried loading by extra Layout after the parent is created:
B4X:
    For i = 0 To 5
        Dim m As Map
        m.Initialize
        Dim p As Panel = CreateListItem(m, clv.AsView.Width, 95dip)
        clv.Add(p, i)
        p.LoadLayout("cell_xchart")


'Again error:
java.lang.IllegalArgumentException: width and height must be > 0
 

Attachments

Last edited:

klaus

Expert
Licensed User
Here you are.
The main problem was the vertical anchor of BarChart2 set to both and the use of an old version of CustomListView.
I replaced the CustomListView by the xCustomListView library.
There were also some problems in the xChart module.
Attached a modified project.
The height of each panel in the CustomListView is calculated in the CreateListItem routine according to the layout.
 

Attachments

Mahares

Well Known Member
Licensed User
in the project in post #4, the xChart module is updated.
I tried to run the example that @klaus fixed in post #4. It runs well for bar graph. But if I try to run it for line graph, it returns this error:
Error occurred on line: 470 (xChart)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.RectF.round(android.graphics.Rect)' on a null object reference
All i did is:
Replace this line:
B4X:
BarChart2.AddBar("", color)
With:
B4X:
BarChart2.AddLine("", color)
And Replace this line:
B4X:
BarChart2.AddBarPointData($"${j}"$, Rnd(0, 200))
With:
B4X:
BarChart2.AddLinePointData($"${j}"$, Rnd(0, 200), False)
Anything else I need to do to make it work for a line graph?
Thank you
 

klaus

Expert
Licensed User
You should use the last update in the xChart thread, which includes a demo program.
There you will see how to generate a LINE chart.
For a line chart you must:
Set the Chart type to LINE.
Add a line with Chart1.AddLine
Add points either with
Chart1.AddLineMultiplePoints(... for multiple lines
or
Chart1.AddLinePointData(... for a single line
You can also use Chart1.AddLineMultiplePoints(... for a single line with one value in the points array.
 

Mahares

Well Known Member
Licensed User
You should use the last update in the xChart thread
Even after updating the project in post #4 to the 2.60 xChart class, I get the same error. Please see attached project
Error occurred on line: 544 (xChart)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.RectF.round(android.graphics.Rect)' on a null object reference
Please check it out. Thanks
 

Attachments

Mahares

Well Known Member
Licensed User
Attached the modified project.
The modified class and project in post #12 allowed me to get a line graph. But, if I increase the number from 5 to say 75 as in this line:
For i = 0 To 75 instead of For i = 0 To 5 I get out of mem error with 75 or 100.
Maybe, using lazy loading here which I could not figure out may remove that error.
java.lang.OutOfMemoryError: Failed to allocate a 677156 byte allocation with 165332 free bytes and 161KB until OOM

Thank you
 

klaus

Expert
Licensed User
Maybe, using lazy loading here which I could not figure out may remove that error.
Probably yes.
I tested the program from post #12 with 200 xCharts and get no error on my Samsung S8.
It takes some time to get the screen filled.
 

Mahares

Well Known Member
Licensed User
I tested the program from post #12 with 200 xCharts and get no error on my Samsung S8.
I tested it on a Galaxy S6 phone and it crashes with 100 charts. So it depends on the device's memory. Having a poor understanding of lazy loading, I could not integrate it in an xCustomListView with xCharts. Maybe someone with a better grasp of lazy loading can help us below:
B4X:
Sub clv_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 20
    For i = 0 To clv.Size - 1
        Dim p As B4XView = clv.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
'                'missing code
                Dim mp As Map=clv.GetValue(i)
                p.LoadLayout("cell_vote")
                'missing code
            End If
        Else
            If p.NumberOfViews > 0 Then
                p.RemoveAllViews
            End If
        End If
    Next
End Sub
 

Erel

Administrator
Staff member
Licensed User
Example with lazy loading is attached.

The charts are reused. Note that I've made xBase a public variable.

The layout itself, without the chart, is eagerly loaded. It shouldn't be too difficult to change it and only load the layout when needed. The "heavy" view however is the chart.

Tested with 1000 charts, it takes some time to load all the layouts but once loaded the list performs nicely.
 

Attachments

Erel

Administrator
Staff member
Licensed User
The above example was not good enough.

I've changed it to only load the layout when needed. It now creates a list with 2000 charts immediately.
 

Attachments

Top