Core Plot轴线未出现

时间:2014-12-03 19:19:15

标签: objective-c core-plot

设置hostingView.collapsesLayers = NO时,我的图表的轴线和刻度线不会出现。当我设置hostingView.collapsesLayers = YES时,它们会出现。知道可能导致这种情况的原因吗?

我已经尝试更改图表图层顺序,但是没有任何顺序(我尝试过)似乎会使轴线出现。该问题似乎也与托管视图的背景颜色或图表的填充颜色无关,我已尝试将其设置为两者都清晰。

轴标签出现在正确的位置。

编辑:网格线和注释也都出现了。除了轴线和刻度线之外,一切都应该出现。

EDIT2:图形设置代码,在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 个答案:

答案 0 :(得分:0)

只需将新轴数组设置为它,而不是替换默认轴组:

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

您正在用CPTXYAxisSet替换CPTAxisSet,而{{1}}不知道如何布局和绘制x-y轴。