iOS Code Snippet Chart with JS (WebView)

Discussion in 'iOS Code Snippets' started by JanPRO, Jan 4, 2017.

  1. JanPRO

    JanPRO Well-Known Member Licensed User

    Attached Files:

    Last edited: Jun 23, 2017
    Mashiane, Humberto, ilan and 4 others like this.
  2. tufanv

    tufanv Expert Licensed User

    AweSome! Charts are one of the basic needs of a simple app. They look beautifulş . Thanks Jan for another help to b4i community.
     
  3. tufanv

    tufanv Expert Licensed User

    Dear Jan,

    There is a little problem with update data i think. Lets say we updated the data with a higher number of datasets ( lets say we had 5 data for the graph but later we updated it to 10 data for the graph.) this is no problem. The graph adds the new datas and there are a total of 10 points on the graph.. But if we update the data with less number of datasets ( lets say we had 10 points at first but we want to update to 5 points ) first 5 points updates to graph but there are still 10 points on the graph and last 5 data is from the previous dataset(from 6 to 10 ) so it makes problem.
     
  4. JanPRO

    JanPRO Well-Known Member Licensed User

    Hi,

    ist very simple: just clean the data array before you add new data:
    Code:
    JS($"JSBarChart.data.datasets[0].data = [];"$,W)
    But note: When you change the number of data, you also have to take care about the labels, backgroundColor and borderColor.

    Jan
     
    tufanv likes this.
  5. tufanv

    tufanv Expert Licensed User

    This results with this :

    other half of the data still shows but without any line
    [​IMG]
     
    Mashiane likes this.
  6. JanPRO

    JanPRO Well-Known Member Licensed User

    Can you post the relevant code?
     
  7. tufanv

    tufanv Expert Licensed User

    Code:
    JS($"JSBarChart.data.datasets[0].data = [];"$,W)
        
    'JS($"JSBarChart.data.labels[0].data = [];"$,W)
    '    JS("JSBarChart.reset();",w)
    '    JS("JSBarChart.update();",w)


        
    For i = 0 To Data.Size -1
            JS(
    $"JSBarChart.data.datasets[0].data[${i}] = ${Data.Get(i)};"$,W)
            JS(
    $"JSBarChart.data.labels[${i}] = ${Data.Get(i)};"$,W)
        
    Next
       
        JS(
    "JSBarChart.update();",w)
        Data.Clear

    I call this with :

    Code:
    UpdateData(Array(listegrafik.Get(0),listegrafik.Get(1),listegrafik.Get(2),listegrafik.Get(3),listegrafik.Get(4),listegrafik.Get(5),listegrafik.Get(6),listegrafik.Get(7),listegrafik.Get(8),listegrafik.Get(9),listegrafik.Get(10),listegrafik.Get(11),listegrafik.Get(12),listegrafik.Get(13),listegrafik.Get(14)))
    it works good but when switching to a smaller dataset this problem happens

    Code:
    UpdateData(Array(listegrafik.Get(0),listegrafik.Get(1),listegrafik.Get(2),listegrafik.Get(3),listegrafik.Get(4),listegrafik.Get(5),listegrafik.Get(6)))
    i also tried the commented code blocks at the begining with no luck:
     
  8. JanPRO

    JanPRO Well-Known Member Licensed User

    Try to add the following:
    Code:
    JS($"JSBarChart.data.labels = [];"$,W)
    Jan
     
    moster67 likes this.
  9. tufanv

    tufanv Expert Licensed User

    YEs ! works perfect now ! Thanks Jan.
     
  10. ilan

    ilan Expert Licensed User

    great work as always @JanPRO, thanx a lot for this. :)
     
  11. tufanv

    tufanv Expert Licensed User

    Dear Jan. Can we use this code snipper with b4a without using b4a library for this _? Just to show with the webview ?
     
  12. tufanv

    tufanv Expert Licensed User

    I tried it, I changed the nativeobject to javaobject and it works. But for the updatedata, a line is giving an error under sub js

    Code:
    NaObj.RunMethod("stringByEvaluatingJavaScriptFromString:"Array(Script))
    this gives error :

    java.lang.RuntimeException: Method: stringByEvaluatingJavaScriptFromString: not found in: android.webkit.WebView

    probably this is for ios webview , for android we have to change this to something but i couldnt find .
     
  13. JanPRO

    JanPRO Well-Known Member Licensed User

    Hi,

    change the JS sub to the following:

    Code:
    Sub JS (Script As String, WebV As WebView)
        WebV.Loadurl(
    "javascript:" & Script)
    End Sub
    Attached you can find a B4A demo project.

    Jan
     

    Attached Files:

    • B4A.zip
      File size:
      8.3 KB
      Views:
      71
    tufanv likes this.
  14. tufanv

    tufanv Expert Licensed User

    Thanks Jan ! Works perfect now
     
  15. tufanv

    tufanv Expert Licensed User

    Dear Jan ,

    I have a little question with the b4i code.
    JS($"JSBarChart.data.labels[1] = "xxx";"$,WebViewairports)

    instead of xxx how can i use list.get(0) . I am having trouble with $'s
     
  16. JanPRO

    JanPRO Well-Known Member Licensed User

    Hi,

    Code:
    JS($"JSBarChart.data.labels[1] = "${list.get(0)}";"$,WebViewairports)
    ;)

    Jan
     
    tufanv likes this.
  17. tufanv

    tufanv Expert Licensed User

    Thanks =)
     
  18. Descartex

    Descartex Well-Known Member Licensed User

  19. tufanv

    tufanv Expert Licensed User

    Last edited: Jun 20, 2017
  20. JanPRO

    JanPRO Well-Known Member 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