【问题标题】:iOS CorePlot: y-axis rendered in the wrong locationiOS CorePlot:y 轴呈现在错误的位置
【发布时间】:2018-04-25 15:20:46
【问题描述】:

我正在尝试在核心图中使用 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 轴变得可见:

提前谢谢你!

【问题讨论】:

    标签: ios swift core-plot


    【解决方案1】:

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

    【讨论】:

    • 非常感谢您出色的图书馆和及时的支持!我还必须调整 plotAreaFrame 填充,以免标签被切断。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多