Why won't my x-axis show with core-plot on the iPhone?
Asked Answered
C

4

5

EDIT: I think my question is better phrased as: How can I have a Y-axis that doesn't start at zero? It seems like the x-axis always gets placed at the y=0, but I would like the x-axis to be at some positive number on the y-axis.

Here's a graph with more regular data... I just wish the x-axis was placed at the minimum y-value for the plot (about 77), instead of at 0.

alt text

Here is the function I use to create the graph.

It's a whole bunch of code, and I'm not quite sure which piece might be off to not display the x-axis.

Could the x-axis not showing have something to do with my data?

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

Chromophore answered 13/1, 2010 at 5:51 Comment(0)
T
5

You set the position of an axis on the orthogonal axis using the constantCoordinateValue property of the axis. Just set that property on the x axis to a positive value of y, and the axis should move up.

Temporize answered 13/1, 2010 at 7:49 Comment(0)
O
6

I've had the same problem yesterday/today and the solution seems to be using the orthogonalCoordinateDecimal property on the axis. For example:

graph.axisSet.xAxis.orthogonalCoordinateDecimal = CPDecimalFromFloat(minHeight);
Octoroon answered 6/7, 2010 at 14:19 Comment(0)
T
5

You set the position of an axis on the orthogonal axis using the constantCoordinateValue property of the axis. Just set that property on the x axis to a positive value of y, and the axis should move up.

Temporize answered 13/1, 2010 at 7:49 Comment(0)
U
3

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

This states that the isFloatingAxis property has been removed from at least version 0.9.

I also found this in the discussion group as well http://code.google.com/p/core-plot/issues/detail?id=125

After some hunting, it looks like the new way to float an axis looks like this:

x.axisConstraints = [CPTConstraints constraintWithUpperOffset:132];
Ushaushant answered 4/1, 2012 at 21:51 Comment(0)
T
1

this framework really needs a better way to configure itself. I don't see any problem jumping out. Maybe just try simplifying the method to the point where debugging becomes manageable. Check the data that you are passing in to make sure it is what you expect it to be. Better yet, create some dummy data that you know will produce the plot you expect.

Tillio answered 13/1, 2010 at 5:58 Comment(7)
The data is all fine. The graph shows, the y-axis shows, the y-axis labels show, the plot shows... just the x-axis doesn't show, nor its labels. The only problem with the data is the y-values might sometimes be below the minimum y-value. Would that mess things up?Chromophore
sorry, not too familiar with this framework. But, what about this method you have: plotRangeWithLocation:CPDecimalFromFloat(minDistance) length:CPDecimalFromFloat(maxDistance)]; Is that the correct value for length? For eg. with alt you have maxAlt being the difference between your datasources min and max, but for distance you have it between 0 and datasource max.Tillio
Also, as a general observation, it looks like the whole graph may be there but the x axis is being cut off because of some low y values. Can you create a more simple dummy set of of data (perhaps where all y are the same to produce a horizontal line) to see if the x axis shows up then?Tillio
It does seem to be the data. I can see the axis when I use a track that doesn't have any points with 0 altitude in it.Chromophore
I think my question is better phrased as: How can I have a Y-axis that doesn't start at zero? It seems like the x-axis always gets placed at the y=0, but I would like the x-axis to be at some positive number on the y-axis.Chromophore
What about creating a simple filter function that would constrain your values to some given range? e.g. pick some minimum value for y and if a sample falls below that, it would be set to that value?Tillio
Looking through the corePlot sourcecode, what about ramping up the labelOffset, or changing the labelingOrigin?Tillio

© 2022 - 2024 — McMap. All rights reserved.