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)
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)
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
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.
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:
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.
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
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
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
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.