问题
I am using coreplot
for graphs in my mac application & its working fine. I have a requirement to show custom labels
along the axis for ticks like :
I have a graph for power values against time : Power values -> Y-Axis
& Time -> X-Axis
.
I want to draw graph for Time seconds values but show fixed intervals ticks of minutes along x-axis for which i figured that i should use custom labels but its not happening for me.
Code for custom labels is :
let graph = CPTXYGraph()
plots[0].identifier = PlotType.target.rawValue
plots[1].identifier = PlotType.user.rawValue
for plot in plots {
plot.dataSource = self
plot.delegate = self
graph.addPlot(plot)
}
if let plotArea = graph.plotAreaFrame {
plotArea.borderLineStyle = nil
plotArea.paddingLeft = 25.0
plotArea.paddingBottom = 20.0
plotArea.paddingRight = 10.0
plotArea.paddingTop = 20.0
}
guard let axisSet = graph.axisSet else { return }
let xAxis = axisSet.axes?[0]
var xLabels = [CPTAxisLabel]()
var xLocations = [NSNumber]()
let doubleSecs = Double(activity.durationSeconds)
let labels = [0.0, doubleSecs * 0.25, doubleSecs * 0.50, doubleSecs * 0.75, doubleSecs]
for label in labels {
let xLabel = CPTAxisLabel(text: "\(label/60)", textStyle: axisTextStyle)
xLabel.tickLocation = NSNumber(double: label)
xLocations.append(NSNumber(double: label))
xLabels.append(xLabel)
}
xAxis?.labelingPolicy = CPTAxisLabelingPolicy.LocationsProvided
xAxis?.labelFormatter = axisFormatter
xAxis?.axisLabels = Set(xLabels)
xAxis?.majorTickLocations = Set(xLocations)
chartView.hostedGraph = graph
With above code it is drawing graph & along x-axis it is dividing time in 4 equal portions & showing labels, everything is good.
I want to show labels along x-axis as minutes and remaining drawing is ok.
I tried with this :
let xLabel = CPTAxisLabel(text: "\(label/60)", textStyle: axisTextStyle())
but labels are not changing no effect.Also tried to change
tickLocation
like :xLabel.tickLocation = NSNumber(double: label/60)
No effectWith changing
majorTickLocations
like :xLocations.append(NSNumber(double: label/60))
it behaving weird and all x-axis labels are messed.
I tried to follow this example : StackOverFlow Post
Can anyone guide whats going wrong [Remember I am using Swift] ?
回答1:
The .LocationsProvided
labeling policy uses the labelFormatter
to create labels from the tick locations automatically. If you want to use custom labels, use the .None
labeling policy instead.
回答2:
I solved it myself by doing it like :
let xAxis = axisSet.axes?[0]
xAxis?.majorIntervalLength = activity.durationSeconds / 60 / 4
xAxis?.labelingPolicy = CPTAxisLabelingPolicy.FixedInterval
Now labels are coming as required.
来源:https://stackoverflow.com/questions/36614204/change-tick-labels-for-coreplot-graph