2012-07-22 106 views
0

在下面的代碼中,我嘗試添加5個自定義標籤並將其顯示在x軸上。但我面臨以下問題。我使用散點圖示例代碼來測試它。附上結果以及結果。CorePlot x軸定位問題

  1. 並非所有5個標籤都顯示。
  2. X軸標記從負側開始。所有x應該位於正面。
  3. 在x軸上我想要 獲得與y軸相同的效果。同樣的灰色效果。

enter image description here

// Create graph from theme 
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; 
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; 
[graph applyTheme:theme]; 
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view; 
hostingView.collapsesLayers = NO; // Setting to YES reduces GPU memory usage, but can slow drawing/scrolling 
hostingView.hostedGraph  = graph; 

graph.paddingLeft = 10.0; 
graph.paddingTop = 10.0; 
graph.paddingRight = 10.0; 
graph.paddingBottom = 10.0; 

// Setup plot space 
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
plotSpace.allowsUserInteraction = YES; 
plotSpace.yRange    = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-80.0) length:CPTDecimalFromFloat(500.0)]; 
plotSpace.xRange    = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-1) length:CPTDecimalFromFloat(15.0)]; 

CPTMutableLineStyle *xLineStyle = [CPTMutableLineStyle lineStyle]; 
xLineStyle.miterLimit  = 1.0f; 
xLineStyle.lineWidth   = 3.0f; 
xLineStyle.lineColor = [CPTColor whiteColor]; 
// Axes 
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 
CPTXYAxis *x   = axisSet.xAxis; 
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(100)]; 
x.axisLineStyle    = xLineStyle; 
x.majorTickLineStyle   = xLineStyle; 
x.minorTickLineStyle   = xLineStyle; 
x.majorIntervalLength   = CPTDecimalFromString(@"1"); 
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1"); 
x.titleLocation    = CPTDecimalFromFloat(0.5f); 
x.titleOffset     = 5.0f; 

x.labelingPolicy = CPTAxisLabelingPolicyNone; 
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:2], [NSDecimalNumber numberWithInt:3], [NSDecimalNumber numberWithInt:4], nil]; 
NSArray *xAxisLabels   = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil]; 
NSUInteger labelLocation  = 0; 
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]]; 
for (NSNumber *tickLocation in customTickLocations) { 
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle]; 
    newLabel.tickLocation = [tickLocation decimalValue]; 
    newLabel.offset  = x.labelOffset + x.majorTickLength; 
    newLabel.rotation  = M_PI/4; 
    [customLabels addObject:newLabel]; 
    [newLabel release]; 
} 

x.axisLabels = [NSSet setWithArray:customLabels]; 

CPTXYAxis *y = axisSet.yAxis; 
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(500)]; 
y.majorIntervalLength   = CPTDecimalFromString(@"40"); 
y.minorTicksPerInterval  = 5; 
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2"); 

y.delegate    = self; 

// Create a blue plot area 
CPTScatterPlot *boundLinePlot = [[[CPTScatterPlot alloc] init] autorelease]; 
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
lineStyle.miterLimit  = 1.0f; 
lineStyle.lineWidth   = 3.0f; 
lineStyle.lineColor   = [CPTColor blueColor]; 
boundLinePlot.dataLineStyle = lineStyle; 
boundLinePlot.identifier = @"Blue Plot"; 
boundLinePlot.dataSource = self; 
[graph addPlot:boundLinePlot]; 

// Do a blue gradient 
CPTColor *areaColor1  = [CPTColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8]; 
CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]]; 
areaGradient1.angle = -90.0f; 
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient1]; 
boundLinePlot.areaFill  = areaGradientFill; 
boundLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue]; 

// Add plot symbols 
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle]; 
symbolLineStyle.lineColor = [CPTColor blackColor]; 
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
plotSymbol.fill   = [CPTFill fillWithColor:[CPTColor blueColor]]; 
plotSymbol.lineStyle  = symbolLineStyle; 
plotSymbol.size   = CGSizeMake(10.0, 10.0); 
boundLinePlot.plotSymbol = plotSymbol; 

// Add some initial data 
NSMutableArray *contentArray = [[NSMutableArray alloc] init]; 
NSUInteger i; 
for (i = 0; i < 5; i++) { 

    id x = [NSNumber numberWithFloat:i]; 
    id y = [NSNumber numberWithFloat:200-i]; 
    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]]; 
} 
self.dataForPlot = contentArray; 
[contentArray release]; 

回答

1
  1. 你只創建了四個標籤。 customTickLocations數組包含四個位置;爲每個位置創建一個標籤。

  2. 標籤位於代碼中指定的位置。數據點位於(0,1,2,3,4),而標籤位於(1,2,3,4)。由於orthogonalCoordinateDecimal屬性設置的原因,y軸在x = 2處穿過x軸。

  3. 如果要保留主題中的默認值,請不要在x軸上設置軸線或刻度線樣式。

+0

太好了。謝謝你的幫助。 – Dilshan 2012-07-23 07:54:32

+0

一個簡單的問題。有什麼方法可以使上述點可點擊並顯示工具提示或推送到導航控制器? – Dilshan 2012-07-23 07:57:40

+1

添加實現'-scatterPlot:plotSymbolWasSelectedAtRecordIndex:'或'-scatterPlot:plotSymbolWasSelectedAtRecordIndex:withEvent:'方法的圖表委託。 – 2012-07-23 23:35:43