0

我正在尝试绘制我的折线图。但我无法绘制majorgridline。当我自定义 x 轴标签时,所有主要网格线都不会显示。以及如何隐藏 y 轴标签。这是我的折线图: 在此处输入链接描述

帮我画一下: 在此处输入链接描述

  • 主要网格线
  • 自定义 xaxis 标签,并隐藏 yaxis 标签。感谢。

这是我的代码:

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

CGRect bounds = layerHostingView.bounds;

// Create the graph and assign the hosting view.
graph = [[CPTXYGraph alloc] initWithFrame:bounds];
layerHostingView.hostedGraph = graph;
[graph applyTheme:theme];

graph.plotAreaFrame.masksToBorder = NO;

// chang the chart layer orders so the axis line is on top of the bar in the chart.
NSArray *chartLayers = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:CPTGraphLayerTypePlots],
                        [NSNumber numberWithInt:CPTGraphLayerTypeMajorGridLines], 
                        [NSNumber numberWithInt:CPTGraphLayerTypeMinorGridLines],  
                        [NSNumber numberWithInt:CPTGraphLayerTypeAxisLines], 
                        [NSNumber numberWithInt:CPTGraphLayerTypeAxisLabels], 
                        [NSNumber numberWithInt:CPTGraphLayerTypeAxisTitles], 
                        nil];
graph.topDownLayerOrder = chartLayers;    
[chartLayers release];


// Add plot space for horizontal bar charts
graph.paddingLeft = 50.0;
graph.paddingTop = 25.0;
graph.paddingRight = 50.0;
graph.paddingBottom = 60.0;


// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(7)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(1000)];

// Setup grid line style
CPTMutableLineStyle *majorXGridLineStyle = [CPTMutableLineStyle lineStyle];
majorXGridLineStyle.lineWidth = 1.0f;
majorXGridLineStyle.dashPattern =  CPTLinearBlendingMode;
majorXGridLineStyle.lineColor = [CPTColor colorWithComponentRed:64/255.0 green:177/255.0 blue:219/255.0 alpha:1.0];


// Setup x-Axis.
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorGridLineStyle = majorXGridLineStyle;
x.preferredNumberOfMajorTicks = 7;
x.majorIntervalLength = CPTDecimalFromString(@"1");
x.minorTicksPerInterval = 1;
x.labelRotation =  M_PI/4;
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(-7.0f)];
x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(-7.0f)];
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSMutableArray *labels = [[NSMutableArray alloc] initWithCapacity:[xAxisWeek count]];
int idx = 0;
for (NSString *week in xAxisWeek)
{
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:week textStyle:x.labelTextStyle];
    label.tickLocation = CPTDecimalFromInt(idx);
    label.offset = 5.0f;
    label.rotation = M_PI/4;
    [labels addObject:label];
    [label release];
    idx--; 
}
x.axisLabels = [NSSet setWithArray:labels];
[labels release];


// Setup y-Axis.
CPTMutableLineStyle *majorYGridLineStyle = [CPTMutableLineStyle lineStyle];
majorYGridLineStyle.lineWidth = 1.0f;
majorYGridLineStyle.dashPattern =  CPTLinearBlendingMode;
majorYGridLineStyle.lineColor = [CPTColor colorWithComponentRed:87/255.0 green:142/255.0 blue:242/255.0 alpha:1.0];

CPTXYAxis *y = axisSet.yAxis;
y.majorGridLineStyle = majorYGridLineStyle;
y.majorIntervalLength = CPTDecimalFromString(@"200");
y.minorTicksPerInterval = 1;
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(1000.0f)];
y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(-1000.0f)];
4

1 回答 1

0

不要为任一轴设置visibleRangegridLinesRange。默认值将使轴和网格线延伸到绘图区域,这就是第二张图片中显示的内容。

如果您确实需要使用这些属性(例如,让绘图符号挂在网格之外),请将两者设置为相同的范围。

于 2012-11-05T04:36:02.400 回答