更改Coreplotgraphics的勾号标签
我在我的Mac应用程序中使用coreplot
来处理graphics,并且工作正常。 我有一个要求,显示custom labels
沿轴的滴答如:
我有一个功率值随时间变化的图表: 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.
自定义标签的代码是:
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
上面的代码是绘制图表和沿着x轴它是划分时间在4个等份和显示标签,一切都很好。
I want to show labels along x-axis as minutes and remaining drawing is ok.
我试着这样做:
let xLabel = CPTAxisLabel(text: "\(label/60)", textStyle: axisTextStyle())
但标签不会改变没有效果。还试图改变
tickLocation
如:xLabel.tickLocation = NSNumber(double: label/60)
没有效果随着更改
majorTickLocations
如:xLocations.append(NSNumber(double: label/60))
它performance怪异,所有的X轴标签是混乱的。
我试图按照这个例子: StackOverFlow Post
任何人都可以指导怎么回事[记得我正在使用Swift]?
.LocationsProvided
标签策略使用labelFormatter
从刻度位置自动创build标签。 如果您想使用自定义标签,请改用.None
标签策略。
我自己解决这个问题,就像这样做:
let xAxis = axisSet.axes?[0] xAxis?.majorIntervalLength = activity.durationSeconds / 60 / 4 xAxis?.labelingPolicy = CPTAxisLabelingPolicy.FixedInterval
现在标签即将到来。