Android Tutorial mpAndroidCharts - PieChart, Line Chart, BarChart and MultiLineChart with 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: 33
  • mpAndroidPieChartJO.zip
    9.9 KB · Views: 30
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: 24

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: 21
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: 24
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: 19

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

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: 14
Top