2012-12-10 42 views
1

我正在使用核心圖框架創建帶有圖例的條形圖。除了圖例是複製條形圖每個部分中的列(請參閱附加屏幕截圖)之外,它的所有工作都是如此。帶有重複圖例的核心圖條形圖

enter image description here

我已經通過核心情節類的引用看了一下,是不是能找到什麼來解決這個問題。我嘗試將CPTLegend屬性numberOfColumns設置爲1,並將numberOfRows設置爲3,並使圖例顯示圖例中適當數量的項目,但顯示的數據不正確。

enter image description hereenter image description here

下面是我使用建條形圖的代碼。你們都有任何建議我可以解決這個問題嗎?我認爲這不是一個核心劇情的bug,而是傳說的一個限制,我希望有一個解決方法。

// Create barChart from theme 
barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; 
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; 
[barChart applyTheme:theme]; 
chartView.hostedGraph = barChart; 
barChart.plotAreaFrame.masksToBorder = NO; 

barChart.paddingLeft = 60.0; 
barChart.paddingTop = 10.0; 
barChart.paddingRight = 0.0; 
barChart.paddingBottom = 30.0; 

//find max y 
int maxY = 0; 
NSMutableArray *maxDrillDownData = [chartData.drillDownData objectForKey:DRILLDOWN_EQUIPMENT_ALL_TYPE]; 
for (NSMutableArray *dataArray in maxDrillDownData) { 
    maxY = dataArray.count>maxY?dataArray.count:maxY; 
} 

//add buffer 
maxY = maxY+100; 

// Add plot space for horizontal bar charts 
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)barChart.defaultPlotSpace; 
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromInt(maxY)]; 
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(3.25)]; 

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChart.axisSet; 
CPTXYAxis *x = axisSet.xAxis; 
x.axisLineStyle = nil; 
x.majorTickLineStyle = nil; 
x.minorTickLineStyle = nil; 
x.majorIntervalLength = CPTDecimalFromString(@"1"); 
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); 
x.titleLocation = CPTDecimalFromFloat(7.5f); 
x.titleOffset = 55.0f; 
x.labelOffset = 2.0; 

// Define some custom labels for the data elements 
x.labelRotation = M_PI/4; 
x.labelingPolicy = CPTAxisLabelingPolicyNone; 
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithFloat:0.5], [NSDecimalNumber numberWithFloat:1.5], [NSDecimalNumber numberWithFloat:2.5], nil]; 
NSArray *xAxisLabels = (NSMutableArray *)[chartData.labels objectForKey:LABELS_EQUIPMENT_TYPE]; 
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; 
    [customLabels addObject:newLabel]; 
    //[newLabel release]; 
} 

x.axisLabels = [NSSet setWithArray:customLabels]; 

CPTXYAxis *y = axisSet.yAxis; 
y.axisLineStyle = nil; 
y.majorTickLineStyle = nil; 
y.minorTickLineStyle = nil; 
y.majorIntervalLength = CPTDecimalFromString(@"50"); 
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); 
y.title = (NSString *)[chartData.labels objectForKey:LABELS_Y_AXIS]; 
y.titleOffset = 45.0f; 
y.titleLocation = CPTDecimalFromFloat(150.0f); 

//identifiers 
NSMutableArray *identifiers = (NSMutableArray *)[chartData.identifiers objectForKey:IDENTIFIER_EQUIPMENT_TYPE]; 

// First bar plot 
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO]; 
barPlot.baseValue = CPTDecimalFromString(@"0"); 
barPlot.dataSource = self; 
barPlot.barOffset = CPTDecimalFromFloat(-0.6f); 
barPlot.barWidth = CPTDecimalFromFloat(.5); 
barPlot.delegate = self; 
barPlot.identifier = (NSString *)[identifiers objectAtIndex:0]; 
[barChart addPlot:barPlot toPlotSpace:plotSpace]; 

NSLog(@"barPlot.identifier: %@", barPlot.identifier); 

// Second bar plot 
barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO]; 
barPlot.dataSource = self; 
barPlot.barOffset = CPTDecimalFromFloat(-0.4f); 
barPlot.barWidth = CPTDecimalFromFloat(.5); 
barPlot.baseValue = CPTDecimalFromString(@"0"); 
barPlot.identifier = (NSString *)[identifiers objectAtIndex:1]; 
barPlot.delegate = self; 
[barChart addPlot:barPlot toPlotSpace:plotSpace]; 

NSLog(@"barPlot.identifier: %@", barPlot.identifier); 

//third bar plot 
barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO]; 
barPlot.dataSource = self; 
barPlot.barOffset = CPTDecimalFromFloat(-0.2f); 
barPlot.barWidth = CPTDecimalFromFloat(.5); 
barPlot.baseValue = CPTDecimalFromString(@"0"); 
barPlot.identifier = (NSString *)[identifiers objectAtIndex:2]; 
barPlot.delegate = self; 
[barChart addPlot:barPlot toPlotSpace:plotSpace]; 

NSLog(@"barPlot.identifier: %@", barPlot.identifier); 

// Add legend 
CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart]; 
theLegend.numberOfColumns = 3; 
theLegend.numberOfRows = 1; 
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]]; 
theLegend.borderLineStyle = [CPTLineStyle lineStyle]; 
theLegend.cornerRadius = 5.0; 

barChart.legend = theLegend; 
barChart.legendAnchor = CPTRectAnchorTopRight; 
barChart.legendDisplacement = CGPointMake(-10.0f, -20.0f); 

感謝您的幫助!

回答

5

確保既不以下方法在你的情節數據源來實現:

-legendTitleForBarPlot:recordIndex: 
-barFillForBarPlot:recordIndex: 

如果這些方法存在,該地塊將使用爲單個圖例項的每個欄的傳奇人物進入,而不是整個情節。

+0

真棒...刪除-barFillForBarPlot:recordIndex:解決了我的問題。感謝您的幫助! –

+0

@EricSkroch我必須使用'barFillForBarPlot:recordIndex'方法,我也希望單個圖例而不是多個。如果我設置'numberOfColumns = 1',它會重疊。任何解決方案 –

3

我知道這是一個非常古老的問題,但提供的答案沒有任何幫助。所以我看着它,試圖找出它。在我看來,最好的解決方案是隻添加一個情節到你的傳奇,而不是添加圖。添加圖形會將與圖形關聯的所有圖形添加到圖例中。

CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart]; 

更換

NSMutableArray *legendPlots =[[NSMutableArray alloc] init]; 
[legendPlots addObject:barPlot]; 
CPTLegend *theLegend = [CPTLegend legendWithPlots:legendPlots]; 

我在一個圖表有多個餅圖同樣的問題。這爲我解決了這個問題。

+0

很酷,是啊,考慮到這篇文章已經三年了,這是非常合理的,你在一個更新版本的CorePlot比我第一次遇到這個問題併發布答案時。感謝您更新此主題,以便它保持相關。 –