【发布时间】:2016-04-14 23:25:33
【问题描述】:
我在我的 mac 应用程序中使用coreplot 来绘制图表,它工作正常。我需要在轴上显示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())但标签没有改变没有效果。还尝试更改
tickLocationlike :xLabel.tickLocation = NSNumber(double: label/60)无效随着
majorTickLocations的变化,例如:xLocations.append(NSNumber(double: label/60))它的行为很奇怪 所有的 x 轴标签都乱了。
我试着按照这个例子:StackOverFlow Post
谁能指导一下出了什么问题[记住我使用的是 Swift] 吗?
【问题讨论】:
标签: ios swift swift2 core-plot