swiftswiftcharts

Swift charts not showing value labels


I am using the Swift Charts library by Daniel Gindi. I am trying to show individual values for each point in a scatter plot.

I have set drawValuesEnabled to true for the set and created a custom IValueFormatter. However, it appears that the method in the formatter class is never even being called, and no labels are shown. What else could be the cause of this?

EDIT: Code to create scatter plot is as follows:

let graphView: ScatterChartView = {
    let scatter = ScatterChartView()
    scatter.translatesAutoresizingMaskIntoConstraints = false
    scatter.chartDescription?.enabled = true
    scatter.dragEnabled = true
    scatter.setScaleEnabled(true)
    //scatter.maxVisibleCount = 200
    scatter.pinchZoomEnabled = false
    scatter.backgroundColor = .clear

    scatter.leftAxis.enabled = false
    scatter.rightAxis.enabled = true
    scatter.rightAxis.drawGridLinesEnabled = false
    scatter.rightAxis.valueFormatter = TimeValueFormatter()

    scatter.xAxis.labelPosition = XAxis.LabelPosition.bottom
    scatter.xAxis.axisMaxLabels = 8
    scatter.xAxis.granularity = 1
    scatter.xAxis.drawLabelsEnabled = true
    scatter.xAxis.drawGridLinesEnabled = false
    //date formatter should be provided by controller

    scatter.legend.enabled = false

    return scatter
}()

And the data set (with x-axis as integers and y-axis as time):

self.dataSet = {
        let set = ScatterChartDataSet(values: entries)
        set.setScatterShape(.circle)
        set.setColor(UIColor.PRColors.prGreen)
        set.valueFormatter = TimeValueFormatter()

        return set
    }()

Expected result is labels with the y-value shown by each point, as in the example project.


Solution

  • Figured it out. Turns out, when you create a data set using an array of ChartDataEntry objects, the array must be immutable (let rather than var), or labels will not be shown.