2010-10-11 4 views
2

J'utilise le traceur principal (et les histogrammes) pour afficher des informations financières. Mon problème est que chaque fois que j'utilise des valeurs négatives, il semble que l'échelle du graphique montré est fausse. Je voudrais que le graphique entier soit visible, mais souvent seulement une partie du graphique est montrée, laissant les barres coupées et parfois même pas l'axe des x visible (la partie montrée est si bas sur l'axe des ordonnées) . Si j'active l'interaction de l'utilisateur, il est possible de faire défiler la vue pour voir différentes parties du graphique, mais je veux un aperçu, pas d'étudier le graphique en détail. Notez que je n'utilise pas tout l'écran pour le graphique, mais seulement environ 300x240px.Comment mettre à l'échelle un graphique pour l'adapter à l'espace visible

Pour une raison quelconque, les nouveaux utilisateurs ne sont pas autorisés à publier des images, donc je ne peux pas joindre de captures d'écran. Si quelqu'un pense que cela peut aider avec la réponse (et a le temps) je serais heureux de vous envoyer deux captures d'écran de ce à quoi il ressemble avec seulement des valeurs positives et à quoi il ressemble avec des valeurs négatives.

J'apprécierais vraiment toute aide que je peux obtenir. Je vous remercie.

Le code j'utilise pour produire le graphe est le suivant:

- (id)initWithFrame:(CGRect)frame:(FinancialTopicData *) topicDataToSet:(NSArray *) colors { 

//Assert correct data 
assert([topicDataToSet isKindOfClass:[FinancialTopicData class]]); 

if ((self = [super initWithFrame:frame])) { 

    self.topicData = topicDataToSet; 

    //Create the graph 
    graph = [[CPXYGraph alloc] initWithFrame:CGRectZero]; 

    CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme]; 
    [graph applyTheme:theme]; 
    self.hostedLayer = graph; 

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

    // Paddings 
    graph.paddingLeft = 0.0f; 
    graph.paddingRight = 0.0f; 
    graph.paddingTop = 0.0f; 
    graph.paddingBottom = 0.0f; 
    graph.titlePlotAreaFrameAnchor = CPRectAnchorTop; 

    //Plot padding 
    graph.plotAreaFrame.paddingLeft = 70.0; 
    graph.plotAreaFrame.paddingTop = 10; 
    graph.plotAreaFrame.paddingRight = 20.0; 
    graph.plotAreaFrame.paddingBottom = 45.0; 

    NSDecimalNumber *intervalDivider = [NSDecimalNumber decimalNumberWithString:@kIntervalDivider]; 

    // Add plot space for horizontal bar charts 
    NSDecimalNumber *numberDecimalValueMaxUp = [[topicData maxValueRoundedUp] decimalNumberByAdding:[[[topicData maxValueRoundedUp] decimalNumberBySubtracting:[topicData minValueRoundedDown]] decimalNumberByDividingBy:intervalDivider]]; 
    NSDecimal decimalValueMaxUp = [numberDecimalValueMaxUp decimalValue]; 
    NSDecimal decimalValueMinDown = [[topicData minValueRoundedDown] decimalValue]; 
    NSInteger numberOfTypes = [[topicData years] count]; 
    NSDecimal numberOfBars = [[NSNumber numberWithInteger:numberOfTypes] decimalValue]; 
    NSDecimalNumber *yInterval = [[numberDecimalValueMaxUp decimalNumberBySubtracting:[topicData minValueRoundedDown]] decimalNumberByDividingBy:intervalDivider]; 

    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 

    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:decimalValueMinDown length:decimalValueMaxUp]; 

    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:numberOfBars]; 


    //Add x-axis 
    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet; 
    CPXYAxis *x = axisSet.xAxis; 
    x.majorTickLineStyle = nil; 
    x.minorTickLineStyle = nil; 
    x.majorIntervalLength = CPDecimalFromString(@"1"); 
    x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0"); 

    // Define some custom labels for the data elements 
    x.labelRotation = M_PI/4; 
    x.labelingPolicy = CPAxisLabelingPolicyNone; 


    NSMutableArray *customTickLocations = [NSMutableArray new]; 
    NSArray *xAxisLabels = [topicData years]; 

    for(int i = 0; i < [xAxisLabels count];i++){ 
     [customTickLocations addObject:[NSDecimalNumber numberWithFloat:0.5f+i*1.0f]]; 
    } 

    NSUInteger labelLocation = 0; 
    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]]; 

    for (NSNumber *tickLocation in customTickLocations) { 

     CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle]; 
     newLabel.tickLocation = [tickLocation decimalValue]; 
     newLabel.offset = x.labelOffset + x.majorTickLength*(numberOfTypes/2); 
     newLabel.rotation = M_PI/4; 
     [customLabels addObject:newLabel]; 
     [newLabel release]; 
    } 

    x.axisLabels = [NSSet setWithArray:customLabels]; 

    [customTickLocations release]; 

    CPXYAxis *y = axisSet.yAxis; 
    y.minorTickLineStyle = nil; 
    y.majorIntervalLength = [yInterval decimalValue]; 
    y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0"); 

    int i = 0; 

    for(NSString *key in [topicData types]){ 

     CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[colors objectAtIndex:(i%[colors count])] horizontalBars:NO]; 
     i++; 

     barPlot.baseValue = CPDecimalFromString(@"0"); 
     barPlot.dataSource = topicData; 
     barPlot.barOffset = 1.0f+i; 
     barPlot.identifier = key; 
     barPlot.delegate = topicData; 
     barPlot.needsDisplayOnBoundsChange = YES;  

     [graph addPlot:barPlot toPlotSpace:plotSpace]; 
    } 


} 
return self; 
} 

Les méthodes de source de données:

-(NSNumber *)numberForPlot:(CPPlot *)plot 
       field:(NSUInteger)fieldEnum 
      recordIndex:(NSUInteger)index { 
NSNumber *num = nil;  
if ([plot isKindOfClass:[CPBarPlot class]]) {       
    switch (fieldEnum) { 
     case CPBarPlotFieldBarLocation:{ 
      num = [NSDecimalNumber numberWithUnsignedInteger:index]; 
      break; 
     }  
     case CPBarPlotFieldBarLength:{ 
      NSString *stringValue = [[[types objectForKey:plot.identifier] objectForKey:[years objectAtIndex:index]] objectForKey:KEY_COMPANY_FINANCIALS_VALUE];     
      num = [formatter numberFromString:stringValue]; 
      break; 
     } 
    } 
} 
return num; 
} 


-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{ 
return [years count]; 
} 

Répondre

1

La longueur de plotSpace.yRange doit être la différence entre decimalValueMaxUp et decimalValueMinDown .

+0

Merci, ça a bien fonctionné! – OnlyFreeNick

Questions connexes