Alexander M.
Alexander M.

Reputation: 3478

iOS CorePlot: y-axis rendered in the wrong location

I am trying to make an XY plot with y-axis in a Core Plot but I've run into the issue.

Here is a minimal example to reproduce it:

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
  }
} 

Unfortunately I don't see the y-axis:

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}>);

Looks like the axis are rendered in the wrong place. If I change xMax value to 0.0, the y-axis becomes visible:

axis-visible

Thank you in advance!

Upvotes: 0

Views: 88

Answers (1)

Eric Skroch
Eric Skroch

Reputation: 27381

The default axis positions make them cross at (0, 0). If you want something different, either change the orthogonalPosition of the y-axis or use axisConstraints to hold the axis in a particular position on screen, letting the graph scroll underneath.

Upvotes: 1

Related Questions