Search code examples
iosswiftios-charts

.noDataText not updating within Chart. Swift 2


I am using the Chart Library (danielgindi/Charts).

I have a segment which displays a Day, Week, Month and Year option. Depending on what is selected will show on the Chart.

I have the following code:

var highestValue : Double = 0.0
@IBAction func segmentChanged(sender: AnyObject) {
    switch durationSegment.selectedSegmentIndex
    {
    case 0:
        segmentSelected = "Day"
        highestValue = 8.0
    case 1:
        segmentSelected = "Week"
        highestValue = 10.0
    case 2:
        segmentSelected = "Month"
        highestValue = 12.0
    case 3:
        segmentSelected = "Year"
        highestValue = 14.0
    default:
        break;
    }

    print("segment selected = \(segmentSelected)")
    displayDurationLabel(segmentSelected, providedStartDate : NSDate())

}


 func setChart(xValues: [String], valuesCandleChart: [Double], durationToBeDisplayed: String) {
    chartView.descriptionText = ""
    print("within setChart, durationToBeDisplayed = \(durationToBeDisplayed)")
    chartView.noDataText = "No spends logged for \(durationToBeDisplayed)" 
chartView.leftAxis.customAxisMax = highestValue
}

When the ViewController is initially called, it uses the segmentSelected = "Month" and the chart displays the correct message however when I change the segment, e.g. "Day", the print shows the correct output however the '.noDataText' doesn't update as expected.

Any help would be appreciated :)


Solution

    • Within the setChart function, at the end call:

    chartView.notifyDataSetChanged()

    • Immediately after calling the setChart function call:

    chartView.legend.resetCustom()