2014-12-31 96 views
2

我花了超過一週的時間嘗試爲Core Plot創建自定義軸標籤,但沒有成功。我閱讀了許多關於SO的教程和例子,並試圖在我的項目中複製它,但沒有成功。請幫忙。Core Plot的自定義軸標籤

這是我的代碼:

// Create an CPXYGraph and host it inside the view 
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme]; 
CPTXYGraph *graph = (CPTXYGraph *)[theme newGraph]; 
hostView.hostedGraph = graph; 

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

[graph.plotAreaFrame setPaddingTop:10.0f]; 
[graph.plotAreaFrame setPaddingLeft:55.0f]; 
[graph.plotAreaFrame setPaddingRight:10.0f]; 
[graph.plotAreaFrame setPaddingBottom:35.0f]; 

// Set up Plot Space 
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
plotSpace.allowsUserInteraction = YES; 
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(800) 
               length:CPTDecimalFromInteger(400)]; 
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(7000) 
               length:CPTDecimalFromCGFloat(1000)]; 

// Set up Axes 
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 

CPTXYAxis *x = axisSet.xAxis; 
x.majorIntervalLength = CPTDecimalFromInteger(60); 
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(7000); 
x.minorTicksPerInterval = 3; 
x.borderWidth = 0; 
x.majorTickLength = 5.0f; 
x.minorTickLength = 2.0f; 
x.axisLineCapMax = [CPTLineCap openArrowPlotLineCap]; 
x.axisLineCapMax.size = CGSizeMake(8, 10); 

x.labelingPolicy = CPTAxisLabelingPolicyAutomatic; 

NSMutableSet *xLabels = [NSMutableSet setWithCapacity:[self.arrXValues count]]; 
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:[self.arrXValues count]]; 

CGFloat location = 0.0; 
for (NSString *string in self.arrXValues) { 
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:string textStyle:x.labelTextStyle]; 
    location++; 
    label.tickLocation = CPTDecimalFromCGFloat(location); 
    label.offset = x.titleOffset + x.majorTickLength; 
    if (label) { 
     [xLabels addObject:label]; 
     [xLocations addObject:[NSNumber numberWithFloat:location]]; 
    } 
} 
x.axisLabels = xLabels; 
x.majorTickLocations = xLocations; 

CPTXYAxis *y = axisSet.yAxis; 
y.majorIntervalLength = CPTDecimalFromCGFloat(250); 
y.minorTicksPerInterval = 5; 
y.orthogonalCoordinateDecimal = CPTDecimalFromInteger(800); 
y.majorTickLength = 5.0f; 
y.minorTickLength = 2.0f; 
y.axisLineCapMax = [CPTLineCap openArrowPlotLineCap]; 
y.axisLineCapMax.size = CGSizeMake(8, 10); 

// Set up the chart 
CPTScatterPlot *linePlot = [[CPTScatterPlot alloc] init]; 
linePlot.identifier = @"AAPL"; 
CPTMutableLineStyle *lineStyle = [linePlot.dataLineStyle mutableCopy]; 
lineStyle.lineWidth = 2.f; 
lineStyle.lineColor = [CPTColor blueColor]; 
linePlot.dataLineStyle = lineStyle; 

linePlot.dataSource = self; 
[graph addPlot:linePlot]; 

這是我的數據結構:

self.arrXValues = [NSMutableArray arrayWithObjects:@"900", @"930", @"1000", @"1010", @"1020", @"1030", @"1040", @"1050", @"1100", @"1130", nil]; 
self.arrYValues = [NSMutableArray arrayWithObjects:@"7600", @"7460", @"7700", @"7720", @"7680", @"7700", @"7720", @"7720", @"7680", @"7700", nil]; 

如果我把labelingPolicy到CPTAxisLabelingPolicyAutomatic,它會顯示默認標籤,但至少申明標籤不被別的東西隱藏。不幸的是,我沒有足夠的聲望發佈圖片。

請幫助發現錯誤,那裏肯定有一些愚蠢的東西。

回答

3

您將自定義標籤放置在可見繪圖區域之外。 xRange涵蓋800和1200之間的值(400的length)。標籤爲1,2,3等。使用標籤值作爲tickLocation

label.tickLocation = CPTDecimalFromString(string); 
[xLocations addObject:string]; 
+0

是的,就是這樣。謝謝,埃裏克。你是個天才。 – HSH

+0

@hsh @Eric它不適用於我x軸標籤顯示來自xLocations的值而不是來自xLabel標題。 –

+0

@MuhammadYawarAli這個問題已經過了一年多了。請提出一個新的問題,詳細說明您的情況,我們可以提供幫助。一定要在問題上使用'core-plot'標籤。 –