iOS CorePlot : y-axis rendered in the wrong location

标签 ios swift core-plot

我正在尝试在 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 轴:

axis-not-visible

<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 轴变为可见:

axis-visible

提前致谢!

最佳答案

默认轴位置使它们在 (0, 0) 处交叉。如果您想要不同的东西,请更改 y 轴的 orthogonalPosition 或使用 axisConstraints 将轴保持在屏幕上的特定位置,让图形在下方滚动。

关于iOS CorePlot : y-axis rendered in the wrong location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50025937/

相关文章:

ios - UISearchBar 在向下滚动之前不可见

ios - 更新按钮标题时重新执行 Swift if 语句

ios - 通知中心在模拟器上导致 EXC_BAD_ACCESS 的原因是什么,但在设备上却没有?

ios - Core-plot 实时数据重绘图

ios - 核心图 : bring grid lines to the foreground?

xcode - CorePlot 1.2 - pod 安装没有响应

ios - 在 UIView 中创建 WKWebView

ios - Quartz Core 干扰正在运行的单元测试

iphone - (anObject *)在语句前做什么?

swift - 暂停循环,直到在 Swift 中获得 UIAlertController 的响应