Android Question xChart on xCustomListView trouble

Discussion in 'Android Questions' started by peacemaker, Nov 2, 2018.

  1. peacemaker

    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
    Code:
    java.lang.IllegalArgumentException: width and height must be > 0
    at
    Code:
    Sub CreateListItem(Text As Map, Width As Int, Height As Int) As Panel
        
    Dim p As Panel
        p.Initialize(
    "")
        p.SetLayout(
    00, Width, Height)
        p.LoadLayout(
    "cell_vote")    'HERE DURING Initialization

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

    klaus Expert Licensed User

    Can you post a small project showing the problem.
     
    peacemaker likes this.
  3. peacemaker

    peacemaker Well-Known Member Licensed User

    Attached.

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

    Code:
    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, 
    00, p.Width, p.Height)
    Error:
    Code:
    Compiling generated Java code.    Error
    B4A line: 
    45
    p.AddView(BarChart2, 
    00, 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:
    Code:
    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
     

    Attached Files:

    Last edited: Nov 3, 2018
  4. klaus

    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.
     

    Attached Files:

    DonManfred, Erel and peacemaker like this.
  5. peacemaker

    peacemaker Well-Known Member Licensed User

    Thanks, Klaus, so xChart class is not changed\updated ?
     
  6. klaus

    klaus Expert Licensed User

    Will come soon, in the project in post #4, the xChart module is updated.
     
    peacemaker likes this.
  7. peacemaker

    peacemaker Well-Known Member Licensed User

    OK, please, increment the version, not incremented.
     
  8. klaus

    klaus Expert Licensed User

    It is already incremented in next, not yet published, 'official' update.
     
    peacemaker likes this.
  9. Mahares

    Mahares Well Known Member Licensed User

    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:
    Code:
    BarChart2.AddBar("", color)
    With:
    Code:
    BarChart2.AddLine("", color)
    And Replace this line:
    Code:
    BarChart2.AddBarPointData($"${j}"$Rnd(0200))
    With:
    Code:
    BarChart2.AddLinePointData($"${j}"$Rnd(0200), False)
    Anything else I need to do to make it work for a line graph?
    Thank you
     
  10. klaus

    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.
     
  11. Mahares

    Mahares Well Known Member Licensed User

    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
     

    Attached Files:

  12. klaus

    klaus Expert Licensed User

    Thank you for reporting this.
    There was a problem in the class.
    Attached the modified project.
    I will update the class tomorrow.
     

    Attached Files:

    Mahares likes this.
  13. Mahares

    Mahares Well Known Member Licensed User

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

    klaus Expert Licensed User

    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.
     
  15. Mahares

    Mahares Well Known Member Licensed User

    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:
    Code:
    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
     
  16. Erel

    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.
     

    Attached Files:

  17. Erel

    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.
     

    Attached Files:

    Mahares likes this.
  18. klaus

    klaus Expert Licensed User

    Wow! Amazing!
    I made just a few changes, because there is no need to set the chart properties in the code, I set them directly in the Designer.
     

    Attached Files:

    Mahares, DonManfred and Erel like this.
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