MPAndroidChart to ios-charts syntax conversion

less than 1 minute read

ValueFormatter interface

Android: Simple Formatter

// usage on axis, could also use on data object or dataset
yAxis.setValueFormatter(new MyValueFormatter());

iOS: Simple Formatter

var myValueFormatter = NSNumberFormatter()
// normal value formatter configuration
myValueFormatter.numberStyle = .PercentStyle
yAxis.valueFormatter = myValueFormatter
// usage on axis, could also use on data object or dataset
var leftAxis = chart.getAxis(ChartYAxis.AxisDependency.Left)

Android: Custom Formatter

public class MyValueFormatter implements ValueFormatter {

    private DecimalFormat mFormat;

    public MyValueFormatter() {
        mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
    }

    @Override
    public String getFormattedValue(float value) {
        return mFormat.format(value) + " $"; // append a dollar-sign
    }
}

iOS: Custom Formatter (not directly equivalent to Android version above)

  class MyValueFormatter: NSNumberFormatter {
    required init(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
    }
    
    override init() {
      super.init()
      self.locale = NSLocale.currentLocale()
    }
    
    override func stringFromNumber(duration: NSNumber) -> String? {
      return String(format: "%01d$", duration.floatValue)
    }
    
    // Swift 1.2 or above
    static let sharedInstance = FeedDurationFormatter()
  }

Y axis

Android:

YAxis leftAxis = chart.getAxisLeft();

iOS:

var leftAxis = chart.getAxis(ChartYAxis.AxisDependency.Left)

####Axis properties:

Android:

axis.setEnabled(false)

iOS:

axis.enabled = false

####Legend Properties:

Android:

chart.getLegend().setEnabled(true)

iOS:

chart.legend.enabled = false

Label Text Color

Android:

XLabels label = chart.getXLabels();
label.setTextColor(getResources().getColor(android.R.color.white));

iOS:

var xAxis = chartView.xAxis
xAxis.labelTextColor = UIColor.whiteColor()