Search code examples
swiftgraphviewcontrollercore-plot

Is there a way to set custom string Axis Ticks in core plot?


Is there a way to set the Axis Ticks to be non-numeric in CorePlot. So that it doesn't display 1,2,3,4,5.... on the axis, but rather corresponding stings A,B,C,D?


Solution

  • Just create new axis labels from locations through

    func axis(_ axis: CPTAxis, shouldUpdateAxisLabelsAtLocations locations: Set<NSNumber>) -> Bool {
    var newLabels: Set<CPTAxisLabel> = []
    for tickLocation in locations {
        let theLabelTextStyle: CPTTextStyle? = nil 
        let labelString = "?"
        let newLabelLayer = CPTTextLayer(text: labelString, style: theLabelTextStyle)
        let newLabel = CPTAxisLabel(contentLayer: newLabelLayer)
        newLabel.tickLocation = tickLocation
        newLabel.offset = labelOffset
        newLabel.rotation = labelRotation
        newLabels.insert(newLabel)
    }
    axis.axisLabels = newLabels
    return false
    

    }

    func axisShouldRelabel(_ axis: CPTAxis) -> Bool { return true }

    func axis(_ axis: CPTAxis, shouldUpdateMinorAxisLabelsAtLocations locations: Set) -> Bool { return true }