iOS Code Snippet Chart with JS (WebView)

Hi,

attached you can find a little project which shows the basic integration of a BarChart with Chart.js.

Jan

barchart-png.51654

More examples:
 

Attachments

  • BarChartExample.zip
    72.1 KB · Views: 492
  • BarChart.png
    BarChart.png
    98.1 KB · Views: 1,579
Last edited:

tufanv

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

tufanv

Expert
Licensed User
Longtime User
Hi,

attached you can find a little project which shows the basic integration of a BarChart with Chart.js.

Jan

barchart-png.51654
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.
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

ist very simple: just clean the data array before you add new data:
B4X:
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

Expert
Licensed User
Longtime User
Hi,

ist very simple: just clean the data array before you add new data:
B4X:
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
This results with this :

other half of the data still shows but without any line
IMG_9086.png
 

tufanv

Expert
Licensed User
Longtime User
Can you post the relevant code?
B4X:
    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 :

B4X:
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

B4X:
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:
 

tufanv

Expert
Licensed User
Longtime User
Try to add the following:
B4X:
JS($"JSBarChart.data.labels = [];"$,W)

Jan

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

tufanv

Expert
Licensed User
Longtime User
Dear Jan. Can we use this code snipper with b4a without using b4a library for this _? Just to show with the webview ?
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

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

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

change the JS sub to the following:

B4X:
Sub JS (Script As String, WebV As WebView)
    WebV.Loadurl("javascript:" & Script)
End Sub

Attached you can find a B4A demo project.

Jan
 

Attachments

  • B4A.zip
    8.3 KB · Views: 362

tufanv

Expert
Licensed User
Longtime User
Hi,

change the JS sub to the following:

B4X:
Sub JS (Script As String, WebV As WebView)
    WebV.Loadurl("javascript:" & Script)
End Sub

Attached you can find a B4A demo project.

Jan

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
 

JanPRO

Well-Known Member
Licensed User
Longtime User

Attachments

  • Multibar.zip
    72.2 KB · Views: 373
Top