2010-08-18 8 views
1

Je suis nouveau avec Core-plot. Je voudrais ajouter une étiquette à propos du nombre sur chaque colonne de CPBarPlot. Je savais que je devais mettre en œuvre cette méthode de délégué CPBarPlotDataSource:L'étiquette du traceur n'apparaît pas au-dessus de chaque colonne

-(CPTextLayer *)barLabelForBarPlot:(CPBarPlot *)barPlot recordIndex: 
    (NSUInteger)index; 

Mais je mis en œuvre cette méthode, rien ne se passe, l'affichage graphique sans étiquette sur le dessus de chaque barre. Est-ce que je manque quelque chose?

Ceci est mon code:

- (void) constructBarChart { 
//create bar chart from theme 
barChart = [[CPXYGraph alloc] initWithFrame:CGRectZero]; 
CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme]; 
[barChart applyTheme:theme]; 
hostView.hostedLayer = barChart; 
barChart.plotAreaFrame.masksToBorder = NO; 

barChart.paddingTop = 30.0; 
barChart.paddingBottom = 30.0; 
barChart.paddingLeft = 30.0; 
barChart.paddingRight = 30.0; 


// setup plot space 
CPXYPlotSpace *plotSpace = (CPXYPlotSpace*)barChart.defaultPlotSpace; 
plotSpace.allowsUserInteraction = YES; 
plotSpace.delegate = self; 
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(10.0f)]; 
    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(10.0f)]; 
plotSpace.globalXRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(20.0f)]; 
plotSpace.globalYRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(20.0f)]; 


// Grid line styles 
    CPLineStyle *majorGridLineStyle = [CPLineStyle lineStyle]; 
    majorGridLineStyle.lineWidth = 0.75; 
    majorGridLineStyle.lineColor = [[CPColor colorWithGenericGray:0.2] 
     colorWithAlphaComponent:0.75]; 

    CPLineStyle *minorGridLineStyle = [CPLineStyle lineStyle]; 
    minorGridLineStyle.lineWidth = 0.25; 
    minorGridLineStyle.lineColor = [[CPColor whiteColor] 
     colorWithAlphaComponent:0.1]; 

    CPLineStyle *redLineStyle = [CPLineStyle lineStyle]; 
    redLineStyle.lineWidth = 10.0; 
    redLineStyle.lineColor = [[CPColor redColor] 
     colorWithAlphaComponent:0.5]; 


//Axis 
CPXYAxisSet *axisSet = (CPXYAxisSet*)barChart.axisSet; 
CPXYAxis *x = axisSet.xAxis; 
CPLineStyle *lineStyle = [CPLineStyle lineStyle]; 
lineStyle.lineColor = [CPColor blackColor]; 
x.axisLineStyle = lineStyle; 
x.majorIntervalLength = CPDecimalFromString(@"5"); 
x.minorTicksPerInterval = 4; 


CPXYAxis *y = axisSet.yAxis; 
y.axisLineStyle = lineStyle; 
y.majorIntervalLength = CPDecimalFromString(@"1"); 
y.majorGridLineStyle = majorGridLineStyle; 
y.isFloatingAxis = YES; 


CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor redColor] horizontalBars:NO]; 
barPlot.baseValue = CPDecimalFromString(@"0"); 
barPlot.dataSource = self; 
barPlot.delegate = self; 
barPlot.identifier = @"Bar Plot 1"; 
[barChart addPlot:barPlot toPlotSpace: plotSpace]; 
} 

- (NSUInteger) numberOfRecordsForPlot:(CPPlot*)plot { 
if ([plot isKindOfClass:[CPBarPlot class]]) { 
    return 16; 
} else { 
    return [dataGraph count]; 
} 
} 

- (NSNumber *)numberForPlot: (CPPlot*) plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger) index { 
NSDecimalNumber *num = nil; 
if ([plot isKindOfClass:[CPBarPlot class]]) { 
    switch (fieldEnum) { 
    case CPBarPlotFieldBarLocation: 
    num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index]; 
    break; 
    case CPBarPlotFieldBarLength: 
    num = (NSDecimalNumber *) [NSDecimalNumber numberWithUnsignedInteger:(index + 1)]; 
    break; 
    } 
} else { 
    num = [[dataGraph objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")]; 
} 
return num; 
} 

#pragma mark - 
#pragma mark Plot Space Delegate Methods 

-(CGPoint)plotSpace:(CPPlotSpace *)space willDisplaceBy:(CGPoint)proposedDisplacementVector { 
CGPoint result = CGPointMake(proposedDisplacementVector.x, 0.0); 
return result; 
} 

#pragma mark - 
#pragma mark Bar Plot Delegate Methods 

- (CPTextLayer *) barLabelForBarPlot:(CPBarPlot *) barPlot recordIndex:(NSUInteger) index { 
CPTextLayer *textLayer = [[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"%d", index+1]]; 
return textLayer; 
} 

Toute aide serait appréciée.

Merci

Répondre

5

La méthode de marquage a récemment été modifiée. C'est juste un changement de nom; ça marche comme avant. La nouvelle déclaration est la suivante:

-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index; 

De même, cette méthode doit renvoyer les objets auto-libérés pour éviter les fuites de mémoire.

+0

Non, il est encore "barLabelForBarPlot". Parce que je viens de le télécharger sur le site de CorePlot. Et je cherche la classe CPBarPlot, elle a la méthode "barLabelForBarPlot". En tout cas, merci pour votre réponse. – haisergeant

+0

merci Eric ça fonctionne parfaitement pour moi. – Pooja

+0

est-il possible graphique à barres de même couleur que l'étiquette ont avec intrigue de base, je peux afficher graphique à barres, mais ont une couleur bleue par défaut.Merci beaucoup pour la lecture.J'espère que quelque chose lié à ce que vous savez. – Pooja

0

signature de la méthode maintenant changé

- (CPTLayer *) dataLabelForPlot: (CPTPlot *) parcelle RecordIndex: index (NSUInteger)

ou

- (NSArray *) dataLabelsForPlot: (CPTPlot *) intrigue recordIndexRange: (NSRange) indexRange

1

Hope Quelqu'un trouvera cela utile, Qui ne sait pas comment afficher l'étiquette sur le dessus de la barre. Je l'ai mis en œuvre le CPTPlotDataSource comme ci-dessous,

enter image description here

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)idx{ 

    CPTMutableTextStyle *ts = [[CPTMutableTextStyle alloc] init]; 
    ts.textAlignment = CPTTextAlignmentCenter; 
    ts.color = [CPTColor redColor]; 

    CPTTextLayer *tl = [[CPTTextLayer alloc] initWithText:@"5" style:ts]; 

    return tl; 
} 
1

essayer. (Pour core plot 1.6) Méthode

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index; 
{ 
     CPTMutableTextStyle *ts = [[CPTMutableTextStyle alloc] init]; 
     ts.textAlignment = CPTTextAlignmentCenter; 
     ts.color = [CPTColor blackColor]; 
     ts.fontSize = 12; 
     plot.labelOffset = 0; 
     CPTTextLayer *tl = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@",index] style:ts]; 
     return tl; 
}