Android Question MPAndroidCharts - skipping a value

wimpie3

Well-Known Member
Licensed User
I'm using MPAndroidCharts (old, but still is the best chart library for B4A).

I'd like to skip values on the X-axis.

At this moment, every X-axis label needs to have a value. Like this:
B4X:
mbc1.LegendText = Array As String("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20","21", "22", "23", "24")
mbc1.ChartData = Array As Float(52.0, 58.0, 63.0, 70.0, 48.0, 38.0, 25.0, 53.0, 66.0, 46.0, 67.0, 75.0, 52.0, 58.0, 63.0, 70.0, 48.0, 38.0, 25.0, 53.0, 66.0, 46.0, 67.0, 75.0)
But what if I want to show the label "2" but don't have a value at that point in the chart? Putting NULL as the value, doesn't work.

Any help?
 

Johan Schoeman

Expert
Licensed User
I am not sure if the original Github project that I have wrapped allows for this. If you can find something about this in MpAndroidCharts then I will try and add it to the library. The Github project has changed a lot during the last 4 years - I will only add the functionality if it does not mean having to wrap any of the graphs from scratch.

Rgds

JS
 

Johan Schoeman

Expert
Licensed User
See my posting here - new lib files in post #1 of that thread (V1.15)
https://www.b4x.com/android/forum/t...y-files-in-post-362.58017/page-19#post-697283

Have done a mod for MPLineChart (i.e single line chart)

Try with attached project (and new lib files in post #1 of the mpandroidcharts thread).

Take note of the method added to either ignore or to take into consideration the zero values.

B4X:
    Dim myfloat() As Float = Array As Float(31.534, 0, 0, 0, 83.659, -54.103, 60.512, -15.773, 0, -45.845, 68.313, -5.708)
    mlc4.IgnoreZero = True
    Dim myxvals() As String = Array As String("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

mlc4.IgnoreZero = False
zeropic1.png



mlc4.IgnoreZero = True
zeropic2.png


If you set mlc4.IgnoreZero = True and you do have a valid zero value that needs to be displayed, then just change the valid zero value to something like 0.00001. As this is not zero it will display it as a "valid" zero. Something like this:

B4X:
    Dim myfloat() As Float = Array As Float(31.534, 0, 0, 0, 83.659, -54.103, 60.512, -15.773, 0.00001, -45.845, 68.313, -5.708)
    mlc4.IgnoreZero = True
    Dim myxvals() As String = Array As String("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
zeropic3.png
 

Attachments

Last edited:

wimpie3

Well-Known Member
Licensed User
Fantastic, thanks!!! I also need this on combined line charts... Does it work there as well?
 

wimpie3

Well-Known Member
Licensed User
Wow thanks, that would really, really help me. Do you have a PayPal address for donations?
 

Johan Schoeman

Expert
Licensed User
Wow thanks, that would really, really help me. Do you have a PayPal address for donations?
No donation required - thank you. Buy yourself a beer or two!

Version 1.16 in Post #1 of the mpAndroidCharts thread takes care of MultiLineCharts. Some old sample code that I have added the "IgnoreZero" to:

B4X:
#Region  Project Attributes
    #ApplicationLabel: MPMultiLineChartV3
    #VersionCode: 3
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#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.
   

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private mlc1  As MultiLineChart
    Private Button1 As Button
    Dim flag As Int = 0

    Private ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
   
   
    ImageView1.Visible = False

   
    mlc1.ChartDescription = "TITLE : Just some random data"   'SORTED OUT DISPLAY OF DESCRIPTION IF NO TITLE IS SET - 22 OCT 2015
    mlc1.ChartDescriptionColor = Colors.Red
    mlc1.ChartDescriptionTextSize = 15  
    mlc1.GridBackgroundColor = Colors.DarkGray
   
    mlc1.LegendShapeSize = 10.0

    mlc1.LegendTextColorsToMatchLineColors = True                                       'ADDED 25 Dec 2015  
   
'    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,

'    CIRCLE, SQUARE, LINE    
    mlc1.setTheLegendPositionAndForm("BELOW_CHART_CENTER","SQUARE")
    mlc1.LegendXEntrySpace = 35.0f                                                      'ADDED 25 Dec 2015
    mlc1.LegendYEntrySpace = 25.0f                                                      'ADDED 25 DEc 2015  
    mlc1.TheLegendColor = Colors.White
    mlc1.TheLegendTextSize = 10.0  
    mlc1.LegendText = Array As String("2011", "2012", "2013", "2014", "2015")

    mlc1.ValueTextColor = Array As Int(Colors.White, Colors.Red, Colors.White, Colors.Red, Colors.White)
    mlc1.ValueTextSize = Array As Float(12.0, 10.0, 14.0, 8.0, 16.0)

    mlc1.YaxisTextSize = 10.0
    mlc1.IgnoreZero = True
'   We are going to draw 5 charts/graphs
    mlc1.Chart_1_Data = Array As Float(10.0, 30.0, 20.0, 0, 40.0, 0, 45.0, -15.0, 38.3, 0.00001, 29.8, -15.7)
'    mlc1.Chart_2_Data = Array As Float(1200.0, 750.0, 450.0, 960.0, 730.0, 1100.0, 676.5, 985.6, 1010.8, 836.4, 498.5, 965.3)
    mlc1.Chart_2_Data = Array As Float(-10.0, 0, -20.0, 50.0, -40.0, 30.0, -45.0, 15.0, -38.3, 0, -29.8, 15.7)        
'    mlc1.Chart_4_Data = Array As Float(345.0, -380.2, -250.4, 849.7, 445.0, 600.5, -300.0, 50.0, 550.0, 1100.0, 894.2, 993.8)  
    mlc1.Chart_3_Data = Array As Float(40.0, 0, 50.0, -80.0, 0, 0, 75.0, -45.0, 0.00001, 0, 59.8, -45.7)              
   
'   25 DEC 2015 - THE DEFAULT Y-AXIS FOR ALL LINE CHARTS HAS BEEN SET TO THE LEFT Y-AXIS
'    Dim ad() As String = Array As String("RIGHT", "LEFT", "RIGHT", "LEFT", "RIGHT")     'NEW - ADDED Y AXIS DEPENDANCY - 22 OCT 2015
'    mlc1.YaxisDependancy = ad                                                           'NEW - ADDED Y AXIS DEPENDANCY - 22 OCT 2015

    mlc1.DrawGraphValues = Array As Boolean(True, True, True, True, True)  
    mlc1.MaxVisibleValueCount = 20
   
'   Maximum 5 colors to be passed ==> must be at least equal to the number of data sets that are passed i.e maximum 5
'   If you pass only for eg 2 data sets then at least 2 colors need to be passed
'   If more that 2 colors are passed with for eg only 2 data sets then only the first 2 colors will be used in the color array
    mlc1.LineColors = Array As Int(Colors.Yellow, Colors.Red, Colors.Blue, Colors.Green, Colors.Cyan)
    mlc1.GraphLineWidth = Array As Float(2.0, 3.0, 4.0, 5.0, 6.0)
   
    mlc1.DrawFilled = Array As Boolean(False, False, False, False, False)                                 'BUG FIX - 25 DEC 2015
    mlc1.FillColor = Array As Int(Colors.Yellow, Colors.Red, Colors.Blue, Colors.Green, Colors.Cyan)      'BUG FIX - 25 DEC 2015
   
    mlc1.XaxisTextColor = Colors.Green
    mlc1.XaxisTextSize = 12.0  
    mlc1.XaxisLables = Array As String("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
'  TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    mlc1.XaxisLabelPosition = "TOP"
    mlc1.XaxisTextAngle = 30                          'NEW - 'AMENDED CODE TO SET THE ANGLE OF THE X AXIS LABELS BETWEEN -90 and 90 - 22 OCT 2015
    mlc1.VerticalGridColor = Colors.White             'NEW  - ADDED ABILITY TO SET THIS COLOR - 22 OCT 2015

    mlc1.DrawXaxisGridLines = True
   
    mlc1.DrawYaxisLeftGridLines = True                 'NEW - ADDED 23 OCT 2015
    mlc1.DrawYaxisRightGridLines = False               'NEW - ADDED 23 OCT 2015
   
    mlc1.YaxisLeftMinVal = -100                        'BUG FIX 25 DEC 2015 TO SET LEFT Y AXIS MIN VAL INDEPENDANTLY - comment it out so that the scale will be set automatically
    mlc1.YaxisLeftMaxVal = 100                      'AMENDED 22 OCT 2015 TO SET LEFT Y AXIS MAX VAL INDEPENDANTLY - comment it out so that the scale will be set automatically

'    mlc1.YaxisRightMinVal = -175                      'BUG FIX 25 DEC 2015 TO SET RIGHT Y AXIS MIN VAL INDEPENDANTLY - comment it out so that the scale will be set automatically
'    mlc1.YaxisRightMaxVal = 175                        'AMENDED 22 OCT 2015 TO SET RIGHT Y AXIS MAX VAL INDEPENDANTLY - comment it out so that the scale will be set automatically
   
    mlc1.YaxisTextSize = 10.0
   
    mlc1.YaxisLeftTextColor = Colors.Cyan              'NEW - ADDED 23 OCT 2015    
    mlc1.YaxisRightTextColor = Colors.Yellow          'NEW - ADDED 23 OCT 2015
   
    mlc1.ShowYaxisRightLabels = False
    mlc1.ShowYaxisLeftLabels = True
   
    mlc1.DrawLeftGridDashed = False                   'NEW - ADDED 23 OCT 2015
    mlc1.DrawRightGridDashed = True                   'NEW - ADDED 23 OCT 2015
   
    mlc1.YaxisTextAngle = -15                         'NEW - 'ADDED ABILITY TO SET THE ANGLE OF THE Y AXIS LABELS BETWEEN -90 and 90 - 22 OCT 2015
    mlc1.HorizontalGridColorLeft = Colors.White       'NEW - 'ADDED ABILITY TO SET THIS COLOR - 22 OCT 2015
    mlc1.HorizontalGridColorRight = Colors.Yellow     'NEW - 'ADDED ABILITY TO SET THIS COLOR - 22 OCT 2015
   
    mlc1.CubicIntensity = Array As Float(0.2, 0.3, 0.3, 0.4, 0.5)
    mlc1.DrawCubicGraph = Array As Boolean(True, True, True, False, True)
    mlc1.DrawDashedLine = Array As Boolean(True, False, True, False, True)
    mlc1.GraphCircleSize = Array As Float(4.0, 5.0, 6.0, 7.0, 8.0)
    mlc1.GraphCircleColor = Array As Int(Colors.Red, Colors.Green, Colors.Magenta, Colors.Black, Colors.Blue)
    mlc1.DrawGraphHollowCircles = Array As Boolean(True, False, True, False, True)
    mlc1.MarkerToUse = 4
   
    mlc1.XAnimate = False
    mlc1.YAnimate = True
    mlc1.ChartAnimationTime = 2000                     'milliseconds
   
'   the number of charts to be drawn (maximum 5, in this case 5)
'   the number of x-axis values per chart (in this case 12 = number of elements in the array passed to mlc1.XaxisLables)
    mlc1.setLineData(3,12)
See this part of the code....

B4X:
    mlc1.IgnoreZero = True
'   We are going to draw 5 charts/graphs
    mlc1.Chart_1_Data = Array As Float(10.0, 30.0, 20.0, 0, 40.0, 0, 45.0, -15.0, 38.3, 0.00001, 29.8, -15.7)
'    mlc1.Chart_2_Data = Array As Float(1200.0, 750.0, 450.0, 960.0, 730.0, 1100.0, 676.5, 985.6, 1010.8, 836.4, 498.5, 965.3)
    mlc1.Chart_2_Data = Array As Float(-10.0, 0, -20.0, 50.0, -40.0, 30.0, -45.0, 15.0, -38.3, 0, -29.8, 15.7)        
'    mlc1.Chart_4_Data = Array As Float(345.0, -380.2, -250.4, 849.7, 445.0, 600.5, -300.0, 50.0, 550.0, 1100.0, 894.2, 993.8)  
    mlc1.Chart_3_Data = Array As Float(40.0, 0, 50.0, -80.0, 0, 0, 75.0, -45.0, 0.00001, 0, 59.8, -45.7)
pic1.png
 
Top