2012-11-05 125 views
0

我在繪製我的折線圖。但我不能繪製majorgridline。 當我定製x軸標籤時,所有majorgridline都不顯示。以及如何隱藏y軸標籤。 這裏是我的折線圖: enter link description here繪製majorGridLine繪圖圖 - CorePlot

幫我畫它想: enter link description here

  • MajorGridLine
  • custome x軸拉布勒,並隱藏y軸拉布勒。 謝謝。

這裏是我的代碼:

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)]; 

回答

0

不要設置visibleRangegridLinesRange的任一軸。默認值將使軸線和網格線在整個繪圖區域上延伸,這就是第二張圖像中顯示的內容。

如果您確實需要使用這些屬性(例如,讓繪圖符號懸掛在網格之外),請將兩者都設置爲相同的範圍。