0
votes

The axis lines and tick marks for my graph don't appear when I set hostingView.collapsesLayers = NO. When I set hostingView.collapsesLayers = YES, they do appear. Any idea what might be causing this situation?

I've tried changing up the chart layer order, but no order (that I've tried) seems to make the axis lines appear. The issue also doesn't seem to be related to the background color of the hosting view or fill color of the graph, which I've tried setting to be clear for both.

The axis labels are appearing and in the correct locations.

EDIT: Grid lines and annotations are also all appearing. Everything is appearing as it should except the axis lines and tick marks.

EDIT2: Graph setup code, called in viewDidAppear

//create graph and hosting view
_graph = [[CPTXYGraph alloc] initWithFrame:_graphHostingView.frame];
_graphHostingView.hostedGraph = _graph;
_graphHostingView.collapsesLayers = NO;

//format graph
CGFloat graphPadding = 0.0f;
_graph.paddingBottom = graphPadding;
_graph.paddingLeft = graphPadding;
_graph.paddingRight = graphPadding;
_graph.paddingTop = graphPadding;
_graph.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
CGFloat plotPadding = 20.0f;
_graph.plotAreaFrame.paddingBottom = plotPadding;
_graph.plotAreaFrame.paddingLeft = plotPadding;
_graph.plotAreaFrame.paddingRight = plotPadding;
_graph.plotAreaFrame.paddingTop = plotPadding;

//configure plot spaces
_rightAxisPlotSpace = [[CPTXYPlotSpace alloc] init];
[_graph addPlotSpace:_rightAxisPlotSpace];

//init axes
_xAxis = [[CPTXYAxis alloc] init];
_xAxis.coordinate = CPTCoordinateX;
_xAxis.plotSpace = _graph.defaultPlotSpace;
_yAxisLeft = [[CPTXYAxis alloc] init];
_yAxisLeft.coordinate = CPTCoordinateY;
_yAxisLeft.plotSpace = _graph.defaultPlotSpace;
_yAxisRight = [[CPTXYAxis alloc] init];
_yAxisRight.coordinate = CPTCoordinateY;
_yAxisRight.plotSpace = _rightAxisPlotSpace;
_xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0f];
_yAxisLeft.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0f];
_yAxisRight.axisConstraints = [CPTConstraints constraintWithUpperOffset:0.0f];
_xAxis.tickDirection = CPTSignNegative;
_yAxisLeft.tickDirection = CPTSignNegative;
_yAxisRight.tickDirection = CPTSignPositive;

CPTAxisSet* axisSet = [[CPTAxisSet alloc] init];
axisSet.axes = [NSArray arrayWithObjects:_xAxis, _yAxisLeft, _yAxisRight, nil];
_graph.axisSet = axisSet;

//format axes
CPTMutableLineStyle* axisStyle = [CPTMutableLineStyle lineStyle];
axisStyle.lineWidth = 0.75f;
axisStyle.lineColor = [CPTColor colorWithCGColor:DARK_GRAY.CGColor];
CPTMutableLineStyle* axisTickStyle = [CPTMutableLineStyle lineStyle];
axisTickStyle.lineWidth = 0.5f;
axisTickStyle.lineColor = axisStyle.lineColor;
CPTMutableLineStyle* gridStyle = [CPTMutableLineStyle lineStyle];
gridStyle.lineWidth = 0.25f;
gridStyle.lineColor = [CPTColor colorWithCGColor:LIGHT_GRAY.CGColor];
CPTMutableTextStyle* labelTextStyle = [CPTMutableTextStyle textStyle];
labelTextStyle.fontName = FONT_REGULAR;
labelTextStyle.fontSize = 8.0f;
labelTextStyle.color = [CPTColor colorWithCGColor:DARK_GRAY.CGColor];
CPTMutableTextStyle* leftAxisTitleStyle = [CPTMutableTextStyle textStyle];
leftAxisTitleStyle.fontName = FONT_HEAVY;
leftAxisTitleStyle.fontSize = 10.0f;
leftAxisTitleStyle.color = [CPTColor colorWithCGColor:BLUE.CGColor];
CPTMutableTextStyle* rightAxisTitleStyle = [CPTMutableTextStyle textStyle];
rightAxisTitleStyle.fontName = FONT_HEAVY;
rightAxisTitleStyle.fontSize = 10.0f;
rightAxisTitleStyle.color = [CPTColor colorWithCGColor:YELLOW.CGColor];
CPTTimeFormatter* calendarFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:_dateFormatter];
_xAxis.majorGridLineStyle = gridStyle;
_xAxis.labelFormatter = calendarFormatter;

NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMinimumFractionDigits:0];
[numberFormatter setMaximumFractionDigits:2];
_yAxisLeft.majorGridLineStyle = gridStyle;
_yAxisLeft.minorGridLineStyle = gridStyle;
_yAxisLeft.titleTextStyle = leftAxisTitleStyle;
_yAxisLeft.titleOffset = 20.0f;
_yAxisLeft.labelFormatter = numberFormatter;

_yAxisRight.majorGridLineStyle = nil;
_yAxisRight.minorGridLineStyle = nil;
_yAxisRight.titleTextStyle = rightAxisTitleStyle;
_yAxisRight.titleOffset = 20.0f;
_yAxisRight.titleRotation = 3.0f*M_PI/2.0f;
_yAxisRight.labelFormatter = numberFormatter;
for(CPTXYAxis* axis in _graph.axisSet.axes)
{
    axis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    axis.preferredNumberOfMajorTicks = 7;
    axis.minorTicksPerInterval = 0;
    axis.majorTickLineStyle = axisTickStyle;
    axis.minorTickLineStyle = axisTickStyle;
    axis.axisLineStyle = axisStyle;
    axis.labelTextStyle = labelTextStyle;
    axis.majorTickLength = 3.0f;
    axis.minorTickLength = 1.5f;
    axis.labelOffset = 0.0f;
}

//set chart layer order
NSArray* chartLayers = [NSArray arrayWithObjects:[NSNumber numberWithInteger:CPTGraphLayerTypeAxisLines],
                        [NSNumber numberWithInteger:CPTGraphLayerTypeAxisLabels],
                        [NSNumber numberWithInteger:CPTGraphLayerTypePlots],
                        [NSNumber numberWithInteger:CPTGraphLayerTypeMajorGridLines],
                        [NSNumber numberWithInteger:CPTGraphLayerTypeMinorGridLines],
                        [NSNumber numberWithInteger:CPTGraphLayerTypeAxisTitles], nil];
_graph.topDownLayerOrder = chartLayers;
1
Please post your graph setup code. - Eric Skroch
edited to include graph setup code @EricSkroch - mistershoe

1 Answers

0
votes

Instead of replacing the default axis set, just set your new axes array to it:

_graph.axisSet.axes = @[_xAxis, _yAxisLeft, _yAxisRight];

You are replacing a CPTXYAxisSet with a CPTAxisSet that doesn't know how to lay out and draw the x-y axes.