//
//  YCPlotDataSource.m
//  HR-WorkLoad
//
//  Created by Julian on 2/1/10.
//  Copyright 2010 Electronic Visualization Laboratory, University of Illinois at Chicago. All rights reserved.
//

#import "YCPlotDataSource.h"

@implementation YCPlotDataSource

@synthesize plotData;

+ (YCPlotDataSource *) initWithArray:(NSMutableArray *)array;
{
	YCPlotDataSource *source = [[[YCPlotDataSource alloc] init] autorelease];
	source.plotData = [array retain];
	
	return source;
}

- (void)dealloc 
{
	[plotData release];
	
    [super dealloc];
}

#pragma mark -
#pragma mark Additional values accesor
- (double *) getMinMax
{
	double *minmax = (double *) malloc(sizeof(double) * 2);
	
	for(int i = 0; i < [plotData count]; ++i) {
		NSDecimalNumber *num = [[plotData objectAtIndex:i] objectForKey:[NSNumber numberWithInt:1]];
		double val = [num doubleValue];

		if(i == 0) {
			minmax[0] = val;
			minmax[1] = val;
		} else {
			if(minmax[0] > val) {
				minmax[0] = val;
			}
			
			if(minmax[1] < val) {
				minmax[1] = val;
			}
		}		
	}
	
	return minmax;
}

- (NSNumber *) getValueAtIndex:(NSUInteger) index
{
	NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:1]];
	
	return num;
}

#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{
    return plotData.count;
}

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
	
    return num;
}

@end
