2011-04-20 6 views
0

저는 Core Plot을 가지고 놀았으며 동적 날짜 x 축을 만들려고합니다. 날짜 플롯 예제에서 정적 날짜 축을 만들 수 있었지만 언제든지 2 분짜리 창을 만들고 xRange 값을 업데이트하려고합니다. xRange min 및 length 값으로 날짜를 전달하고 x 축에 시간을 표시하는 방법은 잘 모르겠습니다.x 축의 날짜 이동

예제를 살펴 봤지만 NSTimeInterval (이 방법은 ...하는 경우)을 사용할 수 없었습니다. (도움이된다면) 다음

다음은

enter image description here

지금까지 내 시도되는 사진입니다; 누군가가 이것을 달성하는 방법에 대해 조언을 해줄 수 있습니까? 플롯을 시작할 위치

- (void)loadView { 
// Alloc & Init Main View and since the display resolution is 1024x768 take 20 off for labels later 
UIView *tmpView = [ [ UIView alloc ] initWithFrame:CGRectMake(0, 0, 1024.0,768.0) ]; 
[ tmpView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ]; 
[ tmpView setBackgroundColor:[ UIColor redColor ] ]; 

// Alloc Graph View 
graphView = [ [ CPGraphHostingView alloc ] initWithFrame:CGRectMake(0, 0, 1024.0,768.0) ]; 
[ tmpView addSubview:[ graphView autorelease ] ]; 

// Set MainView 
[ self setView:[ tmpView autorelease ] ]; 

} 

-(void)viewDidLoad{ 
[super viewDidLoad]; 

NSDate *refDate = [NSDate date]; 

// NSTimeInterval oneDay = 24 * 60 * 60; 
NSTimeInterval oneHour = 60 * 60; 
NSTimeInterval fivemin= 5 * 60; 
// Create graph from theme 
graph = [(CPXYGraph *) [CPXYGraph alloc] initWithFrame:self.view.bounds]; 

CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme]; 
[graph applyTheme:theme]; 
graphView.hostedGraph = graph; 

//padding 
graph.paddingLeft = 20.0; 
graph.paddingTop = 20.0; 
graph.paddingRight = 20.0; 
graph.paddingBottom = 20.0; 

graph.plotAreaFrame.paddingTop=10.0; 
graph.plotAreaFrame.paddingLeft=50.0; 
graph.plotAreaFrame.paddingRight=35.0; 
graph.plotAreaFrame.paddingBottom=50.0; 

// Setup scatter plot space 
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 
NSTimeInterval xLow = 0.0f; 
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(xLow) length:CPDecimalFromFloat(oneHour)]; 
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(100.0f)]; 

//Line Styles 
CPLineStyle *lineStyle = [CPLineStyle lineStyle]; 
lineStyle.lineColor = [CPColor redColor]; 
lineStyle.lineWidth = 2.0f; 
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]; 

CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet; 


// X-Axes formatting 
CPXYAxis *x = axisSet.xAxis; 
x.majorIntervalLength = CPDecimalFromFloat(oneHour); 
x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0"); 
x.minorTicksPerInterval = 0; 
x.labelOffset=0; 
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
dateFormatter.dateStyle = kCFDateFormatterShortStyle; 
CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease]; 
timeFormatter.referenceDate = refDate; 
x.labelFormatter = timeFormatter; 
x.majorGridLineStyle = majorGridLineStyle; 
x.minorGridLineStyle = minorGridLineStyle; 
[email protected]"Time Axis"; 


//Y-Axes formatting 
CPXYAxis *y = axisSet.yAxis; 
y.majorIntervalLength = [ [ NSDecimalNumber decimalNumberWithString:@"10.0" ] decimalValue ]; 
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0"); 
y.minorTicksPerInterval = 5; 
y.labelOffset = 0.0; 
y.majorGridLineStyle = majorGridLineStyle; 
y.minorGridLineStyle = minorGridLineStyle; 
y.preferredNumberOfMajorTicks = 10; 
y.minorTickLineStyle = nil; 
y.labelTextStyle = nil; 
y.visibleRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(100.0f)]; 
CPConstraints yConstraints = {CPConstraintFixed, CPConstraintFixed}; 
y.isFloatingAxis=YES; 
y.constraints=yConstraints; 


// Create a plot that uses the data source method 
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease]; 
dataSourceLinePlot.identifier = @"Date Plot"; 
dataSourceLinePlot.dataLineStyle = lineStyle; 
dataSourceLinePlot.dataSource = self; 

[graph addPlot:dataSourceLinePlot]; 

mydata = [[NSMutableArray alloc]initWithObjects: 
      [NSDecimalNumber numberWithInt:0], 
      nil ]; 
//a timer to re-load the graph every 2 seconds and re-draw x-axis 

Timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(testingTimer:) userInfo:nil repeats:YES]; 

} 


-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot 
{ 
return mydata.count; 
} 

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{ 
switch (fieldEnum) { 
    case CPScatterPlotFieldX: 
     return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index]; 
    case CPScatterPlotFieldY: 
     return [mydata objectAtIndex:index]; 
} 
return nil; 
} 


-(void) testingTimer: (NSTimer *) Timer{ 

//generating random number and add to mydata array 
testdata=arc4random() % 100; 
[mydata addObject:[NSNumber numberWithInt:testdata]]; 

[graph reloadData]; 
count++; 
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(count) length:CPDecimalFromFloat(5*30.0f)]; 

} 

답변

2

xRange의 위치는 5 분 줄거리로드 데이터 (초당 1 개 항목을 가정)가 있다면, 예를 들면, 당신은 분 3에서 시작하고 싶어 당신의 위치는 3 * 60이됩니다. 2 분간의 데이터 만 표시하려면 길이가 항상 2 * 60이됩니다.

x.majorIntervalLength도 변경하려는 경우, 해당 값은 x 축을 따라 큰 눈금이 나타나는 빈도를 제어합니다. 주요 틱에는 일반적으로 레이블이 연결되어 있으므로 2 분 동안 데이터를보다 적절하게 값을 낮추고 자 할 때

+0

@andrew lattis 이제 xaxis가 움직이는 것을 볼 수 있지만 실제 그래프는 더 이상 (나는 y 축에서 점을 볼 수 있지만 선 자체는 볼 수 없다). 그러나 내가 위치에 대한 상수 값을 주면 라인 그래프를 볼 수 있습니다. count가 xRange에서 구현하는 잘못된 방법입니까? – Linus

+0

값이 y 축 범위 밖에있을 수 있습니까? 원래 주석의 코드에서 y 축 범위를 0-100으로 설정하면 값이 모두 해당 범위에 맞습니까? 그것들은 보이는 범위보다 위/아래에있는 것처럼 들린다. –

+0

값은 확실히 0과 100 사이이며 yaxis 라인 (x = 0에서)에서 그래프를 볼 수는 있지만 실제 그래프에는 없다. xrange plotwithLocation 값을 0.0fi로하면 그래프를 볼 수 있습니다 (그러나 x 축이 움직이지 않을 것으로 예상 함) – Linus

관련 문제