为什么我的X轴不能在iPhone上显示核心图?

时间:2010-01-13 05:51:33

标签: iphone objective-c cocoa-touch core-plot

编辑:我认为我的问题更好地表达为:我怎么能有一个不从零开始的Y轴?似乎x轴总是放在y = 0处,但我希望x轴在y轴上处于某个正数。

这是一个包含更多常规数据的图表...我只希望x轴位于图表的最小y值(约77),而不是0。

alt text

这是我用来创建图形的函数。

这是一大堆代码,我不太确定哪一块可能没有显示x轴。

x轴没有显示与我的数据有关吗?

- (void) showGraph:(SavedDetailScreen*)dataSource {

  // create the graph and add it to the view
  CPXYGraph *graph = [[CPXYGraph alloc] initWithFrame: CGRectZero];
  graph.plotArea.masksToBorder = NO;
  CPLayerHostingView *graphView = [[CPLayerHostingView alloc] initWithFrame:CGRectMake(0,0, 280, 240)];
  [self addSubview:graphView];
  graphView.hostedLayer = graph;
  graph.paddingLeft = 50.0;
  graph.paddingTop = 20.0;
  graph.paddingRight = 10.0;
  graph.paddingBottom = 40.0;              

  // set up the ranges for the graph axis
  float minElevation = dataSource.track.tbStats.minAlt;
  float maxElevation = dataSource.track.tbStats.maxAlt-dataSource.track.tbStats.minAlt;
  float minDistance = 0.0f;
  float maxDistance = dataSource.track.tbStats.totalDistance;
  CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
  plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(minDistance)
                                             length:CPDecimalFromFloat(maxDistance)];
  plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(minElevation)
                                                 length:CPDecimalFromFloat(maxElevation)];

  // style the graph with white text and lines
  CPTextStyle *whiteText = [CPTextStyle textStyle];
  whiteText.color = [CPColor whiteColor];              
  CPLineStyle *whiteStyle = [CPLineStyle lineStyle];
  whiteStyle.lineColor = [CPColor whiteColor];
  whiteStyle.lineWidth = 2.0f;

  // set up the axis
  CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;      
  CPXYAxis *x = axisSet.xAxis;
  CPXYAxis *y = axisSet.yAxis;              
  x.majorIntervalLength = CPDecimalFromFloat(maxDistance/10.0f);
  x.minorTicksPerInterval = 0;
  x.majorTickLineStyle = whiteStyle;
  x.minorTickLineStyle = whiteStyle;
  x.axisLineStyle = whiteStyle;
  x.minorTickLength = 5.0f;
  x.majorTickLength = 10.0f;
  x.labelOffset = 3.0f;
  x.labelTextStyle = whiteText;
  y.majorIntervalLength = CPDecimalFromFloat(maxElevation/5.0f);
  y.minorTicksPerInterval = 0;
  y.majorTickLineStyle = whiteStyle;
  y.minorTickLineStyle = whiteStyle;
  y.axisLineStyle = whiteStyle;
  y.minorTickLength = 5.0f;
  y.majorTickLength = 10.0f;
  y.labelOffset = 3.0f;
  y.labelTextStyle = whiteText;

  CPScatterPlot *plot = [[[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];
  plot.dataLineStyle.lineWidth = 2.0f;
  plot.dataLineStyle.lineColor = [CPColor blueColor];
  plot.dataSource = dataSource;
  [graph addPlot:plot];
  CPPlotSymbol *greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol];
  greenCirclePlotSymbol.fill = [CPFill fillWithColor:[CPColor greenColor]];
  greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
  plot.plotSymbol = greenCirclePlotSymbol;                 

}

alt text

4 个答案:

答案 0 :(得分:6)

昨天/今天我遇到了同样的问题,解决方案似乎是在轴上使用 orthogonalCoordinateDecimal 属性。例如:

graph.axisSet.xAxis.orthogonalCoordinateDecimal = CPDecimalFromFloat(minHeight);

答案 1 :(得分:5)

使用轴的constantCoordinateValue属性设置轴在正交轴上的位置。只需将x轴上的该属性设置为y的正值,轴就应该向上移动。

答案 2 :(得分:3)

https://github.com/djw/core-plot/tree/9282845bddbb8c40ff314bbfa158beff797c91f7/examples

这表明isFloatingAxis属性已从至少0.9版本中删除。

我也在讨论组中发现了这一点http://code.google.com/p/core-plot/issues/detail?id=125

经过一些狩猎后,看起来浮动轴的新方法看起来像这样:

x.axisConstraints = [CPTConstraints constraintWithUpperOffset:132];

答案 3 :(得分:1)

这个框架确实需要一种更好的配置方式。我没有看到跳出来的任何问题。也许只是尝试简化方法,使调试变得易于管理。检查您传入的数据,以确保它符合您的预期。更好的是,创建一些您知道会产生预期图形的虚拟数据。