更改 Coreplot 图形的刻度标签
Change Tick Labels for Coreplot Graph
我在我的 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.
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 effect
With changing majorTickLocations
like :
xLocations.append(NSNumber(double: label/60))
it behaving weird and
all x-axis labels are messed.
我试着按照这个例子:Whosebug Post
谁能指导一下哪里出了问题[记得我用的是Swift]吗?
.LocationsProvided
标签策略使用 labelFormatter
从刻度位置自动创建标签。如果您想使用自定义标签,请改用 .None
标签政策。
我自己解决了这个问题:
let xAxis = axisSet.axes?[0]
xAxis?.majorIntervalLength = activity.durationSeconds / 60 / 4
xAxis?.labelingPolicy = CPTAxisLabelingPolicy.FixedInterval
现在标签按要求来了。
我在我的 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.
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.
我试着按照这个例子:Whosebug Post
谁能指导一下哪里出了问题[记得我用的是Swift]吗?
.LocationsProvided
标签策略使用 labelFormatter
从刻度位置自动创建标签。如果您想使用自定义标签,请改用 .None
标签政策。
我自己解决了这个问题:
let xAxis = axisSet.axes?[0]
xAxis?.majorIntervalLength = activity.durationSeconds / 60 / 4
xAxis?.labelingPolicy = CPTAxisLabelingPolicy.FixedInterval
现在标签按要求来了。