2012-02-29 2 views
2

코어 플롯에 새로 추가되었으며 coreplot을 사용하여 그려지는 막대 그래프의 x 축을 따라 NSdate (년 또는 나날 또는 일)를 표시하려고합니다. 일부 샘플을 통과했습니다. 코어 플레이트의 X 축을 따라 NSdate를 플롯 할 수 없습니다

이 하나가 ... X 축을 따라있는 NSDate를 세우고 나에게 감사

를 코드를 제공 할 수 있습니다하시기 바랍니다 .. 플롯 할 수없는 X-axis.but 오전 함께있는 NSDate 음모 I에 유래에서 아래의 코드를 시도 x 축에 레이블을 표시 할 수 없습니다.

// If you make sure your dates are calculated at noon, you shouldn't have to 
// worry about daylight savings. If you use midnight, you will have to adjust 
// for daylight savings time. 
NSDate *refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:31556926 * 10]; 
NSTimeInterval oneDay = 24 * 60 * 60; 

// Invert graph view to compensate for iOS coordinates 
//CGAffineTransform verticalFlip = CGAffineTransformMakeScale(1,-1); 
// self.view.transform = verticalFlip; 

// allocate the graph within the current view bounds 
graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds]; 

// assign theme to graph 
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; 
[graph applyTheme:theme]; 

// Setting the graph as our hosting layer 
CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds]; 

[self.view addSubview:hostingView]; 

hostingView.hostedGraph = graph; 

graph.paddingLeft = 20.0; 
graph.paddingTop = 20.0; 
graph.paddingRight = 20.0; 
graph.paddingBottom = 150.0; 

// setup a plot space for the plot to live in 
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
NSTimeInterval xLow = 0.0f; 
// sets the range of x values 
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow) 
               length:CPTDecimalFromFloat(oneDay*5.0f)]; 
// sets the range of y values 
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) 
               length:CPTDecimalFromFloat(5)]; 

// plotting style is set to line plots 
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
lineStyle.lineColor = [CPTColor blackColor]; 
lineStyle.lineWidth = 2.0f; 

// X-axis parameters setting 
CPTXYAxisSet *axisSet = (id)graph.axisSet; 
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneDay); 
axisSet.xAxis.minorTicksPerInterval = 0; 
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1"); //added for date, adjust x line 
axisSet.xAxis.majorTickLineStyle = lineStyle; 
axisSet.xAxis.minorTickLineStyle = lineStyle; 
axisSet.xAxis.axisLineStyle = lineStyle; 
axisSet.xAxis.minorTickLength = 5.0f; 
axisSet.xAxis.majorTickLength = 7.0f; 
axisSet.xAxis.labelOffset = 3.0f; 

// added for date 
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
dateFormatter.dateStyle = kCFDateFormatterShortStyle; 
CPTTimeFormatter *timeFormatter = [[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease]; 
timeFormatter.referenceDate = refDate; 
axisSet.xAxis.labelFormatter = timeFormatter; 

// Y-axis parameters setting  
axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"0.5"); 
axisSet.yAxis.minorTicksPerInterval = 2; 
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(oneDay); // added for date, adjusts y line 
axisSet.yAxis.majorTickLineStyle = lineStyle; 
axisSet.yAxis.minorTickLineStyle = lineStyle; 
axisSet.yAxis.axisLineStyle = lineStyle; 
axisSet.yAxis.minorTickLength = 5.0f; 
axisSet.yAxis.majorTickLength = 7.0f; 
axisSet.yAxis.labelOffset = 3.0f; 


// This actually performs the plotting 
CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease]; 

CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle]; 
//xSquaredPlot.identifier = @"X Squared Plot"; 
xSquaredPlot.identifier = @"Date Plot"; 

dataLineStyle.lineWidth = 1.0f; 
dataLineStyle.lineColor = [CPTColor redColor]; 
xSquaredPlot.dataLineStyle = dataLineStyle; 
xSquaredPlot.dataSource = self; 

CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]]; 
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0); 
xSquaredPlot.plotSymbol = greenCirclePlotSymbol; 

// add plot to graph 
[graph addPlot:xSquaredPlot]; 

// Add some data 
NSMutableArray *newData = [NSMutableArray array]; 
NSUInteger i; 
for (i = 0; i < 5; i++) { 
    NSTimeInterval x = oneDay*i; 
    id y = [NSDecimalNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2]; 
    [newData addObject: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
     [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX], 
     y, [NSNumber numberWithInt:CPTScatterPlotFieldY], 
     nil]]; 
    NSLog(@"%@",newData); 
} 
plotData = [newData retain]; 

} 

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

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

Core Plot에 포함 된 DatePlot 및 Plot Gallery 예제를 살펴본 후에도 여전히 문제가있는 경우 질문을 편집하여 지금까지 시도한 내용 (코드 포함)을 설명하십시오. –

답변

7

당신이 ...이 같은 뭔가를 찾고 있다면

Key Indicators

당신은 정의를 정의해야합니다 (I 이미지의 크기에 대한 사과, 나는 어떻게 SO에 대한 크기의 이미지에 익숙하지 않은 오전) 귀하의 X 축 레이블. _axisLabelStrings 처리하고 당신이 그것을 할 방법을 정렬 할 필요가 있다고

/* --Define some custom labels for the data elements-- */ 
// Note that x is defined as: CPTXYAxisSet *axisSet = (CPTXYAxisSet*)self.graph.axisSet; 
//          CPTXYAxis *x = axisSet.xAxis; 
x.labelingPolicy = CPTAxisLabelingPolicyNone; // This allows us to create custom axis labels for x axis 
NSMutableArray *ticks = [NSMutableArray arrayWithCapacity:1]; 
for(unsigned int counter = 0; counter < [_axisLabelStrings count];counter++) { 
    // Here the instance variable _axisLabelStrings is a list of custom labels 
    [ticks addObject:[NSNumber numberWithInt:counter]]; 
} 
NSUInteger labelLocation = 0; 
NSMutableArray* customLabels = [NSMutableArray arrayWithCapacity:[_axisLabelStrings count]]; 
@try { 
    for (NSNumber *tickLocation in ticks) { 
     CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [_axisLabelStrings objectAtIndex:labelLocation++] textStyle:x.labelTextStyle]; 
     newLabel.tickLocation = [tickLocation decimalValue]; 
     newLabel.offset = 3.0f;//x.labelOffset + x.majorTickLength could be useful here. 
     newLabel.rotation = M_PI/3.5f; 
     [customLabels addObject:newLabel]; 
     [newLabel release]; 
    } 
} 
@catch (NSException * e) { 
    NSLog(@"An exception occurred while creating date labels for x-axis"); 
} 
@finally { 
    x.axisLabels = [NSSet setWithArray:customLabels]; 
} 
x.majorTickLocations = [NSSet setWithArray:ticks]; 

참고 :

(위 이미지를 생성하는 코드에서 가져온) 다음과 같은 코드를 생각해 보자. NSDates를 사용하여 레이블을 정렬하고 NSString을 생성해야합니다.

하지만 간단히 말해 맞춤 라벨을 만드는 방법입니다.

+0

샘플 코드를 제공해 주시겠습니까? 나는 또한이 똑같은 문제에 당했다. 감사 – Annu

관련 문제