iOS CorePlot:y轴呈现在错误的位置

时间:2018-04-25 15:20:46

标签: ios swift 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

提前谢谢!

1 个答案:

答案 0 :(得分:1)

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