【问题标题】:Change Tick Labels for Coreplot Graph更改 Coreplot 图形的刻度标签
【发布时间】:2016-04-14 23:25:33
【问题描述】:

我在我的 mac 应用程序中使用coreplot 来绘制图表,它工作正常。我需要在轴上显示custom labels 的刻度,例如:

我有一个功率值与时间的关系图:Power values -> Y-Axis & Time -> X-Axis

I want to draw graph for Time seconds values but show fixed intervals ticks of minutes along x-axis for which i figured that i should use custom labels but its not happening for me.

自定义标签的代码是:

  let graph = CPTXYGraph()

  plots[0].identifier = PlotType.target.rawValue
  plots[1].identifier = PlotType.user.rawValue

  for plot in plots {
     plot.dataSource = self
     plot.delegate = self
     graph.addPlot(plot)
  }

  if let plotArea = graph.plotAreaFrame {
     plotArea.borderLineStyle = nil
     plotArea.paddingLeft = 25.0
     plotArea.paddingBottom = 20.0
     plotArea.paddingRight = 10.0
     plotArea.paddingTop = 20.0
  }

  guard let axisSet = graph.axisSet else { return }

  let xAxis = axisSet.axes?[0]
  var xLabels = [CPTAxisLabel]()
  var xLocations = [NSNumber]()

  let doubleSecs = Double(activity.durationSeconds)
  let labels = [0.0, doubleSecs * 0.25, doubleSecs * 0.50, doubleSecs * 0.75, doubleSecs]
  for label in labels {
     let xLabel = CPTAxisLabel(text: "\(label/60)", textStyle: axisTextStyle)
     xLabel.tickLocation = NSNumber(double: label)
     xLocations.append(NSNumber(double: label))
     xLabels.append(xLabel)
  }
  xAxis?.labelingPolicy = CPTAxisLabelingPolicy.LocationsProvided
  xAxis?.labelFormatter = axisFormatter
  xAxis?.axisLabels = Set(xLabels)
  xAxis?.majorTickLocations = Set(xLocations)

  chartView.hostedGraph = graph

使用上面的代码,它正在绘制图形并沿 x 轴将时间分成 4 等份并显示标签,一切都很好。

I want to show labels along x-axis as minutes and remaining drawing is ok.

我试过这个:let xLabel = CPTAxisLabel(text: "\(label/60)", textStyle: axisTextStyle()) 但标签没有改变没有效果。

还尝试更改tickLocation like : xLabel.tickLocation = NSNumber(double: label/60) 无效

随着 majorTickLocations 的变化,例如: xLocations.append(NSNumber(double: label/60)) 它的行为很奇怪 所有的 x 轴标签都乱了。

我试着按照这个例子:StackOverFlow Post

谁能指导一下出了什么问题[记住我使用的是 Swift] 吗?

【问题讨论】:

    标签: ios swift swift2 core-plot


    【解决方案1】:

    .LocationsProvided 标签策略使用 labelFormatter 从刻度位置自动创建标签。如果您想使用自定义标签,请改用.None 标签政策。

    【讨论】:

    • 我试过.None然后它根本没有显示任何标签。
    • 您可能需要调整每个标签上的offset。它们可能在图表的可见部分之外。
    【解决方案2】:

    我自己解决了这个问题:

      let xAxis = axisSet.axes?[0]
      xAxis?.majorIntervalLength = activity.durationSeconds / 60 / 4
      xAxis?.labelingPolicy = CPTAxisLabelingPolicy.FixedInterval
    

    现在标签按要求来了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多