How to change the No Data message using iOS Charts?

Viewed 792

When using iOS Charts and there is no data, there is a default message that says "No chart data available". Can this default be edited and handle translations?

3 Answers

This is how you can customize the chart appearance before data is shown.

@IBOutlet private weak var chart: LineChartView!

chart.noDataText = "Loading"
chart.noDataTextColor = .green
chart.noDataFont = UIFont(name: "Helvetica", size: 10.0)!

You can edit the default using .noDataText along the lines of the lines of the following:

lazy var lineChartView: LineChartView = {
    let chartView = LineChartView()
    chartView.noDataText = NSLocalizedString("chart_no_data", comment: "")

    ...
    
    return chartView
}()

FYI I am using a chart in a table view cell and found I needed to set the various noData parameters in awakeFromNib() for them to properly reflected.

override func awakeFromNib() {
    //show the activity indicator which automatically animates
    activityIndicator.isHidden = false
    
    projectionChartView.noDataText = "Loading..."
    projectionChartView.noDataFont = UIFont.systemFont(ofSize: 18.0, weight: .medium)
}
Related