更改Coreplot图的刻度标签 [英] Change Tick Labels for Coreplot Graph

查看:72
本文介绍了更改Coreplot图的刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac应用程序&中将coreplot用于图形它的工作正常.我要求沿着轴显示custom labels,如:

I am using coreplot for graphs in my mac application & its working fine. I have a requirement to show custom labels along the axis for ticks like :

我有一个随时间变化的功率值图:Power values -> Y-Axis& Time -> X-Axis.

I have a graph for power values against time : 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个相等的部分&显示标签,一切都很好.

With above code it is drawing graph & along x-axis it is dividing time in 4 equal portions & showing labels, everything is good.

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

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

我尝试过此操作:let xLabel = CPTAxisLabel(text: "\(label/60)", textStyle: axisTextStyle()),但标签没有改变没有效果.

I tried with this : let xLabel = CPTAxisLabel(text: "\(label/60)", textStyle: axisTextStyle()) but labels are not changing no effect.

还尝试更改tickLocation,例如:xLabel.tickLocation = NSNumber(double: label/60)没有效果

Also tried to change tickLocation like : xLabel.tickLocation = NSNumber(double: label/60) No effect

随着majorTickLocations的变化,如: xLocations.append(NSNumber(double: label/60))它表现得怪异而 所有x轴标签都弄乱了.

With changing majorTickLocations like : xLocations.append(NSNumber(double: label/60)) it behaving weird and all x-axis labels are messed.

我尝试遵循以下示例: StackOverFlow发布

I tried to follow this example : StackOverFlow Post

任何人都可以引导出什么问题吗[记住我正在使用Swift]吗?

推荐答案

我自己通过以下方式解决了问题:

I solved it myself by doing it like :

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

现在需要标签了.

这篇关于更改Coreplot图的刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆