Android Tutorial mpAndroidCharts - Various Charts using JavaObject

Have never done a B4A "wrapper" before making use of JavaObject only and thought I would see if I could get the PieChart of mpAndroidCharts implemented by using B4A's JavaObject only. Here it is:

1.png


Copy the attached Jar to your B4A additional library folder
Sample project attached

Try the various animation:

Easing Options:
    'Linear, EaseInQuad, EaseOutQuad, EaseInOutQuad, EaseInCubic, EaseOutCubic, EaseInOutCubic, EaseInQuart, EaseOutQuart, EaseInOutQuart, EaseInSine, EaseOutSine, EaseInOutSine, EaseInExpo, EaseOutExpo, EaseInOutExpo, EaseInCirc, EaseOutCirc, EaseInOutCirc, EaseInElastic, EaseOutElastic, EaseInOutElastic, EaseInBack, EaseOutBack, EaseInOutBack, EaseInBounce, EaseOutBounce, EaseInOutBounce
    Dim EasingOption As JavaObject
    EasingOption.InitializeStatic("com.github.mikephil.charting.animation.Easing.EasingOption")
    pieChart.RunMethod("animateY", Array(2000, EasingOption.GetField("EaseInCubic")))


B4A code using JavaObject:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidPieChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
 
    Dim ctx As JavaObject
    Dim pieChart As JavaObject
    Dim Entry As JavaObject
    Dim pieDataSet As JavaObject
    Dim pieData As JavaObject
    Dim Legend As JavaObject
 
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
 
    ctx.InitializeContext
    pieChart.InitializeNewInstance("com.github.mikephil.charting.charts.PieChart", Array(ctx))
 
    'create the entries for the pie chart
    Dim entryList As List
    entryList.Initialize
    For i = 0 To 4
        entryList.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.Entry", Array(Rnd(0, 100).As(Float), i)))
    Next
 
    Dim legendTitle As String = "Hello B4A"
    pieDataSet.InitializeNewInstance("com.github.mikephil.charting.data.PieDataSet", Array(entryList, legendTitle))
 
    'set the colors for the pie slices
    Dim pieColors() As Int = Array As Int(Colors.Green, Colors.Yellow, Colors.Magenta, Colors.Cyan, Colors.Blue)
    pieDataSet.RunMethod("setColors", Array(pieColors))
    pieDataSet.RunMethod("setSliceSpace", Array(3.0f))

    Legend.InitializeNewInstance("com.github.mikephil.charting.components.Legend", Null)
    Legend = pieChart.RunMethod("getLegend", Null)

    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")

    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    Legend.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))
    Legend.RunMethod("setXEntrySpace", Array(7.0f))
 
    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
 
    Legend.RunMethod("setForm", Array(Legendform.GetField("SQUARE")))
 
    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    Legend.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    Legend.RunMethod("setTextSize", Array(14.0f))
    Legend.RunMethod("setTextColor", Array(Colors.Black))
 
    Dim legendText As List
    legendText.Initialize
    legendText.AddAll(Array As String("A", "B", "C", "D", "E"))
 
    'Linear, EaseInQuad, EaseOutQuad, EaseInOutQuad, EaseInCubic, EaseOutCubic, EaseInOutCubic, EaseInQuart, EaseOutQuart, EaseInOutQuart, EaseInSine, EaseOutSine, EaseInOutSine, EaseInExpo, EaseOutExpo, EaseInOutExpo, EaseInCirc, EaseOutCirc, EaseInOutCirc, EaseInElastic, EaseOutElastic, EaseInOutElastic, EaseInBack, EaseOutBack, EaseInOutBack, EaseInBounce, EaseOutBounce, EaseInOutBounce
    Dim EasingOption As JavaObject
    EasingOption.InitializeStatic("com.github.mikephil.charting.animation.Easing.EasingOption")
    pieChart.RunMethod("animateY", Array(2000, EasingOption.GetField("EaseInCubic")))
 
    pieChart.RunMethod("setHoleColor", Array(Colors.Black))
    pieChart.RunMethod("setHoleColorTransparent", Array(False))
    pieChart.RunMethod("setDrawHoleEnabled", Array(True))
    pieChart.RunMethod("setHoleRadius", Array(50.0f))
    pieChart.RunMethod("setCenterText", Array("By Johan"))
    pieChart.RunMethod("setDrawCenterText", Array(True))
    pieChart.RunMethod("setDescription", Array("Done with JavaObject"))
    pieChart.RunMethod("setCenterTextColor", Array(Colors.White))
    pieChart.RunMethod("setCenterTextSize", Array(15.0f))
    pieChart.RunMethod("setTransparentCircleColor", Array(Colors.Blue))
    pieChart.RunMethod("setTransparentCircleRadius", Array(55.0f))
    pieChart.RunMethod("setTransparentCircleAlpha", Array(100))
    pieChart.RunMethod("setDrawSliceText", Array(True))
    pieChart.RunMethod("setUsePercentValues", Array(True))
    pieChart.RunMethod("setRotationAngle", Array(0.0f))
    pieChart.RunMethod("setRotationEnabled", Array(True))
    pieChart.RunMethod("setDescriptionTextSize", Array(9.0f))
    pieChart.RunMethod("setDescriptionColor", Array(Colors.Magenta))

    pieData.InitializeNewInstance("com.github.mikephil.charting.data.PieData", Array(legendText, pieDataSet))
    pieData.RunMethod("notifyDataChanged", Null)
    pieData.RunMethod("setValueTextSize", Array(12.5f))
    pieData.RunMethod("setValueTextColor", Array(Colors.Black))

    Dim tf As JavaObject
    tf.InitializeStatic("android.graphics.Typeface")
    pieData.RunMethod("setValueTypeface", Array(tf.GetField("SERIF")))

    pieChart.RunMethod("notifyDataSetChanged", Null)
    pieChart.RunMethod("setData", Array(pieData))
 
    Activity.AddView(pieChart, 0, 0, 100%x, 80%y)
   
    Activity.Invalidate

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

The original project is here (wrapped it to a B4A library)
 

Attachments

  • mpChartLib.jar
    360.7 KB · Views: 216
  • mpAndroidPieChartJO.zip
    9.9 KB · Views: 199
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Have added an Event to the PieChart when you click on a Pie Slice:

Event added:
    Dim e As Object = pieChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "pie_clicked", False)
    pieChart.RunMethod("setOnChartValueSelectedListener", Array(e))

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub pie_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0))
        Log(" ")
    Else
        'do nothing
    End If       
    
End Sub

Leaving it up to you to parse the "pie_clicked_Event" data - should be easy to extract (note - it is the pie values and NOT the percentage values)
 

Attachments

  • mpAndroidPieChartJO.zip
    10.1 KB · Views: 103

Johan Schoeman

Expert
Licensed User
Longtime User
Here is the single line chart. Make sure you download the Jar in post #1 and add it to your B4A additional Library Folder
The line chart includes the pop-up when you touch a point on the line chart
It also includes an event that will be raised when clicking on a data point

There are 4 x files in the /Objects/res/drawable folder of the B4A project. They are set to READ ONLY. Make sure they are present and still set to READ ONLY once you have unzipped the project and BEFORE you run the project for the first time.

Take note of the CreateResource(s) that was added to the B4A Manifest (for the markerviews)
CreateResource in B4A Manifest:
CreateResource(layout, custom_marker_view_1.xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:background="@drawable/marker1" >

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text=""
        android:textSize="12dp"
        android:textColor="@android:color/white"
        android:ellipsize="end"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>)


CreateResource(layout, custom_marker_view_2.xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:background="@drawable/marker2" >

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text=""
        android:textSize="12dp"
        android:textColor="@android:color/white"
        android:ellipsize="end"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>)

CreateResource(layout, custom_marker_view_3.xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:background="@drawable/marker3" >

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text=""
        android:textSize="12dp"
        android:textColor="@android:color/white"
        android:ellipsize="end"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>)

CreateResource(layout, custom_marker_view_4.xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:background="@drawable/marker4" >

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text=""
        android:textSize="12dp"
        android:textColor="@android:color/white"
        android:ellipsize="end"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>)

The B4A project makes use of library XmlLayoutBuilder to access the Resource ID(s) of the markerviews as the Markerview Class needs the resource ID to be initialized with.

1.png



B4A sample Code:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidLineChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
   
    Dim ctx As JavaObject
    Dim lineChart As JavaObject
    Dim Entry As JavaObject
    Dim lineDataSet As JavaObject
    Dim lineData As JavaObject
    Dim Legend As JavaObject
    Dim MarkerView As JavaObject
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
   
    Dim x As XmlLayoutBuilder
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
   
   
    ctx.InitializeContext
    lineChart.InitializeNewInstance("com.github.mikephil.charting.charts.LineChart", Array(ctx))
   
    lineChart.RunMethod("setDrawGridBackground", Array(False))
    lineChart.RunMethod("setDescription", Array(""))
    lineChart.RunMethod("setNoDataTextDescription", Array("You need to provide data for the chart."))
   
    lineChart.RunMethod("setHighlightEnabled", Array(True))

    lineChart.RunMethod("setTouchEnabled", Array(True))

    lineChart.RunMethod("setDragEnabled", Array(True))
    lineChart.RunMethod("setScaleEnabled", Array(True))
    'mChart.setScaleXEnabled(True);
    'mChart.setScaleYEnabled(True);

    'If disabled, scaling can be done on x- And y-axis separately
    lineChart.RunMethod("setPinchZoom", Array(True))
   
   
    Legend.InitializeNewInstance("com.github.mikephil.charting.components.Legend", Null)
    Legend = lineChart.RunMethod("getLegend", Null)

    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")

    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    Legend.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))
    Legend.RunMethod("setXEntrySpace", Array(7.0f))
   
    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
   
    Legend.RunMethod("setForm", Array(Legendform.GetField("SQUARE")))
   
    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    Legend.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    Legend.RunMethod("setTextSize", Array(14.0f))
    Legend.RunMethod("setTextColor", Array(Colors.Black))
     
    Dim legendText As String = "A"
   
   
    'prepare the markerview that will pop up when a point of the chart is touched
    'Note the CreateResource in the B4A manifest
    'Also ensure the 4 x markerview files are in /Object/res/drawable folder
    x.LoadXmlLayout(Null, "custom_marker_view_1")
   
    Dim ResourceId As Int
    ResourceId = x.GetResourceId("layout", "custom_marker_view_1")
   
    MarkerView.InitializeNewInstance("com.github.mikephil.charting.components.MyMarkerView", Array(ctx, ResourceId))
    MarkerView.RunMethod("setNumberOfDigits", Array(2))

    lineChart.RunMethod("setMarkerView", Array(MarkerView))
    'End of preparing markerview
   
    Dim limitLine As JavaObject
    limitLine.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(10.0f, "Index 10"))
    limitLine.RunMethod("setLineWidth", Array(4.0f))
    limitLine.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))

    Dim LimitLabelPosition As JavaObject
    LimitLabelPosition.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
    limitLine.RunMethod("setLabelPosition", Array(LimitLabelPosition.GetField("RIGHT_BOTTOM")))
    limitLine.RunMethod("setTextSize", Array(10.0f))
   
    Dim Xaxis As JavaObject
    Xaxis.InitializeNewInstance("com.github.mikephil.charting.components.XAxis", Null)
    Xaxis = lineChart.RunMethod("getXAxis", Null)
    Xaxis.RunMethod("addLimitLine", Array(limitLine))

    Dim ll1 As JavaObject
    ll1.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(130.0f, "Upper Limit"))
    ll1.RunMethod("setLineWidth", Array(4.0f))
    ll1.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
    Dim ll1Position As JavaObject
    ll1Position.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
    ll1.RunMethod("setLabelPosition", Array(ll1Position.GetField("RIGHT_TOP")))
    ll1.RunMethod("setTextSize", Array(10.0f))

    Dim ll2 As JavaObject
    ll2.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(-30.0f, "Lower Limit"))
    ll2.RunMethod("setLineWidth", Array(4.0f))
    ll2.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
    Dim ll1Position As JavaObject
    ll1Position.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
    ll2.RunMethod("setLabelPosition", Array(ll1Position.GetField("RIGHT_BOTTOM")))
    ll2.RunMethod("setTextSize", Array(10.0f))

    Dim yLeftAxis As JavaObject
    yLeftAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yLeftAxis = lineChart.RunMethod("getAxisLeft", Null)
    yLeftAxis.RunMethod("removeAllLimitLines", Null)
    yLeftAxis.RunMethod("addLimitLine", Array(ll1))
    yLeftAxis.RunMethod("addLimitLine", Array(ll2))
    yLeftAxis.RunMethod("setAxisMaxValue", Array(220.0f))
    yLeftAxis.RunMethod("setAxisMinValue", Array(-50.0f))
    yLeftAxis.RunMethod("setStartAtZero", Array(False))
    yLeftAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
   
    yLeftAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
   
    lineChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(False))
   
    Dim xvals As List
    xvals.Initialize
    For i = 0 To 44
        xvals.Add(i & "")
    Next
   
    Dim yvals As List
    yvals.Initialize
   
    For i = 0 To 44
        yvals.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.Entry", Array((360.0/i).As(Float), i)))
       
    Next
           
    lineDataSet.InitializeNewInstance("com.github.mikephil.charting.data.LineDataSet", Array(yvals, legendText))
    lineChart.RunMethod("notifyDataSetChanged", Null)
    lineDataSet.RunMethod("enableDashedLine", Array(10.0f, 5.05f, 0.0f))
    lineDataSet.RunMethod("setColor", Array(Colors.Black))
    lineDataSet.RunMethod("setCircleColor", Array(Colors.Black))
    lineDataSet.RunMethod("setLineWidth", Array(1.0f))
    lineDataSet.RunMethod("setCircleSize", Array(3.0f))
    lineDataSet.RunMethod("setDrawCircleHole", Array(True))
    lineDataSet.RunMethod("setValueTextSize", Array(9.0f))
    lineDataSet.RunMethod("setFillAlpha", Array(65))
    lineDataSet.RunMethod("setFillColor", Array(Colors.Black))

    Dim datasetList As List
    datasetList.Initialize
    datasetList.Add(lineDataSet)

    lineData.InitializeNewInstance("com.github.mikephil.charting.data.LineData", Array(xvals, datasetList))

    lineChart.RunMethod("setData", Array(lineData))

    Activity.AddView(lineChart, 0, 0, 100%x, 80%y)
   
    Activity.Invalidate
   
    Dim e As Object = lineChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "line_clicked", False)
    lineChart.RunMethod("setOnChartValueSelectedListener", Array(e))

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub line_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0))
        Log(" ")
    Else
        'do nothing
    End If  
    Return Null
   
End Sub
 

Attachments

  • mpAndroidLineChartJO.zip
    29 KB · Views: 106
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
A sine chart (single linechart) with some additional functionality from the Jar.


2.png

Sample Code:

Sample Code:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidLineChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    
    Dim ctx As JavaObject
    Dim lineChart As JavaObject
    Dim Entry As JavaObject
    Dim lineDataSet As JavaObject
    Dim lineData As JavaObject
    Dim Legend As JavaObject
    Dim MarkerView As JavaObject
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    
    Dim x As XmlLayoutBuilder
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    ctx.InitializeContext
    lineChart.InitializeNewInstance("com.github.mikephil.charting.charts.LineChart", Array(ctx))
    
    lineChart.RunMethod("setDrawGridBackground", Array(False))
    lineChart.RunMethod("setDescription", Array(""))
    lineChart.RunMethod("setNoDataTextDescription", Array("You need to provide data for the chart."))
    
    lineChart.RunMethod("setHighlightEnabled", Array(True))

    lineChart.RunMethod("setTouchEnabled", Array(True))

    lineChart.RunMethod("setDragEnabled", Array(True))
    lineChart.RunMethod("setScaleEnabled", Array(True))
    'mChart.setScaleXEnabled(True);
    'mChart.setScaleYEnabled(True);

    'If disabled, scaling can be done on x- And y-axis separately
    lineChart.RunMethod("setPinchZoom", Array(True))
    
    Legend.InitializeNewInstance("com.github.mikephil.charting.components.Legend", Null)
    Legend = lineChart.RunMethod("getLegend", Null)

    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")

    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    Legend.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))
    Legend.RunMethod("setXEntrySpace", Array(7.0f))
    
    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
    Legend.RunMethod("setForm", Array(Legendform.GetField("SQUARE")))
    
    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    Legend.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    Legend.RunMethod("setTextSize", Array(14.0f))
    Legend.RunMethod("setTextColor", Array(Colors.Black))
      
    Dim legendText As String = "Sin(x)"

    'prepare the markerview that will pop up when a point of the chart is touched
    'Note the CreateResource in the B4A manifest
    'Also ensure the 4 x markerview files are in /Object/res/drawable folder
    x.LoadXmlLayout(Null, "custom_marker_view_1")
    
    Dim ResourceId As Int
    ResourceId = x.GetResourceId("layout", "custom_marker_view_1")
    
    MarkerView.InitializeNewInstance("com.github.mikephil.charting.components.MyMarkerView", Array(ctx, ResourceId))
    MarkerView.RunMethod("setNumberOfDigits", Array(3))

    lineChart.RunMethod("setMarkerView", Array(MarkerView))
    'End of preparing markerview
    
'    Dim limitLine As JavaObject
'    limitLine.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(10.0f, "Index 10"))
'    limitLine.RunMethod("setLineWidth", Array(4.0f))
'    limitLine.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))

'    Dim LimitLabelPosition As JavaObject
'    LimitLabelPosition.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
'    limitLine.RunMethod("setLabelPosition", Array(LimitLabelPosition.GetField("RIGHT_BOTTOM")))
'    limitLine.RunMethod("setTextSize", Array(10.0f))
    
    Dim Xaxis As JavaObject
    Xaxis.InitializeNewInstance("com.github.mikephil.charting.components.XAxis", Null)
    Xaxis = lineChart.RunMethod("getXAxis", Null)
    Xaxis.RunMethod("setTextSize", Array(12.0f))
    Xaxis.RunMethod("setTextColor", Array(Colors.Red))
    Xaxis.RunMethod("setDrawGridLines", Array(True))
    Xaxis.RunMethod("setDrawAxisLine", Array(True))
    Xaxis.RunMethod("setXaxisTextAngle", Array(45.0f))
    
    Dim xAxisPosition As JavaObject
    'TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    xAxisPosition.InitializeStatic("com.github.mikephil.charting.components.XAxis.XAxisPosition")
    Xaxis.RunMethod("setPosition", Array(xAxisPosition.GetField("BOTTOM")))
'    Xaxis.RunMethod("addLimitLine", Array(limitLine))

'    Dim ll1 As JavaObject
'    ll1.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(130.0f, "Upper Limit"))
'    ll1.RunMethod("setLineWidth", Array(4.0f))
'    ll1.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
'    Dim ll1Position As JavaObject
'    ll1Position.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
'    ll1.RunMethod("setLabelPosition", Array(ll1Position.GetField("RIGHT_TOP")))
'    ll1.RunMethod("setTextSize", Array(10.0f))
'
'    Dim ll2 As JavaObject
'    ll2.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(-30.0f, "Lower Limit"))
'    ll2.RunMethod("setLineWidth", Array(4.0f))
'    ll2.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
'    Dim ll1Position As JavaObject
'    ll1Position.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
'    ll2.RunMethod("setLabelPosition", Array(ll1Position.GetField("RIGHT_BOTTOM")))
'    ll2.RunMethod("setTextSize", Array(10.0f))

    Dim yLeftAxis, yRightAxis As JavaObject
    yLeftAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yRightAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yLeftAxis = lineChart.RunMethod("getAxisLeft", Null)
    yRightAxis = lineChart.RunMethod("getAxisRight", Null)
    yLeftAxis.RunMethod("removeAllLimitLines", Null)
'    yLeftAxis.RunMethod("addLimitLine", Array(ll1))
'    yLeftAxis.RunMethod("addLimitLine", Array(ll2))
    yLeftAxis.RunMethod("setAxisMaxValue", Array(1.0f))
    yLeftAxis.RunMethod("setAxisMinValue", Array(-1.0f))
    yLeftAxis.RunMethod("setStartAtZero", Array(True))
    yLeftAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yLeftAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yLeftAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
    
    yRightAxis.RunMethod("setAxisMaxValue", Array(1.0f))
    yRightAxis.RunMethod("setAxisMinValue", Array(-1.0f))
    yRightAxis.RunMethod("setStartAtZero", Array(True))
    yRightAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yRightAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yRightAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
    
    Dim valueFormatter As JavaObject
    valueFormatter.InitializeNewInstance("com.github.mikephil.charting.utils.MyValueFormatter", Null)
    valueFormatter.RunMethod("setNumberOfDigits", Array(3))
    yLeftAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    yRightAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    
    lineChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(True))
    
    Dim xvals As List
    xvals.Initialize
    For i = 0 To 360
        xvals.Add(i & "")
    Next
    
    Dim yvals As List
    yvals.Initialize
    
    For i = 0 To 360
        yvals.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.Entry", Array((Sin(2 * cPI * i / 360)).As(Float), i)))
        
    Next
            
    lineDataSet.InitializeNewInstance("com.github.mikephil.charting.data.LineDataSet", Array(yvals, legendText))
    lineChart.RunMethod("notifyDataSetChanged", Null)
    lineDataSet.RunMethod("enableDashedLine", Array(10.0f, 5.05f, 0.0f))
    lineDataSet.RunMethod("setColor", Array(Colors.Blue))
    lineDataSet.RunMethod("setCircleColor", Array(Colors.Blue))
    lineDataSet.RunMethod("setLineWidth", Array(2.0f))
    lineDataSet.RunMethod("setCircleSize", Array(2.0f))
    lineDataSet.RunMethod("setDrawCircleHole", Array(True))
    lineDataSet.RunMethod("setValueTextSize", Array(9.0f))
    lineDataSet.RunMethod("setFillAlpha", Array(200))
    lineDataSet.RunMethod("setFillColor", Array(Colors.Black))
    lineDataSet.RunMethod("setDrawCubic", Array(True))
    lineDataSet.RunMethod("setCubicIntensity", Array(1.0f))
    lineDataSet.RunMethod("setHighLightColor", Array(Colors.Black))
    lineDataSet.RunMethod("setHighLightColor",Array(Colors.Red))
    lineDataSet.RunMethod("setHighlightLineWidth", Array(2.0f))

    'Linear, EaseInQuad, EaseOutQuad, EaseInOutQuad, EaseInCubic, EaseOutCubic, EaseInOutCubic, EaseInQuart, EaseOutQuart, EaseInOutQuart, EaseInSine, EaseOutSine, EaseInOutSine, EaseInExpo, EaseOutExpo, EaseInOutExpo, EaseInCirc, EaseOutCirc, EaseInOutCirc, EaseInElastic, EaseOutElastic, EaseInOutElastic, EaseInBack, EaseOutBack, EaseInOutBack, EaseInBounce, EaseOutBounce, EaseInOutBounce
    Dim EasingOption As JavaObject
    EasingOption.InitializeStatic("com.github.mikephil.charting.animation.Easing.EasingOption")
    lineChart.RunMethod("animateY", Array(2000, EasingOption.GetField("EaseInOutBounce")))

    Dim datasetList As List
    datasetList.Initialize
    datasetList.Add(lineDataSet)

    lineData.InitializeNewInstance("com.github.mikephil.charting.data.LineData", Array(xvals, datasetList))

    lineChart.RunMethod("setData", Array(lineData))

    Activity.AddView(lineChart, 0, 5%y, 100%x, 80%y)
    
    Activity.Invalidate
    
    Dim e As Object = lineChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "line_clicked", False)
    lineChart.RunMethod("setOnChartValueSelectedListener", Array(e))

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub line_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0))
        Log(" ")
    Else
        'do nothing
    End If   
    Return Null
    
End Sub
 

Attachments

  • mpAndroidLineChartJOSine.zip
    29.4 KB · Views: 105
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
I guess a good enough start for the single BarChart with JavaObject.

Read post #3 above for the MarkerViews.

1.png
2.png


Sample Code:

Sample Code:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidBarChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
   
    Dim ctx As JavaObject
    Dim barChart As JavaObject
    Dim Entry As JavaObject
    Dim barDataSet As JavaObject
    Dim barData As JavaObject
    Dim Legend As JavaObject
    Dim MarkerView As JavaObject
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
   
    Dim x As XmlLayoutBuilder
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
   
    ctx.InitializeContext
    barChart.InitializeNewInstance("com.github.mikephil.charting.charts.BarChart", Array(ctx))
   
    barChart.RunMethod("setDrawGridBackground", Array(False))
    barChart.RunMethod("setDescription", Array(""))
    barChart.RunMethod("setNoDataTextDescription", Array("You need to provide data for the chart."))
   
    barChart.RunMethod("setHighlightEnabled", Array(True))

    barChart.RunMethod("setTouchEnabled", Array(True))

    barChart.RunMethod("setDragEnabled", Array(True))
    barChart.RunMethod("setScaleEnabled", Array(True))
    'mChart.setScaleXEnabled(True);
    'mChart.setScaleYEnabled(True);

    'If disabled, scaling can be done on x- And y-axis separately
    barChart.RunMethod("setPinchZoom", Array(True))
   
    Legend.InitializeNewInstance("com.github.mikephil.charting.components.Legend", Null)
    Legend = barChart.RunMethod("getLegend", Null)

    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")

    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    Legend.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))
    Legend.RunMethod("setXEntrySpace", Array(7.0f))
   
    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
    Legend.RunMethod("setForm", Array(Legendform.GetField("SQUARE")))
   
    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    Legend.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    Legend.RunMethod("setTextSize", Array(14.0f))
    Legend.RunMethod("setTextColor", Array(Colors.Black))
     
    Dim legendText As String = "Sin(x)"

    'prepare the markerview that will pop up when a point of the chart is touched
    'Note the CreateResource in the B4A manifest
    'Also ensure the 4 x markerview files are in /Object/res/drawable folder
    x.LoadXmlLayout(Null, "custom_marker_view_1")
   
    Dim ResourceId As Int
    ResourceId = x.GetResourceId("layout", "custom_marker_view_1")
   
    MarkerView.InitializeNewInstance("com.github.mikephil.charting.components.MyMarkerView", Array(ctx, ResourceId))
    MarkerView.RunMethod("setNumberOfDigits", Array(3))

    barChart.RunMethod("setMarkerView", Array(MarkerView))
    'End of preparing markerview
   
''    Dim limitLine As JavaObject
''    limitLine.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(10.0f, "Index 10"))
''    limitLine.RunMethod("setLineWidth", Array(4.0f))
''    limitLine.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
'
''    Dim LimitLabelPosition As JavaObject
''    LimitLabelPosition.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
''    limitLine.RunMethod("setLabelPosition", Array(LimitLabelPosition.GetField("RIGHT_BOTTOM")))
''    limitLine.RunMethod("setTextSize", Array(10.0f))
'  
    Dim Xaxis As JavaObject
    Xaxis.InitializeNewInstance("com.github.mikephil.charting.components.XAxis", Null)
    Xaxis = barChart.RunMethod("getXAxis", Null)
    Xaxis.RunMethod("setTextSize", Array(12.0f))
    Xaxis.RunMethod("setTextColor", Array(Colors.Red))
    Xaxis.RunMethod("setDrawGridLines", Array(True))
    Xaxis.RunMethod("setDrawAxisLine", Array(True))
    Xaxis.RunMethod("setXaxisTextAngle", Array(45.0f))
   
    Dim xAxisPosition As JavaObject
    'TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    xAxisPosition.InitializeStatic("com.github.mikephil.charting.components.XAxis.XAxisPosition")
    Xaxis.RunMethod("setPosition", Array(xAxisPosition.GetField("BOTTOM")))
'    Xaxis.RunMethod("addLimitLine", Array(limitLine))

''    Dim ll1 As JavaObject
''    ll1.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(130.0f, "Upper Limit"))
''    ll1.RunMethod("setLineWidth", Array(4.0f))
''    ll1.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
''    Dim ll1Position As JavaObject
''    ll1Position.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
''    ll1.RunMethod("setLabelPosition", Array(ll1Position.GetField("RIGHT_TOP")))
''    ll1.RunMethod("setTextSize", Array(10.0f))
''
''    Dim ll2 As JavaObject
''    ll2.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(-30.0f, "Lower Limit"))
''    ll2.RunMethod("setLineWidth", Array(4.0f))
''    ll2.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
''    Dim ll1Position As JavaObject
''    ll1Position.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
''    ll2.RunMethod("setLabelPosition", Array(ll1Position.GetField("RIGHT_BOTTOM")))
''    ll2.RunMethod("setTextSize", Array(10.0f))
'
    Dim yLeftAxis, yRightAxis As JavaObject
    yLeftAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yRightAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yLeftAxis = barChart.RunMethod("getAxisLeft", Null)
    yRightAxis = barChart.RunMethod("getAxisRight", Null)
    yLeftAxis.RunMethod("removeAllLimitLines", Null)
'    yLeftAxis.RunMethod("addLimitLine", Array(ll1))
'    yLeftAxis.RunMethod("addLimitLine", Array(ll2))
    yLeftAxis.RunMethod("setAxisMaxValue", Array(1.2f))
    yLeftAxis.RunMethod("setAxisMinValue", Array(-1.2f))
    yLeftAxis.RunMethod("setStartAtZero", Array(True))
    yLeftAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yLeftAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yLeftAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
   
    yRightAxis.RunMethod("setAxisMaxValue", Array(1.2f))
    yRightAxis.RunMethod("setAxisMinValue", Array(-1.2f))
    yRightAxis.RunMethod("setStartAtZero", Array(True))
    yRightAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yRightAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yRightAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
   
    Dim valueFormatter As JavaObject
    valueFormatter.InitializeNewInstance("com.github.mikephil.charting.utils.MyValueFormatter", Null)
    valueFormatter.RunMethod("setNumberOfDigits", Array(3))
    yLeftAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    yRightAxis.RunMethod("setValueFormatter", Array(valueFormatter))
   
    barChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(True))
   
    Dim xvals As List
    xvals.Initialize
    For i = 0 To 360
        xvals.Add(i & "")
    Next
   
    Dim yvals As List
    yvals.Initialize
   
    For i = 0 To 360
        yvals.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BarEntry", Array((Sin(2 * cPI * i / 360)).As(Float), i)))
       
    Next
           
    barDataSet.InitializeNewInstance("com.github.mikephil.charting.data.BarDataSet", Array(yvals, legendText))
    barChart.RunMethod("notifyDataSetChanged", Null)
    barDataSet.RunMethod("setColor", Array(Colors.Blue))
    barDataSet.RunMethod("setValueTextSize", Array(9.0f))
    barDataSet.RunMethod("setHighLightColor",Array(Colors.Red))

    'Linear, EaseInQuad, EaseOutQuad, EaseInOutQuad, EaseInCubic, EaseOutCubic, EaseInOutCubic, EaseInQuart, EaseOutQuart, EaseInOutQuart, EaseInSine, EaseOutSine, EaseInOutSine, EaseInExpo, EaseOutExpo, EaseInOutExpo, EaseInCirc, EaseOutCirc, EaseInOutCirc, EaseInElastic, EaseOutElastic, EaseInOutElastic, EaseInBack, EaseOutBack, EaseInOutBack, EaseInBounce, EaseOutBounce, EaseInOutBounce
    Dim EasingOption As JavaObject
    EasingOption.InitializeStatic("com.github.mikephil.charting.animation.Easing.EasingOption")
    barChart.RunMethod("animateY", Array(2000, EasingOption.GetField("EaseInOutBounce")))

    Dim datasetList As List
    datasetList.Initialize
    datasetList.Add(barDataSet)

    barData.InitializeNewInstance("com.github.mikephil.charting.data.BarData", Array(xvals, datasetList))

    barChart.RunMethod("setData", Array(barData))

    Activity.AddView(barChart, 0, 5%y, 100%x, 80%y)
   
    Activity.Invalidate
   
    Dim e As Object = barChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "bar_clicked", False)
    barChart.RunMethod("setOnChartValueSelectedListener", Array(e))

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub bar_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0))
        Log(" ")
    Else
        'do nothing
    End If  
    Return Null
   
End Sub
 

Attachments

  • mpAndroidBarChartJO.zip
    29.3 KB · Views: 93

Johan Schoeman

Expert
Licensed User
Longtime User
This is a good starter for MultiLineChart making use of JavaObject. You can add as many Lines as you want - check the code where it is done. This example has 7



1.png


Sample Code:

Sample Code using JavaObject:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidMultiLineChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    
    Dim ctx As JavaObject
    Dim lineChart As JavaObject
    Dim Entry As JavaObject
    Dim lineDataSet As JavaObject
    Dim lineData As JavaObject
    Dim Legend As JavaObject
    Dim MarkerView As JavaObject
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    
    Dim x As XmlLayoutBuilder
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    ctx.InitializeContext
    lineChart.InitializeNewInstance("com.github.mikephil.charting.charts.LineChart", Array(ctx))
    
    Dim e As Object = lineChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "line_clicked", False)
    lineChart.RunMethod("setOnChartValueSelectedListener", Array(e))
    
    lineChart.RunMethod("setDrawGridBackground", Array(True))
    lineChart.RunMethod("setDescription", Array(""))
    lineChart.RunMethod("setHighlightEnabled", Array(True))
    lineChart.RunMethod("setTouchEnabled", Array(True))
    lineChart.RunMethod("setDragEnabled", Array(True))
    lineChart.RunMethod("setScaleEnabled", Array(True))
    lineChart.RunMethod("setPinchZoom", Array(True))
    
    Legend.InitializeNewInstance("com.github.mikephil.charting.components.Legend", Null)
    Legend = lineChart.RunMethod("getLegend", Null)
    
    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")
    
    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    Legend.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))
    Legend.RunMethod("setXEntrySpace", Array(7.0f))
    
    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
    Legend.RunMethod("setForm", Array(Legendform.GetField("SQUARE")))
    
    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    Legend.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    Legend.RunMethod("setTextSize", Array(14.0f))
    Legend.RunMethod("setTextColor", Array(Colors.Black))
    
    Dim xvals As List
    xvals.Initialize
    For i = 0 To 360
        xvals.Add(i & "")
    Next
    
    Dim dataSets As List
    dataSets.Initialize
    
    Dim numOfLineCharts As Int = 7
    For z = 0 To numOfLineCharts - 1
        Dim values As List
        values.Initialize

        Dim extra As Int = 0
        extra = z * 360 / numOfLineCharts
        
        For i = 0 To 360
            values.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.Entry", Array((Sin(2 * cPI * (i + extra) / 360)).As(Float), i)))
        Next
        lineDataSet.InitializeNewInstance("com.github.mikephil.charting.data.LineDataSet", Array(values, "D " & (z+1)))
        
        lineDataSet.RunMethod("setLineWidth", Array(2.5f))
        lineDataSet.RunMethod("setCircleSize", Array(1.0f))
'        lineDataSet.RunMethod("setDrawCircles", Array(False))
        Dim color As Int = Colors.ARGB(Rnd(255, 256), Rnd(100,256),Rnd(100,256), Rnd(100, 256))
        lineDataSet.RunMethod("setColor", Array(color))
        lineDataSet.RunMethod("setCircleColor", Array(color))
        lineChart.RunMethod("notifyDataSetChanged", Null)
        
        dataSets.add(lineDataSet)
            
    Next
    
    dataSets.get(0).As(JavaObject).RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
    'USE THE FOLLOWING AS IN THE ABOVE LINE OF CODE TO SET DIFFERENT ATTRIBUTES FOR EACH LINEDATASET
'    lineDataSet.RunMethod("setColor", Array(Colors.Blue))
'    lineDataSet.RunMethod("setCircleColor", Array(Colors.Blue))
'    lineDataSet.RunMethod("setLineWidth", Array(2.0f))
'    lineDataSet.RunMethod("setCircleSize", Array(2.0f))
'    lineDataSet.RunMethod("setDrawCircleHole", Array(True))
'    lineDataSet.RunMethod("setValueTextSize", Array(9.0f))
'    lineDataSet.RunMethod("setFillAlpha", Array(200))
'    lineDataSet.RunMethod("setFillColor", Array(Colors.Black))
'    lineDataSet.RunMethod("setDrawCubic", Array(True))
'    lineDataSet.RunMethod("setCubicIntensity", Array(1.0f))
    dataSets.get(1).As(JavaObject).RunMethod("setHighLightColor",Array(Colors.Red))
    dataSets.get(1).As(JavaObject).RunMethod("setHighlightLineWidth", Array(3.0f))
    
    lineData.InitializeNewInstance("com.github.mikephil.charting.data.LineData", Array(xvals, dataSets))

    lineChart.RunMethod("setData", Array(lineData))
    
    x.LoadXmlLayout(Null, "custom_marker_view_1")
    
    Dim ResourceId As Int
    ResourceId = x.GetResourceId("layout", "custom_marker_view_1")
    
    MarkerView.InitializeNewInstance("com.github.mikephil.charting.components.MyMarkerView", Array(ctx, ResourceId))
    MarkerView.RunMethod("setNumberOfDigits", Array(3))
    
    lineChart.RunMethod("setMarkerView", Array(MarkerView))
    lineChart.RunMethod("animateXY", Array(3000, 3000))
    
    Dim Xaxis As JavaObject
    Xaxis.InitializeNewInstance("com.github.mikephil.charting.components.XAxis", Null)
    Xaxis = lineChart.RunMethod("getXAxis", Null)
    Xaxis.RunMethod("setTextSize", Array(12.0f))
    Xaxis.RunMethod("setTextColor", Array(Colors.Red))
    Xaxis.RunMethod("setDrawGridLines", Array(True))
    Xaxis.RunMethod("setDrawAxisLine", Array(True))
    Xaxis.RunMethod("setXaxisTextAngle", Array(45.0f))

    Dim xAxisPosition As JavaObject
'    'TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    xAxisPosition.InitializeStatic("com.github.mikephil.charting.components.XAxis.XAxisPosition")
    Xaxis.RunMethod("setPosition", Array(xAxisPosition.GetField("BOTTOM")))
    
    Dim yLeftAxis, yRightAxis As JavaObject
    yLeftAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yRightAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yLeftAxis = lineChart.RunMethod("getAxisLeft", Null)
    yRightAxis = lineChart.RunMethod("getAxisRight", Null)
    yLeftAxis.RunMethod("removeAllLimitLines", Null)
    '    yLeftAxis.RunMethod("addLimitLine", Array(ll1))
    '    yLeftAxis.RunMethod("addLimitLine", Array(ll2))
    yLeftAxis.RunMethod("setAxisMaxValue", Array(1.2f))
    yLeftAxis.RunMethod("setAxisMinValue", Array(-1.2f))
    yLeftAxis.RunMethod("setStartAtZero", Array(True))
    yLeftAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yLeftAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yLeftAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
    
    yRightAxis.RunMethod("setAxisMaxValue", Array(1.2f))
    yRightAxis.RunMethod("setAxisMinValue", Array(-1.2f))
    yRightAxis.RunMethod("setStartAtZero", Array(True))
    yRightAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yRightAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yRightAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
    
    Dim valueFormatter As JavaObject
    valueFormatter.InitializeNewInstance("com.github.mikephil.charting.utils.MyValueFormatter", Null)
    valueFormatter.RunMethod("setNumberOfDigits", Array(3))
    yLeftAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    yRightAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    
    lineChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(True))
        
    'Linear, EaseInQuad, EaseOutQuad, EaseInOutQuad, EaseInCubic, EaseOutCubic, EaseInOutCubic, EaseInQuart, EaseOutQuart, EaseInOutQuart, EaseInSine, EaseOutSine, EaseInOutSine, EaseInExpo, EaseOutExpo, EaseInOutExpo, EaseInCirc, EaseOutCirc, EaseInOutCirc, EaseInElastic, EaseOutElastic, EaseInOutElastic, EaseInBack, EaseOutBack, EaseInOutBack, EaseInBounce, EaseOutBounce, EaseInOutBounce
'    Dim EasingOption As JavaObject
'    EasingOption.InitializeStatic("com.github.mikephil.charting.animation.Easing.EasingOption")
'    lineChart.RunMethod("animateY", Array(2000, EasingOption.GetField("EaseInOutBounce")))
    
    Activity.AddView(lineChart, 0, 5%y, 100%x, 80%y)

    Activity.Invalidate
    
''    Dim limitLine As JavaObject
''    limitLine.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(10.0f, "Index 10"))
''    limitLine.RunMethod("setLineWidth", Array(4.0f))
''    limitLine.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
'
''    Dim LimitLabelPosition As JavaObject
''    LimitLabelPosition.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
''    limitLine.RunMethod("setLabelPosition", Array(LimitLabelPosition.GetField("RIGHT_BOTTOM")))
''    limitLine.RunMethod("setTextSize", Array(10.0f))

''    Dim ll1 As JavaObject
''    ll1.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(130.0f, "Upper Limit"))
''    ll1.RunMethod("setLineWidth", Array(4.0f))
''    ll1.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
''    Dim ll1Position As JavaObject
''    ll1Position.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
''    ll1.RunMethod("setLabelPosition", Array(ll1Position.GetField("RIGHT_TOP")))
''    ll1.RunMethod("setTextSize", Array(10.0f))
''
''    Dim ll2 As JavaObject
''    ll2.InitializeNewInstance("com.github.mikephil.charting.components.LimitLine", Array(-30.0f, "Lower Limit"))
''    ll2.RunMethod("setLineWidth", Array(4.0f))
''    ll2.RunMethod("enableDashedLine", Array(10.0f, 10.0f, 0.0f))
''    Dim ll1Position As JavaObject
''    ll1Position.InitializeStatic("com.github.mikephil.charting.components.LimitLine.LimitLabelPosition")
''    ll2.RunMethod("setLabelPosition", Array(ll1Position.GetField("RIGHT_BOTTOM")))
''    ll2.RunMethod("setTextSize", Array(10.0f))

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub line_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0))  'Logs the xIndex and the yValue - index starts at 0
        Log(Args(2))  'Logs the xIndex and the DataSetIndex - inxdex starts at 0
        Log(" ")
    Else
        'do nothing
    End If   
    Return Null
    
End Sub
 

Attachments

  • mpAndroidMultiLineChartJO.zip
    29.6 KB · Views: 105

Johan Schoeman

Expert
Licensed User
Longtime User
I guess a good enough start to get you going with MultiBarChart (vertical)

1.png


Sample Code:
Sample Code us:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidMultiBarChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    
    Dim ctx As JavaObject
    Dim barChart As JavaObject
    Dim Entry As JavaObject
    Dim Legend As JavaObject
    Dim MarkerView As JavaObject
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    
    Dim x As XmlLayoutBuilder
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    ctx.InitializeContext
    barChart.InitializeNewInstance("com.github.mikephil.charting.charts.BarChart", Array(ctx))
    
    barChart.RunMethod("setDrawGridBackground", Array(False))
    barChart.RunMethod("setDescription", Array(""))
    barChart.RunMethod("setNoDataTextDescription", Array("You need to provide data for the chart."))
    barChart.RunMethod("setPinchZoom", Array(True))
    barChart.RunMethod("setDrawBarShadow", Array(False))
    barChart.RunMethod("setDrawGridBackground", Array(False))

    'prepare the markerview that will pop up when a point of the chart is touched
    'Note the CreateResource in the B4A manifest
    'Also ensure the 4 x markerview files are in /Object/res/drawable folder
    x.LoadXmlLayout(Null, "custom_marker_view_1")
    
    Dim ResourceId As Int
    ResourceId = x.GetResourceId("layout", "custom_marker_view_1")
    
    MarkerView.InitializeNewInstance("com.github.mikephil.charting.components.MyMarkerView", Array(ctx, ResourceId))
    MarkerView.RunMethod("setNumberOfDigits", Array(3))
    
    barChart.RunMethod("setMarkerView", Array(MarkerView))
    'End of preparing markerview

    Legend.InitializeNewInstance("com.github.mikephil.charting.components.Legend", Null)
    Legend = barChart.RunMethod("getLegend", Null)
    
    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")
    
    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    Legend.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))
    Legend.RunMethod("setXEntrySpace", Array(7.0f))
    
    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
    Legend.RunMethod("setForm", Array(Legendform.GetField("SQUARE")))
    
    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    Legend.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    Legend.RunMethod("setTextSize", Array(14.0f))
    Legend.RunMethod("setTextColor", Array(Colors.Black))
    
    Dim Xaxis As JavaObject
    Xaxis.InitializeNewInstance("com.github.mikephil.charting.components.XAxis", Null)
    Xaxis = barChart.RunMethod("getXAxis", Null)
    Xaxis.RunMethod("setTextSize", Array(12.0f))
    Xaxis.RunMethod("setTextColor", Array(Colors.Red))
    Xaxis.RunMethod("setDrawGridLines", Array(True))
    Xaxis.RunMethod("setDrawAxisLine", Array(True))
    Xaxis.RunMethod("setXaxisTextAngle", Array(45.0f))
    
    Dim xAxisPosition As JavaObject
    'TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    xAxisPosition.InitializeStatic("com.github.mikephil.charting.components.XAxis.XAxisPosition")
    Xaxis.RunMethod("setPosition", Array(xAxisPosition.GetField("TOP")))
    
    Dim yLeftAxis, yRightAxis As JavaObject
    yLeftAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yRightAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yLeftAxis = barChart.RunMethod("getAxisLeft", Null)
    yRightAxis = barChart.RunMethod("getAxisRight", Null)
    yLeftAxis.RunMethod("removeAllLimitLines", Null)
    '    yLeftAxis.RunMethod("addLimitLine", Array(ll1))
    '    yLeftAxis.RunMethod("addLimitLine", Array(ll2))
    yLeftAxis.RunMethod("setAxisMaxValue", Array(255.0f))
    yLeftAxis.RunMethod("setAxisMinValue", Array(0.0f))
    yLeftAxis.RunMethod("setStartAtZero", Array(True))
    yLeftAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yLeftAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yLeftAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
    
    barChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(False))
    
    Dim xVals As List
    xVals.Initialize
    
    For i = 0 To 11
        xVals.Add(" " & (2020 + i))
    Next
    
    Dim yVals1, yVals2, yVals3 As List
    yVals1.Initialize
    yVals2.Initialize
    yVals3.Initialize

    For i = 0 To 11
        yVals1.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BarEntry", Array((Rnd(0,255)).As(Float), i)))
        yVals2.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BarEntry", Array((Rnd(0,255)).As(Float), i)))
        yVals3.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BarEntry", Array((Rnd(0,255)).As(Float), i)))
    Next
    
    Dim set1 As JavaObject
    set1.InitializeNewInstance("com.github.mikephil.charting.data.BarDataSet", Array(yVals1, "Company A"))
    set1.RunMethod("setColor", Array(Colors.Red))
    Dim set2 As JavaObject
    set2.InitializeNewInstance("com.github.mikephil.charting.data.BarDataSet", Array(yVals2, "Company B"))
    set2.RunMethod("setColor", Array(Colors.Green))
    Dim set3 As JavaObject
    set3.InitializeNewInstance("com.github.mikephil.charting.data.BarDataSet", Array(yVals3, "Company C"))
    set3.RunMethod("setColor", Array(Colors.Blue))
    
    Dim dataSets As List
    dataSets.Initialize
    dataSets.Add(set1)
    dataSets.Add(set2)
    dataSets.Add(set3)

    Dim data As JavaObject
    data.InitializeNewInstance("com.github.mikephil.charting.data.BarData", Array(xVals, dataSets))
    
    data.RunMethod("setGroupSpace", Array(100.0f))

    
    barChart.RunMethod("setData", Array(data))

    barChart.RunMethod("setHighlightEnabled", Array(True))

    barChart.RunMethod("setTouchEnabled", Array(True))

    barChart.RunMethod("setDragEnabled", Array(True))
    barChart.RunMethod("setScaleEnabled", Array(True))
    
    Dim valueFormatter As JavaObject
    valueFormatter.InitializeNewInstance("com.github.mikephil.charting.utils.MyValueFormatter", Null)
    valueFormatter.RunMethod("setNumberOfDigits", Array(3))
    yLeftAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    yRightAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    
    barChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(True))

    Activity.AddView(barChart, 0, 5%y, 100%x, 80%y)
    
    Activity.Invalidate
    
    Dim e As Object = barChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "bar_clicked", False)
    barChart.RunMethod("setOnChartValueSelectedListener", Array(e))

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub bar_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0)) 'logs xIndex and yValue
        Log(Args(2)) 'logs xIndex and DataSetIndex
        Log(" ")
    Else
        'do nothing
    End If   
    Return Null
    
End Sub
 

Attachments

  • mpAndroidMultiBarChartJO.zip
    29.1 KB · Views: 95

Johan Schoeman

Expert
Licensed User
Longtime User
Should be a good start to get you going with a multiple Horizontal Bar Chart

1.png


Sample Code:

Sample Code using JavaObject:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidHorizontalBarChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    
    Dim ctx As JavaObject
    Dim barChart As JavaObject
    Dim Entry As JavaObject
    Dim Legend As JavaObject
    Dim MarkerView As JavaObject
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    
    Dim x As XmlLayoutBuilder
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    ctx.InitializeContext
    barChart.InitializeNewInstance("com.github.mikephil.charting.charts.HorizontalBarChart", Array(ctx))
    
    'prepare the markerview that will pop up when a point of the chart is touched
    'Note the CreateResource in the B4A manifest
    'Also ensure the 4 x markerview files are in /Object/res/drawable folder
    x.LoadXmlLayout(Null, "custom_marker_view_1")
    
    Dim ResourceId As Int
    ResourceId = x.GetResourceId("layout", "custom_marker_view_1")
    
    MarkerView.InitializeNewInstance("com.github.mikephil.charting.components.MyMarkerView", Array(ctx, ResourceId))
    MarkerView.RunMethod("setNumberOfDigits", Array(3))
    
    barChart.RunMethod("setMarkerView", Array(MarkerView))
    'End of preparing markerview
    
    
    barChart.RunMethod("setDrawBarShadow", Array(True))
    barChart.RunMethod("setDrawValueAboveBar", Array(True))
    barChart.RunMethod("setDescription", Array(""))
    'If more than 60 entries are displayed in the chart, no values will be
    'drawn
    barChart.RunMethod("setMaxVisibleValueCount", Array(60))
    barChart.RunMethod("setPinchZoom", Array(False))
    barChart.RunMethod("setDrawBarShadow", Array(True))
    barChart.RunMethod("setDrawGridBackground", Array(True))
    
    Dim xAxisPosition As JavaObject
    'TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    xAxisPosition.InitializeStatic("com.github.mikephil.charting.components.XAxis.XAxisPosition")

    Dim Xaxis As JavaObject
    Xaxis.InitializeNewInstance("com.github.mikephil.charting.components.XAxis", Null)
    Xaxis.RunMethod("setPosition", Array(xAxisPosition.GetField("BOTTOM")))
    Xaxis = barChart.RunMethod("getXAxis", Null)
    Xaxis.RunMethod("setTextSize", Array(12.0f))
    Xaxis.RunMethod("setTextColor", Array(Colors.Red))
    Xaxis.RunMethod("setDrawGridLines", Array(True))
    Xaxis.RunMethod("setDrawAxisLine", Array(True))
    Xaxis.RunMethod("setXaxisTextAngle", Array(45.0f))
    Xaxis.RunMethod("setPosition", Array(xAxisPosition.GetField("TOP")))
    Xaxis.RunMethod("setGridLineWidth", Array(0.3f))

    Dim YaxisLeft As JavaObject
    YaxisLeft.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    barChart.RunMethod("getAxisLeft", Null)
    YaxisLeft.RunMethod("setDrawAxisLine", Array(True))
    YaxisLeft.RunMethod("setDrawGridLines", Array(True))
    YaxisLeft.RunMethod("setGridLineWidth", Array(0.3f))
    
    Dim YaxisRight As JavaObject
    YaxisRight.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    barChart.RunMethod("getAxisRight", Null)
    YaxisRight.RunMethod("setDrawAxisLine", Array(True))
    YaxisRight.RunMethod("setDrawGridLines", Array(True))
    YaxisRight.RunMethod("setGridLineWidth", Array(0.3f))

    Legend.InitializeNewInstance("com.github.mikephil.charting.components.Legend", Null)
    Legend = barChart.RunMethod("getLegend", Null)
    
    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")
    
    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    Legend.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))
    Legend.RunMethod("setXEntrySpace", Array(7.0f))
    
    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
    Legend.RunMethod("setForm", Array(Legendform.GetField("SQUARE")))
    
    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    Legend.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    Legend.RunMethod("setTextSize", Array(14.0f))
    Legend.RunMethod("setTextColor", Array(Colors.Black))
    
    Dim xVals As List
    xVals.Initialize
    
    For i = 0 To 11
        xVals.Add(" " & (2020 + i))
    Next
    
    Dim yVals1, yVals2, yVals3 As List
    yVals1.Initialize
    yVals2.Initialize
    yVals3.Initialize

    For i = 0 To 11
        yVals1.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BarEntry", Array((Rnd(0,255)).As(Float), i)))
        yVals2.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BarEntry", Array((Rnd(0,255)).As(Float), i)))
        yVals3.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BarEntry", Array((Rnd(0,255)).As(Float), i)))
    Next
    
    Dim set1 As JavaObject
    set1.InitializeNewInstance("com.github.mikephil.charting.data.BarDataSet", Array(yVals1, "Company A"))
    set1.RunMethod("setColor", Array(Colors.Red))
    Dim set2 As JavaObject
    set2.InitializeNewInstance("com.github.mikephil.charting.data.BarDataSet", Array(yVals2, "Company B"))
    set2.RunMethod("setColor", Array(Colors.Green))
    Dim set3 As JavaObject
    set3.InitializeNewInstance("com.github.mikephil.charting.data.BarDataSet", Array(yVals3, "Company C"))
    set3.RunMethod("setColor", Array(Colors.Blue))
    
    Dim dataSets As List
    dataSets.Initialize
    dataSets.Add(set1)
    dataSets.Add(set2)
    dataSets.Add(set3)

    Dim data As JavaObject
    data.InitializeNewInstance("com.github.mikephil.charting.data.BarData", Array(xVals, dataSets))
    data.RunMethod("setGroupSpace", Array(100.0f))
    data.RunMethod("setValueTextSize", Array(12.0f))

    barChart.RunMethod("setData", Array(data))
    barChart.RunMethod("setHighlightEnabled", Array(True))
    barChart.RunMethod("setTouchEnabled", Array(True))
    barChart.RunMethod("setDragEnabled", Array(True))
    barChart.RunMethod("setScaleEnabled", Array(True))
    
    Dim valueFormatter As JavaObject
    valueFormatter.InitializeNewInstance("com.github.mikephil.charting.utils.MyValueFormatter", Null)
    valueFormatter.RunMethod("setNumberOfDigits", Array(1))
    YaxisLeft.RunMethod("setValueFormatter", Array(valueFormatter))
    YaxisRight.RunMethod("setValueFormatter", Array(valueFormatter))
    data.RunMethod("setValueFormatter", Array(valueFormatter))
    
'    Linear, EaseInQuad, EaseOutQuad, EaseInOutQuad, EaseInCubic, EaseOutCubic, EaseInOutCubic, EaseInQuart, EaseOutQuart, EaseInOutQuart, EaseInSine, EaseOutSine, EaseInOutSine, EaseInExpo, EaseOutExpo, EaseInOutExpo, EaseInCirc, EaseOutCirc, EaseInOutCirc, EaseInElastic, EaseOutElastic, EaseInOutElastic, EaseInBack, EaseOutBack, EaseInOutBack, EaseInBounce, EaseOutBounce, EaseInOutBounce
    Dim EasingOption As JavaObject
    EasingOption.InitializeStatic("com.github.mikephil.charting.animation.Easing.EasingOption")
    barChart.RunMethod("animateY", Array(2000, EasingOption.GetField("EaseInOutBounce")))
    Activity.AddView(barChart, 0, 5%y, 100%x, 80%y)
    
    Activity.Invalidate
    
    Dim e As Object = barChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "bar_clicked", False)
    barChart.RunMethod("setOnChartValueSelectedListener", Array(e))

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub bar_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0)) 'logs xIndex and yValue
        Log(Args(2)) 'logs xIndex and DataSetIndex
        Log(" ")
    Else
        'do nothing
    End If   
    Return Null
    
End Sub
 

Attachments

  • mpAndroidHorizontalBarChartJO.zip
    29.3 KB · Views: 87

Johan Schoeman

Expert
Licensed User
Longtime User
Should be good to get you going with the Scatter Chart. Have attached the Java Code so that you can reference the B4A code (using JavaObject) to the original Java project.

1.png


Sample Code:

ScatterChart:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidScatterChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
   
    Dim ctx As JavaObject
    Dim scatterChart As JavaObject
    Dim Entry As JavaObject
    Dim scatterData As JavaObject
    Dim Legend As JavaObject
    Dim MarkerView As JavaObject
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
   
    Dim x As XmlLayoutBuilder
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
   
    ctx.InitializeContext
    scatterChart.InitializeNewInstance("com.github.mikephil.charting.charts.ScatterChart", Array(ctx))
   
    Dim e As Object = scatterChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "line_clicked", False)
    scatterChart.RunMethod("setOnChartValueSelectedListener", Array(e))
    scatterChart.RunMethod("setDescription", Array(""))
   
    scatterChart.RunMethod("setDrawGridBackground", Array(True))
    scatterChart.RunMethod("setHighlightEnabled", Array(True))
    scatterChart.RunMethod("setTouchEnabled", Array(True))
    scatterChart.RunMethod("setDragEnabled", Array(True))
    scatterChart.RunMethod("setScaleEnabled", Array(True))
    scatterChart.RunMethod("setMaxVisibleValueCount", Array(200))
    scatterChart.RunMethod("setPinchZoom", Array(True))

    x.LoadXmlLayout(Null, "custom_marker_view_2")
   
    Dim ResourceId As Int
    ResourceId = x.GetResourceId("layout", "custom_marker_view_2")
   
    MarkerView.InitializeNewInstance("com.github.mikephil.charting.components.MyMarkerView", Array(ctx, ResourceId))
    MarkerView.RunMethod("setNumberOfDigits", Array(1))
   
    scatterChart.RunMethod("setMarkerView", Array(MarkerView))
   
    Legend.InitializeNewInstance("com.github.mikephil.charting.components.Legend", Null)
    Legend = scatterChart.RunMethod("getLegend", Null)
   
    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")
   
    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    Legend.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))
    Legend.RunMethod("setXEntrySpace", Array(7.0f))
   
    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
    Legend.RunMethod("setForm", Array(Legendform.GetField("SQUARE")))
   
    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    Legend.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    Legend.RunMethod("setTextSize", Array(14.0f))
    Legend.RunMethod("setTextColor", Array(Colors.Black))
   
    Dim YAxis As JavaObject
    YAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    YAxis = scatterChart.RunMethod("getAxisLeft", Null)
    scatterChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(False))
   
    Dim xAxis As JavaObject
    xAxis.InitializeNewInstance("com.github.mikephil.charting.components.XAxis", Null)
    xAxis = scatterChart.RunMethod("getXAxis", Null)
    xAxis.RunMethod("setDrawGridLines", Array(True))
    xAxis.RunMethod("setTextSize", Array(12.0f))
    xAxis.RunMethod("setTextColor", Array(Colors.Red))
    xAxis.RunMethod("setDrawAxisLine", Array(True))
    xAxis.RunMethod("setXaxisTextAngle", Array(45.0f))
   
    Dim xAxisPosition As JavaObject
'    'TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    xAxisPosition.InitializeStatic("com.github.mikephil.charting.components.XAxis.XAxisPosition")
    xAxis.RunMethod("setPosition", Array(xAxisPosition.GetField("BOTTOM")))

    Dim xvals As List
    xvals.Initialize
    For i = 0 To 100
        xvals.Add(i & "")
    Next

    Dim yVals1, yVals2, yVals3 As List
    yVals1.Initialize
    yVals2.Initialize
    yVals3.Initialize

    For i = 0 To 100
        yVals1.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.Entry", Array(Rnd(0, 201).As(Float), i)))
        yVals2.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.Entry", Array(Rnd(0, 201).As(Float), i)))
        yVals3.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.Entry", Array(Rnd(0, 201).As(Float), i)))
    Next
   
    Dim scatterShape As JavaObject
    scatterShape.InitializeStatic("com.github.mikephil.charting.charts.ScatterChart.ScatterShape")
   
    Dim set1, set2, set3 As JavaObject
    set1.InitializeNewInstance("com.github.mikephil.charting.data.ScatterDataSet", Array(yVals1, "DS 1"))
    set1.RunMethod("setScatterShape", Array(scatterShape.GetField("SQUARE")))
    set1.RunMethod("setColor", Array(Colors.Red))
   
    set2.InitializeNewInstance("com.github.mikephil.charting.data.ScatterDataSet", Array(yVals2, "DS 2"))
    set2.RunMethod("setScatterShape", Array(scatterShape.GetField("CIRCLE")))
    set2.RunMethod("setColor", Array(Colors.Green))
   
    set3.InitializeNewInstance("com.github.mikephil.charting.data.ScatterDataSet", Array(yVals3, "DS 3"))
    set3.RunMethod("setScatterShape", Array(scatterShape.GetField("CROSS")))
    set3.RunMethod("setColor", Array(Colors.Blue))


    set1.RunMethod("setScatterShapeSize", Array(8.0f))
    set2.RunMethod("setScatterShapeSize", Array(8.0f))
    set3.RunMethod("setScatterShapeSize", Array(8.0f))
   
    Dim scatterDataSetList As List
    scatterDataSetList.Initialize
    scatterDataSetList.Add(set1)
    scatterDataSetList.Add(set2)
    scatterDataSetList.Add(set3)
   
    'Linear, EaseInQuad, EaseOutQuad, EaseInOutQuad, EaseInCubic, EaseOutCubic, EaseInOutCubic, EaseInQuart, EaseOutQuart, EaseInOutQuart, EaseInSine, EaseOutSine, EaseInOutSine, EaseInExpo, EaseOutExpo, EaseInOutExpo, EaseInCirc, EaseOutCirc, EaseInOutCirc, EaseInElastic, EaseOutElastic, EaseInOutElastic, EaseInBack, EaseOutBack, EaseInOutBack, EaseInBounce, EaseOutBounce, EaseInOutBounce
    Dim EasingOption As JavaObject
    EasingOption.InitializeStatic("com.github.mikephil.charting.animation.Easing.EasingOption")
    scatterChart.RunMethod("animateY", Array(2000, EasingOption.GetField("EaseInOutBounce")))
   

    Dim scatterData As JavaObject
    scatterData.InitializeNewInstance("com.github.mikephil.charting.data.ScatterData", Array(xvals, scatterDataSetList))

    Dim yLeftAxis, yRightAxis As JavaObject
    yLeftAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yRightAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yLeftAxis = scatterChart.RunMethod("getAxisLeft", Null)
    yRightAxis = scatterChart.RunMethod("getAxisRight", Null)
    yLeftAxis.RunMethod("removeAllLimitLines", Null)
    yLeftAxis.RunMethod("setAxisMaxValue", Array(220.0f))
    yLeftAxis.RunMethod("setAxisMinValue", Array(0.0f))
    yLeftAxis.RunMethod("setStartAtZero", Array(True))
    yLeftAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yLeftAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yLeftAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
   
    yRightAxis.RunMethod("setAxisMaxValue", Array(220.0f))
    yRightAxis.RunMethod("setAxisMinValue", Array(0.0f))
    yRightAxis.RunMethod("setStartAtZero", Array(True))
    yRightAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yRightAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yRightAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
   
    Dim valueFormatter As JavaObject
    valueFormatter.InitializeNewInstance("com.github.mikephil.charting.utils.MyValueFormatter", Null)
    valueFormatter.RunMethod("setNumberOfDigits", Array(1))
    yLeftAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    yRightAxis.RunMethod("setValueFormatter", Array(valueFormatter))
   
    scatterChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(True))

    scatterChart.RunMethod("setData", Array(scatterData))
    scatterChart.RunMethod("invalidate", Null)
   
    Activity.AddView(scatterChart, 0, 5%y, 100%x, 80%y)

    Activity.Invalidate
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub line_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0))  'Logs the xIndex and the yValue - index starts at 0
        Log(Args(2))  'Logs the xIndex and the DataSetIndex - inxdex starts at 0
        Log(" ")
    Else
        'do nothing
    End If  
    Return Null
   
End Sub
 

Attachments

  • mpAndroidScatterChartJO.zip
    29.2 KB · Views: 87
  • JavaCode.zip
    191.2 KB · Views: 101
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Should be a good enough start to get you going with Bubble Charts.
Get the Jar in post #1 and copy it to your B4A additional library folder.

Bubble Chart with JavaObject:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidBubbleChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    
    Dim ctx As JavaObject
    Dim bubbleChart As JavaObject
    Dim Entry As JavaObject
    Dim Legend As JavaObject
    Dim MarkerView As JavaObject
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    
    Dim x As XmlLayoutBuilder
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    ctx.InitializeContext
    bubbleChart.InitializeNewInstance("com.github.mikephil.charting.charts.BubbleChart", Array(ctx))
    
    Dim e As Object = bubbleChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "bubble_clicked", False)
    bubbleChart.RunMethod("setOnChartValueSelectedListener", Array(e))
    bubbleChart.RunMethod("setDescription", Array(""))
    
    bubbleChart.RunMethod("setDrawGridBackground", Array(True))
    bubbleChart.RunMethod("setHighlightEnabled", Array(True))
    bubbleChart.RunMethod("setTouchEnabled", Array(True))
    bubbleChart.RunMethod("setDragEnabled", Array(True))
    bubbleChart.RunMethod("setScaleEnabled", Array(True))
    bubbleChart.RunMethod("setMaxVisibleValueCount", Array(200))
    bubbleChart.RunMethod("setPinchZoom", Array(True))

    x.LoadXmlLayout(Null, "custom_marker_view_2")
    
    Dim ResourceId As Int
    ResourceId = x.GetResourceId("layout", "custom_marker_view_2")
    
    MarkerView.InitializeNewInstance("com.github.mikephil.charting.components.MyMarkerView", Array(ctx, ResourceId))
    MarkerView.RunMethod("setNumberOfDigits", Array(1))
    
    bubbleChart.RunMethod("setMarkerView", Array(MarkerView))
    
    Legend.InitializeNewInstance("com.github.mikephil.charting.components.Legend", Null)
    Legend = bubbleChart.RunMethod("getLegend", Null)
    
    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")
    
    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    Legend.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))
    Legend.RunMethod("setXEntrySpace", Array(7.0f))
    
    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
    Legend.RunMethod("setForm", Array(Legendform.GetField("SQUARE")))
    
    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    Legend.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    Legend.RunMethod("setTextSize", Array(14.0f))
    Legend.RunMethod("setTextColor", Array(Colors.Black))
    
    Dim YAxis As JavaObject
    YAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    YAxis = bubbleChart.RunMethod("getAxisLeft", Null)
    bubbleChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(False))
    
    Dim xAxis As JavaObject
    xAxis.InitializeNewInstance("com.github.mikephil.charting.components.XAxis", Null)
    xAxis = bubbleChart.RunMethod("getXAxis", Null)
    xAxis.RunMethod("setDrawGridLines", Array(True))
    xAxis.RunMethod("setTextSize", Array(12.0f))
    xAxis.RunMethod("setTextColor", Array(Colors.Red))
    xAxis.RunMethod("setDrawAxisLine", Array(True))
    xAxis.RunMethod("setXaxisTextAngle", Array(45.0f))
    
    Dim xAxisPosition As JavaObject
'    'TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    xAxisPosition.InitializeStatic("com.github.mikephil.charting.components.XAxis.XAxisPosition")
    xAxis.RunMethod("setPosition", Array(xAxisPosition.GetField("BOTTOM")))

    Dim xvals As List
    xvals.Initialize
    For i = 0 To 20
        xvals.Add(i & "")
    Next

    Dim yVals1, yVals2, yVals3 As List
    yVals1.Initialize
    yVals2.Initialize
    yVals3.Initialize

    For i = 0 To 20
        Dim val1 As Float = Rnd(100, 700).As(Float)
        Dim val2 As Float = Rnd(100, 700).As(Float)
        Dim val3 As Float = Rnd(100, 700).As(Float)
        yVals1.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BubbleEntry", Array(i, val1, val1)))
        yVals2.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BubbleEntry", Array(i, val2, val2)))
        yVals3.Add(Entry.InitializeNewInstance("com.github.mikephil.charting.data.BubbleEntry", Array(i, val3, val3)))
    Next
    
    Dim set1, set2, set3 As JavaObject
    set1.InitializeNewInstance("com.github.mikephil.charting.data.BubbleDataSet", Array(yVals1, "DS 1"))
    set1.RunMethod("setColor", Array(Colors.Red))
    set1.RunMethod("setDrawValues", Array(True))

    
    set2.InitializeNewInstance("com.github.mikephil.charting.data.BubbleDataSet", Array(yVals2, "DS 2"))
    set2.RunMethod("setColor", Array(Colors.Green))
    set2.RunMethod("setDrawValues", Array(True))
    
    set3.InitializeNewInstance("com.github.mikephil.charting.data.BubbleDataSet", Array(yVals3, "DS 3"))
    set3.RunMethod("setColor", Array(Colors.Blue))
    set3.RunMethod("setDrawValues", Array(True))
    
    Dim bubbleDataSetList As List
    bubbleDataSetList.Initialize
    bubbleDataSetList.Add(set1)
    bubbleDataSetList.Add(set2)
    bubbleDataSetList.Add(set3)
    
    'Linear, EaseInQuad, EaseOutQuad, EaseInOutQuad, EaseInCubic, EaseOutCubic, EaseInOutCubic, EaseInQuart, EaseOutQuart, EaseInOutQuart, EaseInSine, EaseOutSine, EaseInOutSine, EaseInExpo, EaseOutExpo, EaseInOutExpo, EaseInCirc, EaseOutCirc, EaseInOutCirc, EaseInElastic, EaseOutElastic, EaseInOutElastic, EaseInBack, EaseOutBack, EaseInOutBack, EaseInBounce, EaseOutBounce, EaseInOutBounce
    Dim EasingOption As JavaObject
    EasingOption.InitializeStatic("com.github.mikephil.charting.animation.Easing.EasingOption")
    bubbleChart.RunMethod("animateY", Array(2000, EasingOption.GetField("EaseInQuad")))
    
    Dim bubbleData As JavaObject
    bubbleData.InitializeNewInstance("com.github.mikephil.charting.data.BubbleData", Array(xvals, bubbleDataSetList))
    bubbleData.RunMethod("setValueTextSize", Array(10.0f))
    bubbleData.RunMethod("setValueTextColor", Array(Colors.White))
    bubbleData.RunMethod("setHighlightCircleWidth", Array(1.5f))


    Dim yLeftAxis, yRightAxis As JavaObject
    yLeftAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yRightAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yLeftAxis = bubbleChart.RunMethod("getAxisLeft", Null)
    yRightAxis = bubbleChart.RunMethod("getAxisRight", Null)
    yLeftAxis.RunMethod("removeAllLimitLines", Null)
    yLeftAxis.RunMethod("setAxisMaxValue", Array(800.0f))
    yLeftAxis.RunMethod("setAxisMinValue", Array(0.0f))
    yLeftAxis.RunMethod("setStartAtZero", Array(True))
    yLeftAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yLeftAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yLeftAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
    yLeftAxis.RunMethod("setSpaceTop", Array(30.0f))
    yLeftAxis.RunMethod("setStartAtZero", Array(False))
    yLeftAxis.RunMethod("setSpaceBottom", Array(30.0f))
    
    
    
    yRightAxis.RunMethod("setAxisMaxValue", Array(800.0f))
    yRightAxis.RunMethod("setAxisMinValue", Array(0.0f))
    yRightAxis.RunMethod("setStartAtZero", Array(True))
    yRightAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yRightAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yRightAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
    yRightAxis.RunMethod("setSpaceTop", Array(30.0f))
    yRightAxis.RunMethod("setStartAtZero", Array(False))
    yRightAxis.RunMethod("setSpaceBottom", Array(30.0f))
    
    Dim valueFormatter As JavaObject
    valueFormatter.InitializeNewInstance("com.github.mikephil.charting.utils.MyValueFormatter", Null)
    valueFormatter.RunMethod("setNumberOfDigits", Array(1))
    yLeftAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    yRightAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    
    bubbleChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(True))

    bubbleChart.RunMethod("setData", Array(bubbleData))
    bubbleChart.RunMethod("invalidate", Null)
    
    Activity.AddView(bubbleChart, 0, 5%y, 100%x, 80%y)

    Activity.Invalidate
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub bubble_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0))  'Logs the xIndex and the yValue - index starts at 0
        Log(Args(2))  'Logs the xIndex and the DataSetIndex - inxdex starts at 0
        Log(" ")
    Else
        'do nothing
    End If   
    Return Null
    
End Sub


1.png
 

Attachments

  • mpAndroidBubbleChartJO.zip
    29.2 KB · Views: 66

Johan Schoeman

Expert
Licensed User
Longtime User
Should be good to get you started with the RealTimeLineChart of mpAndroidCharts using JavaObject
Drag the window left/right to see all the added points
Click on a point to see the clicked point value being logged if the IDE.

B4A code:
#Region  Project Attributes
    #ApplicationLabel: mpAndroidRealTimeLineChart
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#AdditionalJar: mpChartLib

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    
    Dim ctx As JavaObject
    Dim lineChart As JavaObject

    Dim lineDataSet As JavaObject
    Dim lineData As JavaObject
    Dim Legend As JavaObject
    Dim MarkerView As JavaObject
    
    Dim mMonths() As String
    Dim year As Int
    
    Dim t As Timer
    Dim cnt As Int
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    
    Dim x As XmlLayoutBuilder
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    mMonths = Array As String("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec")
    year = 2015
    cnt = 0
    
    t.Initialize("t", 200)

    ctx.InitializeContext
    lineChart.InitializeNewInstance("com.github.mikephil.charting.charts.LineChart", Array(ctx))
    
    x.LoadXmlLayout(Null, "custom_marker_view_1")
    
    Dim ResourceId As Int
    ResourceId = x.GetResourceId("layout", "custom_marker_view_1")
    
    MarkerView.InitializeNewInstance("com.github.mikephil.charting.components.MyMarkerView", Array(ctx, ResourceId))
    MarkerView.RunMethod("setNumberOfDigits", Array(3))

    lineChart.RunMethod("setMarkerView", Array(MarkerView))
    
    Dim e As Object = lineChart.CreateEvent("com.github.mikephil.charting.listener.OnChartValueSelectedListener", "line_clicked", False)
    lineChart.RunMethod("setOnChartValueSelectedListener", Array(e))
    
    lineChart.RunMethod("setDescription", Array(""))
    lineChart.RunMethod("setNoDataTextDescription", Array("You need to provide data for the chart."))
    lineChart.RunMethod("setHighlightEnabled", Array(True))
    lineChart.RunMethod("setTouchEnabled", Array(True))

    lineChart.RunMethod("setDragEnabled", Array(True))
    lineChart.RunMethod("setScaleEnabled", Array(True))
    lineChart.RunMethod("setDrawGridBackground", Array(False))
    lineChart.RunMethod("setPinchZoom", Array(True))
    lineChart.RunMethod("setBackgroundColor", Array(Colors.LightGray))
    
    lineData.InitializeNewInstance("com.github.mikephil.charting.data.LineData", Null)

    lineData.RunMethod("setValueTextColor", Array(Colors.White))
    
    lineChart.RunMethod("setData", Array(lineData))

    Dim l As JavaObject
    l.InitializeNewInstance("com.github.mikephil.charting.components.Legend" , Null)
    l = lineChart.RunMethod("getLegend", Null)
    
    Dim Legendposition As JavaObject
    Legendposition.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendPosition")

    'RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, LEFT_OF_CHART, LEFT_OF_CHART_CENTER, LEFT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER, PIECHART_CENTER
    l.RunMethod("setPosition", Array(Legendposition.GetField("BELOW_CHART_CENTER")))

    Dim Legendform As JavaObject
    'SQUARE, CIRCLE, LINE
    Legendform.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendForm")
    l.RunMethod("setForm", Array(Legendform.GetField("LINE")))
    l.RunMethod("setTextColor", Array(Colors.Black))

    'LEFT_TO_RIGHT, RIGHT_TO_LEFT
    Dim Legenddirection As JavaObject
    Legenddirection.InitializeStatic("com.github.mikephil.charting.components.Legend.LegendDirection")
    l.RunMethod("setDirection", Array(Legenddirection.GetField("LEFT_TO_RIGHT")))
    l.RunMethod("setTextSize", Array(14.0f))
    l.RunMethod("setTextColor", Array(Colors.Black))


    Dim Xaxis As JavaObject
    Xaxis.InitializeNewInstance("com.github.mikephil.charting.components.XAxis", Null)
    Xaxis = lineChart.RunMethod("getXAxis", Null)
    Xaxis.RunMethod("setTextSize", Array(12.0f))
    Xaxis.RunMethod("setTextColor", Array(Colors.Red))
    Xaxis.RunMethod("setDrawGridLines", Array(True))
    Xaxis.RunMethod("setAvoidFirstLastClipping", Array(True))
    Xaxis.RunMethod("setSpaceBetweenLabels", Array(5))
    Xaxis.RunMethod("setDrawAxisLine", Array(True))
    Xaxis.RunMethod("setXaxisTextAngle", Array(45.0f))
    Xaxis.RunMethod("setEnabled", Array(False))
    
    Dim xAxisPosition As JavaObject
    'TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    xAxisPosition.InitializeStatic("com.github.mikephil.charting.components.XAxis.XAxisPosition")
    Xaxis.RunMethod("setPosition", Array(xAxisPosition.GetField("BOTTOM")))

    Dim yLeftAxis, yRightAxis As JavaObject
    yLeftAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yRightAxis.InitializeNewInstance("com.github.mikephil.charting.components.YAxis", Null)
    yLeftAxis = lineChart.RunMethod("getAxisLeft", Null)
    
    yLeftAxis.RunMethod("setAxisMaxValue", Array(100.0f))
    yLeftAxis.RunMethod("setAxisMinValue", Array(0.0f))
    yLeftAxis.RunMethod("setStartAtZero", Array(False))
    yLeftAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yLeftAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yLeftAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
    yLeftAxis.RunMethod("setDrawGridLines", Array(True))
    
    yRightAxis = lineChart.RunMethod("getAxisRight", Null)
    yRightAxis.RunMethod("setAxisMaxValue", Array(100.0f))
    yRightAxis.RunMethod("setAxisMinValue", Array(0.0f))
    yRightAxis.RunMethod("setStartAtZero", Array(False))
    yRightAxis.RunMethod("enableGridDashedLine", Array(10.0f, 10.0f, 0.0f))
    yRightAxis.RunMethod("setTextColor", Array(Colors.DarkGray))
    yRightAxis.RunMethod("setDrawLimitLinesBehindData", Array(True))
    yRightAxis.RunMethod("setDrawGridLines", Array(True))
    lineChart.RunMethodJO("getAxisRight", Null).RunMethod("setEnabled", Array(True))
    
    Dim valueFormatter As JavaObject
    valueFormatter.InitializeNewInstance("com.github.mikephil.charting.utils.MyValueFormatter", Null)
    valueFormatter.RunMethod("setNumberOfDigits", Array(3))
    yLeftAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    yRightAxis.RunMethod("setValueFormatter", Array(valueFormatter))
    
    Activity.AddView(lineChart, 0, 5%y, 100%x, 80%y)
    
    Activity.Invalidate
    
    t.Enabled = True
    
End Sub

Sub addEntry
    
    Dim data As JavaObject
    data.InitializeNewInstance("com.github.mikephil.charting.data.LineData", Null)
    data = lineChart.RunMethod("getData", Null)
    
    If data <> Null Then

        Dim set As JavaObject
        set.InitializeNewInstance("com.github.mikephil.charting.data.LineDataSet", Array(Null, "dynamic data"))
        set = data.RunMethod("getDataSetByIndex", Array(0))

        If set.IsInitialized = False Then

            set = createSet
            data.RunMethod("addDataSet", Array(set))
        End If
        data.RunMethod("addXValue", Array((mMonths(data.RunMethod("getXValCount", Null) Mod 12) & " " & (year + data.RunMethod("getXValCount", Null) / 12))))

        Dim Entry As JavaObject
        Dim myval As Float = (Rnd(0,101)/100) * 40 + 30.0f

        Entry.InitializeNewInstance("com.github.mikephil.charting.data.Entry", Array(myval, set.RunMethod("getEntryCount", Null)))
        data.RunMethod("addEntry", Array(Entry, 0))

        data.RunMethod("notifyDataChanged", Null)
            
'        // let the chart know it's data has changed
        lineChart.RunMethod("notifyDataSetChanged", Null)
'       
'        // limit the number of visible entries
        lineChart.RunMethod("setVisibleXRangeMaximum", Array(120.0f))
'        // mChart.setVisibleYRange(30, AxisDependency.LEFT);
        
'        // move To the latest Entry
        Dim d As Float = 121.0
        lineChart.RunMethod("moveViewToX", Array(data.RunMethod("getXValCount", Null).As(Float) - d))
        
    End If
    
    Activity.Invalidate
    
    
End Sub


Sub createSet As JavaObject
    
    Dim set As JavaObject
    set.InitializeNewInstance("com.github.mikephil.charting.data.LineDataSet", Array(Null, "Dynamic Data"))
    Dim axisDependancy As JavaObject
    axisDependancy.InitializeStatic("com.github.mikephil.charting.components.YAxis.AxisDependency")
    
    set.RunMethod("setAxisDependency", Array(axisDependancy.GetField("LEFT")))
    set.RunMethod("setColor", Array(Colors.Cyan))
    set.RunMethod("setCircleColor", Array(Colors.Black))
    set.RunMethod("setLineWidth", Array(2.0f))
    set.RunMethod("setCircleSize", Array(4.0f))
    set.RunMethod("setFillAlpha", Array(65))
    set.RunMethod("setFillColor", Array(Colors.Blue))
    set.RunMethod("setHighLightColor", Array(Colors.RGB(244, 117, 117)))
    set.RunMethod("setValueTextColor", Array(Colors.Black))
    set.RunMethod("setValueTextSize", Array(9.0f))
    set.RunMethod("setDrawValues", Array(False))
    Return set
    
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
    t.Enabled = False

End Sub

Sub line_clicked_Event (MethodName As String, Args() As Object) As Object

    If MethodName = "onValueSelected" Then
        Log(Args(0))
        Log(" ")
    Else
        'do nothing
    End If   
    Return Null
    
End Sub

Sub t_tick
    
    addEntry
    
End Sub


1.png
 

Attachments

  • mpAndroidRealTimeLineChartJO.zip
    29.6 KB · Views: 71
Top