我正在使用coreplot
表示我的mac应用程序&它的工作正常。我需要沿着轴显示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))
它表现得很奇怪 所有x轴标签都是混乱的。
我尝试按照这个例子:StackOverFlow Post
任何人都可以指导哪些内容出错[请记住我使用的是Swift]?
答案 0 :(得分:0)
.LocationsProvided
标签政策使用labelFormatter
自动从刻度位置创建标签。如果您想使用自定义标签,请改用.None
标签政策。
答案 1 :(得分:0)
我自己解决了这个问题:
let xAxis = axisSet.axes?[0]
xAxis?.majorIntervalLength = activity.durationSeconds / 60 / 4
xAxis?.labelingPolicy = CPTAxisLabelingPolicy.FixedInterval
现在标签正在按要求发布。