iOS CorePlot:y 轴呈现在错误的位置
iOS CorePlot: y-axis rendered in the wrong location
我正在尝试在 Core Plot 中制作一个带有 y 轴的 XY 图,但我已经 运行 解决了这个问题。
这是一个最小的重现示例:
let values = [(15.5, 1524665.0), (10.4, 1525265.0), (30.8, 1525565.0), (20.8, 1525865.0)]
func initPlot() {
let graph = CPTXYGraph(frame: hostView.bounds)
hostView.hostedGraph = graph
let xMin = 1524665.0
let xMax = 1525865.0 + 86400.0
guard let plotSpace = graph.defaultPlotSpace as? CPTXYPlotSpace else { return }
plotSpace.xRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(xMin), lengthDecimal: CPTDecimalFromDouble(xMax - xMin))
plotSpace.yRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(0.0), lengthDecimal: CPTDecimalFromDouble(70.0))
guard let axisSet = graph.axisSet as? CPTXYAxisSet else { return }
if let axis = axisSet.yAxis {
axis.labelingPolicy = .automatic
}
if let axis = axisSet.xAxis {
axis.labelingPolicy = .none
}
samplesPlot = CPTScatterPlot(frame: graph.bounds)
samplesPlot.dataSource = self
samplesPlot.delegate = self
graph.add(samplesPlot, to: graph.defaultPlotSpace)
}
extension ChartViewController: CPTPlotDataSource, CPTPlotDelegate {
func numberOfRecords(for plot: CPTPlot) -> UInt {
return UInt(values.count)
}
func number(for plot: CPTPlot, field fieldEnum: UInt, record idx: UInt) -> Any? {
if plot == samplesPlot {
if fieldEnum == UInt(CPTScatterPlotField.Y.rawValue) {
return values[Int(idx)].0
} else {
return values[Int(idx)].1
}
}
return nil
}
}
很遗憾,我没有看到 y 轴:
<CPTXYAxisSet:0x1c40fb300; position = CGPoint (187.5 301.5);
bounds = CGRect (0 0; 375 603);
sublayers = (<<<CPTXYAxis: 0x151e23860> bounds: {{0, 0}, {375, 603}}>
with range: <<CPTPlotRange: 0x1c409a7c0> {1524665, 87600}>
viewCoordinates: {0, 0} to {375, 0}>, <<<CPTXYAxis: 0x151e25580>
bounds: {{0, 0}, {375, 603}}> with range: <<CPTPlotRange: 0x1c409a4a0>
{0, 70}> viewCoordinates: {-6526.82, 0} to {-6526.82, 603}>);
轴的渲染位置似乎有误。
如果我将 xMax 值更改为 0.0,则 y 轴变为可见:
提前致谢!
默认轴位置使它们在 (0, 0) 处交叉。如果您想要不同的东西,请更改 y 轴的 orthogonalPosition
或使用 axisConstraints
将轴保持在屏幕上的特定位置,让图表在下方滚动。
我正在尝试在 Core Plot 中制作一个带有 y 轴的 XY 图,但我已经 运行 解决了这个问题。
这是一个最小的重现示例:
let values = [(15.5, 1524665.0), (10.4, 1525265.0), (30.8, 1525565.0), (20.8, 1525865.0)]
func initPlot() {
let graph = CPTXYGraph(frame: hostView.bounds)
hostView.hostedGraph = graph
let xMin = 1524665.0
let xMax = 1525865.0 + 86400.0
guard let plotSpace = graph.defaultPlotSpace as? CPTXYPlotSpace else { return }
plotSpace.xRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(xMin), lengthDecimal: CPTDecimalFromDouble(xMax - xMin))
plotSpace.yRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(0.0), lengthDecimal: CPTDecimalFromDouble(70.0))
guard let axisSet = graph.axisSet as? CPTXYAxisSet else { return }
if let axis = axisSet.yAxis {
axis.labelingPolicy = .automatic
}
if let axis = axisSet.xAxis {
axis.labelingPolicy = .none
}
samplesPlot = CPTScatterPlot(frame: graph.bounds)
samplesPlot.dataSource = self
samplesPlot.delegate = self
graph.add(samplesPlot, to: graph.defaultPlotSpace)
}
extension ChartViewController: CPTPlotDataSource, CPTPlotDelegate {
func numberOfRecords(for plot: CPTPlot) -> UInt {
return UInt(values.count)
}
func number(for plot: CPTPlot, field fieldEnum: UInt, record idx: UInt) -> Any? {
if plot == samplesPlot {
if fieldEnum == UInt(CPTScatterPlotField.Y.rawValue) {
return values[Int(idx)].0
} else {
return values[Int(idx)].1
}
}
return nil
}
}
很遗憾,我没有看到 y 轴:
<CPTXYAxisSet:0x1c40fb300; position = CGPoint (187.5 301.5);
bounds = CGRect (0 0; 375 603);
sublayers = (<<<CPTXYAxis: 0x151e23860> bounds: {{0, 0}, {375, 603}}>
with range: <<CPTPlotRange: 0x1c409a7c0> {1524665, 87600}>
viewCoordinates: {0, 0} to {375, 0}>, <<<CPTXYAxis: 0x151e25580>
bounds: {{0, 0}, {375, 603}}> with range: <<CPTPlotRange: 0x1c409a4a0>
{0, 70}> viewCoordinates: {-6526.82, 0} to {-6526.82, 603}>);
轴的渲染位置似乎有误。 如果我将 xMax 值更改为 0.0,则 y 轴变为可见:
提前致谢!
默认轴位置使它们在 (0, 0) 处交叉。如果您想要不同的东西,请更改 y 轴的 orthogonalPosition
或使用 axisConstraints
将轴保持在屏幕上的特定位置,让图表在下方滚动。