2012-04-04 2 views
1

2 막대 그래프 (예 : A 및 B)가있는 차트가 있습니다.다중 막대 그래프 범례

매 시간마다 2 개의 막대 그래프가 표시됩니다.

내가 범례를 설정할 때마다 시간 및 바에 대한 견본과 텍스트가 표시되고 음모가 표시됩니다.

예 : 내 차트에서 다음 내 전설이

AAAA처럼 될 것 4 시간이

BBBB

그리고 난 단지 때문에 매 시간에

AB에게

을 원하는 , 막대는 동일을 의미합니다. 어떻게하면됩니까 ??

난이 일어날 수 있도록 모든 것을하지만 지금까지 어떤 성공을 시도

....

우는 소리는 내

// CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart]; 
CPTLegend *theLegend = [CPTLegend legendWithPlots:[NSArray arrayWithObjects:[barChart plotAtIndex:0],[barChart plotAtIndex:1], nil]]; 
theLegend.numberOfRows = 1; 
theLegend.numberOfColumns = 2;//[horas count] +1/2; 
//theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]]; 

//theLegend.borderLineStyle = barLineStyle; 
theLegend.cornerRadius = 10.0; 
theLegend.swatchSize  = CGSizeMake(15, 15); 
//whiteTextStyle.fontSize  = 16.0; 
//theLegend.textStyle   = whiteTextStyle; 
theLegend.rowMargin  = 10.0; 
theLegend.paddingLeft  = 12.0; 
theLegend.paddingTop  = 12.0; 
theLegend.paddingRight = 12.0; 
theLegend.paddingBottom = 12.0; 

//theLegend.equalColumns = YES; 
//theLegend.equalRows = YES; 
theLegend.delegate = self; 

barChart.legend = theLegend; 

- (있는 NSString *) legendTitleForBarPlot : (CPTBarPlot *) barPlot recordIndex :(NSUInteger) 인덱스 {

if ([barPlot.identifier isEqual:@"Embarque"]) { 
    if (index == 0) 
    { 
     return @"Embarque"; 
    }else { 
     return @""; 
    } 
}else { 
    if (index == 0) 
    { 
     return @"Desembarque"; 
    }else { 
     return @""; 
    } 
} 

} - (BOOL)의 ​​전설 : (CPTLegend *) 전설의 shouldDrawSwatchAtIndex : (NSUInteger) 인덱스 forPlot : (CPTPlot *) 플롯 inRect : (CGRect) RECT inContext : 각 줄에 대해 별도의 라벨을 원하지 않는다면 (CGContextRef) 컨텍스트 {

if (index == 0) { 
    return YES; 
}else{ 
    return NO; 
}  

}

답변

1

-legendTitleForBarPlot:recordIndex: 방법을 구현하지 마십시오. title 속성을 사용하여 플롯에 단일 범례 제목을 설정합니다.

-legend:shouldDrawSwatchAtIndex:forPlot:inRect:inContext: 방법은 기본 견본 도면을 어떤 방식으로 변경하려는 경우에만 필요합니다.

+0

감사 에릭, 나는 -legendTitleForBarPlot 댓글을 달았습니다. 두 플롯의 title 속성을 설정했습니다. 하지만 여전히 이전과 같은 일이 일어나고 있습니다. numberOfRows를 1로, numberOfCollouns를 2로 변경하려고 시도했지만 범례가 겹칩니다. 는 http://3.bp.blogspot.com/-QQa9LKUaJHY/T32WTHFcfOI/AAAAAAAAAGI/xpruAySamrQ/s1600/Captura+de+Tela+2012-04-05+às+ 노호 인쇄 화면에서 봐 주시기 바랍니다 09.50.31+ (2) .png –

+0

데이터 소스에'-barFillForBarPlot : recordIndex :'메소드를 구현 했습니까? 그것은 또한 전설적인 행동을 유발할 것입니다. 스크린 샷에서 필요없는 것처럼 보입니다. –

+0

예, 그랬습니다. 다른 답변에 대한 전체 코드를 게시했습니다. 미리 감사드립니다. –

0

- (무효) exibirGraficoEmbarqueDescarga {

// Create barChart from theme 
barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; 
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme]; 
[barChart applyTheme:theme]; 
//CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view; 
//hostingView.hostedGraph = barChart; 


barChart.delegate = self; 

if (chartView == nil) 
{ 
    chartView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(-15, 30, 350, 320)]; 
    // chartView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 35, 510, 280)]; 
    [self.view addSubview:chartView]; 

} 

chartView.hostedGraph = barChart; 

// Border 
barChart.plotAreaFrame.borderLineStyle = nil; 
barChart.plotAreaFrame.cornerRadius = 0.0f; 

// Paddings 
barChart.paddingLeft = 0.0f; 
barChart.paddingRight = 0.0f; 
barChart.paddingTop = 0.0f; 
barChart.paddingBottom = 0.0f; 

barChart.plotAreaFrame.paddingLeft = 70.0; 
barChart.plotAreaFrame.paddingTop = 20.0; 
barChart.plotAreaFrame.paddingRight = 20.0; 
barChart.plotAreaFrame.paddingBottom = 80.0; 

// Graph title 
barChart.title = @"Embarque/Descarga"; 
CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle]; 
textStyle.color     = [CPTColor grayColor]; 
textStyle.fontSize    = 16.0f; 
textStyle.textAlignment   = CPTTextAlignmentCenter; 
barChart.titleTextStyle   = textStyle; 
barChart.titleDisplacement  = CGPointMake(1.0f, -20.0f); 
barChart.titlePlotAreaFrameAnchor = CPTRectAnchorTop; 

// Add plot space for horizontal bar charts 

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)barChart.defaultPlotSpace; 
Apoio *apoio = [[Apoio alloc] init]; 

plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat([apoio retornaMaiorDadosGrafico:qtdEmb :qtdDesc])]; 
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(16.0f)]; 

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChart.axisSet; 
CPTXYAxis *x   = axisSet.xAxis; 

CPTMutableLineStyle *marcacaoLineStyle = [CPTLineStyle lineStyle]; 
marcacaoLineStyle.lineColor = [CPTColor lightGrayColor]; 
marcacaoLineStyle.lineWidth = 1; 


x.axisLineStyle    = marcacaoLineStyle; 
x.majorTickLineStyle   = marcacaoLineStyle; 
x.minorTickLineStyle   = marcacaoLineStyle; 
x.majorIntervalLength   = CPTDecimalFromString(@"5"); 
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); 
//x.title       = @"Horas"; 
//x.titleLocation     = CPTDecimalFromFloat(15.0f); 
//x.titleOffset    = 55.0f; 

// Define some custom labels for the data elements 
x.labelingPolicy = CPTAxisLabelingPolicyNone; 
NSArray *customTickLocations = posicao;//[NSArray arrayWithObjects:[NSDecimalNumber numberWithFloat:0.7], [NSDecimalNumber numberWithFloat:2.7], [NSDecimalNumber numberWithFloat:4.7], nil]; 
NSArray *xAxisLabels   = horas;//[NSArray arrayWithObjects:@"10", @"11", @"12", 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; 
    [customLabels addObject:newLabel]; 
} 


x.axisLabels = [NSSet setWithArray:customLabels]; 


CPTXYAxis *y = axisSet.yAxis; 
y.axisLineStyle    = marcacaoLineStyle; 
y.majorTickLineStyle   = marcacaoLineStyle; 
y.minorTickLineStyle   = marcacaoLineStyle; 
y.majorIntervalLength   = CPTDecimalFromInt([apoio retornaMaiorDadosGrafico:qtdEmb :qtdDesc]/7); //CPTDecimalFromString(@"5"); 
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); 
y.title = @"Containers"; 
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
[formatter setMaximumFractionDigits:0]; 
y.labelFormatter = formatter; 

CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle]; 
majorGridLineStyle.lineWidth = 0.75; 
majorGridLineStyle.lineColor = [CPTColor lightGrayColor ]; 

y.majorGridLineStyle = majorGridLineStyle ; 
y.preferredNumberOfMajorTicks = 10; 

CPTMutableTextStyle *EixostextStyle = [CPTTextStyle textStyle]; 
EixostextStyle.fontSize    = 12.0f; 

y.labelTextStyle = EixostextStyle; 
x.labelTextStyle = EixostextStyle; 

// Embaque 
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO]; 
barPlot.baseValue = CPTDecimalFromString(@"0"); 
barPlot.dataSource = self; 
barPlot.delegate = self; 
barPlot.barOffset = CPTDecimalFromFloat(0.25f); 
barPlot.identifier = @"Embarque"; 
barPlot.title = @"Embarque"; 
barPlot.labelOffset = 0; 
[barChart addPlot:barPlot toPlotSpace:plotSpace]; 

//Descarga 
CPTBarPlot *barPlot2 = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO]; 
barPlot2.baseValue = CPTDecimalFromString(@"0"); 
barPlot2.dataSource = self; 
barPlot2.delegate = self; 
//barPlot2.barOffset = CPTDecimalFromFloat(25f); 
barPlot2.identifier  = @"Descarga"; 
barPlot2.title = @"Descarga"; 
barPlot2.labelOffset = 0; 
[barChart addPlot:barPlot2 toPlotSpace:plotSpace]; 

CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart]; 
theLegend.numberOfRows = 1; 
theLegend.numberOfColumns = 2; 
theLegend.cornerRadius = 10.0; 
theLegend.swatchSize  = CGSizeMake(15, 15); 
//whiteTextStyle.fontSize  = 16.0; 
//theLegend.textStyle   = whiteTextStyle; 
theLegend.rowMargin  = 10.0; 
theLegend.paddingLeft  = 12.0; 
theLegend.paddingTop  = 12.0; 
theLegend.paddingRight = 12.0; 
theLegend.paddingBottom = 12.0; 

//theLegend.equalColumns = YES; 
//theLegend.equalRows = YES; 
barChart.legend = theLegend; 

}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{ 
return [horas count];} 


-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 

{ NSDecimalNumber * NUM = 닐;

if ([plot isKindOfClass:[CPTBarPlot class]]) { 
    switch (fieldEnum) { 
     case CPTBarPlotFieldBarLocation:     
      if ([plot.identifier isEqual:@"Embarque"]) { 
       num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index*2]; 
      } else{ 
       num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:(index*2+1)]; 
      } 
      break;     
     case CPTBarPlotFieldBarTip: 
      if ([plot.identifier isEqual:@"Embarque"]) { 
       num = [qtdEmb objectAtIndex:index]; 
      } else{ 
       num = [qtdDesc objectAtIndex:index]; 
      }     
      break;      
    } 
} 

return num; 

}

-(CPTFill *)barFillForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index 

{ 경우 ([barPlot.identifier ISEQUAL @ "Embarque"]) { 창 [CPTFill fillWithColor [CPTColor colorWithComponentRed (175/255.0) 녹색 :(238/255.0) 파랑 : (238/255.0) 알파 : 1.0]]; recordIndex : : 방법 및 -legend : shouldDrawSwatchAtIndex : forPlot : inRect : inContext : 방법 }

if ([barPlot.identifier isEqual:@"Descarga"]) { 
    return [CPTFill fillWithColor:[CPTColor colorWithComponentRed:(255./255.0) green:(228.0/255.0) blue:(181.0/255.0) alpha:1.0]]; 
}  

}

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index 
{ 
int valor; 
if ([plot.identifier isEqual:@"Embarque"]) 
{ 
    valor = [qtdEmb objectAtIndex:index]; 
}else{ 
    valor = [qtdDesc objectAtIndex:index]; 
} 

CPTTextLayer *newLayer = [[CPTTextLayer alloc] initWithText:[[NSString alloc] initWithFormat:@"%@",valor]]; 
CPTMutableTextStyle *estiloTexto = [[CPTMutableTextStyle alloc] init]; 
estiloTexto.color = [CPTColor blackColor]; 
estiloTexto.fontSize = 10;  

newLayer.textStyle = estiloTexto; 
return newLayer; 

}