Frage

Ich mag die Maximal- und Minimalwerte eines NSMutableArray bekommen, damit ich einen Kern-Plot plotspace erstellen und Grafik auf Basis dieser Maximal- und Minimalwerte.

Mein Code ist wie folgt:

NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
NSUInteger i;
for (i=0; i<60; i++){
id x = [NSNumber numberWithFloat:i*0.05];
id y = [NSNumber numberWithFloat:1.2*rand()/(float)RAND_Max + 0.6];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}

self.dataForPlot = contentArray;

CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat() length:CPDecimalFromFloat()];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat() length:CPDecimalFromFloat()];

Wie fülle ich die Lücken für xrange und YRange Zuordnung, wenn ich die Grafik Spanne nur der Raum wollen, was in contentArray angegeben wird?

Keine korrekte Lösung

Andere Tipps

In diesem Fall sollten Sie -[CPPlotSpace scaleToFitPlots:] verwenden. Weitere allgemeine Berechnungen auf Array-Werte, lesen Sie weiter ...

Dies ist eine ideale Anwendung für Schlüsselwertcodierung und die damit verbundene Array Operatoren . In Ihrem Beispiel

float maxX = [[contentArray valueForKeyPath:@"@max.x"] floatValue];
float minY = [[contentArray valueForKeyPath:@"@min.x"] floatValue];

float maxY = [[contentArray valueForKeyPath:@"@max.y"] floatValue];
float minY = [[contentArray valueForKeyPath:@"@min.y"] floatValue];

Das Array Operatoren Aufruf valueForKeyPath: auf dem contentArray, die eine Anordnung durch den Aufruf der gleichen valueForKeyPath: an jedem Element des Arrays mit dem Schlüsselpfad rechts von der Array-Operator (das heißt @min und @max) gebaut gibt. Der Orignal Aufruf setzt dann den bestimmten Betreiber zu der resultierenden Anordnung. Sie könnte leicht eine Kategorie NSArray definieren, um Sie eine Struktur von min / max Werten zurück:

typedef struct {
  float minX; 
  float minY;
  float maxX;
  float maxY;
} ArrayValueSpace;

@implementation NSArray (PlotSpaceAdditions)
- (ArrayValueSpace)psa_arrayValueSpace {
  ArrayValueSpace result;

  result.maxX = [[contentArray valueForKeyPath:@"@max.x"] floatValue];
  result.minX = [[contentArray valueForKeyPath:@"@min.x"] floatValue];

  result.maxY = [[contentArray valueForKeyPath:@"@max.y"] floatValue];
  result.minY = [[contentArray valueForKeyPath:@"@min.y"] floatValue];

  return result;
}

Sie können die Maximal- und Minimalwerte finden, wenn Sie es füllen, aber es würde nur funktionieren, wenn in Ihrem Snippet verwendet. Wenn Sie etwas mehr Generika wollen, sollten Sie einfach die Liste durchgehen und die Suche nach ihm.

// contentArray already defined
int maxX = 0, minX = 1000, maxY = 0, minY = 1000;

for (int i = 0; i < [contentArray count]; ++1)
{
   int curX = [[[contentArray objectForKey:@"x"] objectAtIndex:i] integerValue];
   int curY = [[[contentArray objectForKey:@"y"] objectAtIndex:i] integerValue];

   if (curX < minX)
     minX = curX;
   else if (curX > maxX)
     maxX = curX;

   if (curY < minY)
     minY = curY;
   else if (curY > maxY)
     maxY = curY;
}

Sie könnten schnell Aufzählung durch die Array. .

NSUInteger  maxX = 0;
NSUInteger  minX = 0xFFFFFFFF; //(for 32 bit)
NSUInteger  maxY = 0;
NSUInteger  minY = 0xFFFFFFFF; //(for 32 bit)
for ( NSDictionary *dict in contentArray ) 
{
    NSUInteger   newX = [[dict objectForKey:@"x"] integerValue];
    NSUInteger   newY = [[dict objectForKey:@"y"] integerValue];


    if (maxX > newX) maxX = newX;
    if (minX > newX) minX = newX;
    if (maxY > newY) maxY = newY;
    if (minY > newY) minX = newY;
}

Wenn Sie NSMutableArray von NSDictionary haben Objekte, die Sie diese verwenden können:

-(float)findMax:array arrayKey:obj {
    float max = [[[array objectAtIndex:0] objectForKey:obj] floatValue];
    for ( NSDictionary *dict in array ) {
        if(max<[[dict objectForKey:obj] floatValue])max=[[dict objectForKey:obj] floatValue];
    }
    return max;
}

-(float)findMin:array arrayKey:obj {
    float min = [[[array objectAtIndex:0] objectForKey:obj] floatValue];
    for ( NSDictionary *dict in array ) {
        if (min > [[dict objectForKey:obj] floatValue])min = [[dict objectForKey:obj] floatValue];
    }
    return min;
}

Sie mögen, dass diese Methoden zugreifen, wenn in einer anderen Klasse:

GraphData *c=[[GraphData alloc] init];
float maxY=[c findMax:plotData arrayKey:[NSNumber numberWithInt:1]]; //1 or 0 depending on axis 
[c release];

// Dies gibt Max und Min Wert in NSMutableArrray - (void) FindingMinAndMaxValueInNSMutableArray {

NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:@"0.987",@"0.951",@"0.881",@"0.784",@"0.662",@"0.522",@"0.381",@"-0.265",@"-0.197", @"0.189",@"-0.233",@"0.310",@"0.402",@"0.402",@"0.988",@"0.633",@"0.661",@"0.656",@"0.617",@"0.634",@"0.690",@"0.767",@"0.836",nil];

NSLog(@"The Array Value is %@",array);
NSLog(@"The Array Count is %lu",(unsigned long)array.count);

NSNumber *maxValue = [array valueForKeyPath:@"@max.doubleValue"];
NSLog(@"The maxValue is %@",maxValue);
NSString *str=[NSString stringWithFormat:@"%@",maxValue];
NSInteger path=[array indexOfObject:str];
NSIndexPath *indexpath=[NSIndexPath indexPathForItem:path inSection:0];
NSLog(@"Max Value = %@ and index = %ld",maxValue,(long)indexpath.row);


NSNumber *minValue = [array valueForKeyPath:@"@min.doubleValue"];
NSLog(@"The minValue is %@",minValue);
NSString *str1 = [NSString stringWithFormat:@"%@",minValue];
NSInteger path1 = [array indexOfObject:str1];
NSIndexPath *indexPath1 = [NSIndexPath indexPathForItem:path1 inSection:0];
NSLog(@"Min Value =%@ and index = %ld",minValue,(long)indexPath1.row);

}

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top