2011-03-18 4 views
0

응용 프로그램에서 코어 플롯을 사용하고 있으며 지난 1 주일부터 x 축과 y 축에 레이블을 표시하려고합니다. 그러나 아직 성공하지 못했습니다. 여기 스크린 샷으로 코드를 게시하고 있습니다. 문제를 해결할 수있는 해결책을 누군가가 알고 있다면 긴급하게 알려주십시오.코어 플롯의 x 축 및 y 축 레이블에 아무 것도 표시 할 수 없음

코드 -

-(void)viewDidLoad { 
[super viewDidLoad]; 

// Initialize all graph dependent data. 
//self.dataForPlot = [[NSMutableArray alloc] initWithCapacity:0]; 

minYValues = [[NSMutableArray alloc] initWithCapacity:0]; 
maxYValues = [[NSMutableArray alloc] initWithCapacity:0]; 

[self createGraph]; 
[self customizeGraph]; 
    } 

- (void) createGraph{ 

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

CPGraphHostingView *hostingView = (CPGraphHostingView *)self.view; 
hostingView.collapsesLayers = YES; 
hostingView.hostedGraph = graph; 
hostingView.frame = self.view.frame; 

//Create a blue plot area 
CPScatterPlot *boundLinePlot = [[[CPScatterPlot alloc] init] autorelease]; 
boundLinePlot.dataLineStyle.miterLimit = 1.0f; 
boundLinePlot.dataLineStyle.lineWidth = 1.0f; 

UIColor* color = [UIColor orangeColor]; 
boundLinePlot.dataLineStyle.lineColor = [CPColor colorWithCGColor:[color CGColor]]; 
boundLinePlot.dataSource = self; 
[graph addPlot:boundLinePlot]; 
    } 

    - (void) customizeGraph{ 
if(graph) 
{ 

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




    CPScatterPlot *goalWeightPlot = [[[CPScatterPlot alloc] init] autorelease]; 
    goalWeightPlot.identifier = kGoalWeightPlot; 
     //boundLinePlot.dataLineStyle.miterLimit = 5.0f; 
    goalWeightPlot.dataLineStyle.lineWidth = 1.0f; 
    goalWeightPlot.dataLineStyle.lineColor = [CPColor redColor]; 
    goalWeightPlot.dataLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0],[NSNumber numberWithFloat:2.0],nil]; 
    goalWeightPlot.dataSource = self; 
    [graph addPlot:goalWeightPlot]; 


    // Create a blue plot area 
    CPScatterPlot *boundLinePlot = [[[CPScatterPlot alloc] init] autorelease]; 
    boundLinePlot.identifier = kActualWeightPlot; 
     //boundLinePlot.dataLineStyle.miterLimit = 5.0f; 
    boundLinePlot.dataLineStyle.lineWidth = 1.0f; 
    boundLinePlot.dataLineStyle.lineColor = [CPColor orangeColor]; 
    boundLinePlot.dataSource = self; 


    // Add plot symbols 
    CPLineStyle *symbolLineStyle = [CPLineStyle lineStyle]; 
    symbolLineStyle.lineColor = [CPColor orangeColor]; 

    CPPlotSymbol *plotSymbol = [CPPlotSymbol ellipsePlotSymbol]; 
    plotSymbol.fill = [CPFill fillWithColor:[CPColor orangeColor]]; 
    plotSymbol.lineStyle = symbolLineStyle; 
    plotSymbol.size = CGSizeMake(5.0, 5.0); 
    boundLinePlot.plotSymbol = plotSymbol; 
    [graph addPlot:boundLinePlot]; 
} 
    } 


    - (void) resetData{ 
    dataForPlot = nil; 
    } 

    - (void) setGraphData:(NSArray*)graphData andRefrenceValue:(float)goalValue{ 

self.refereceValue = goalValue; 
[self setGraphData:graphData]; 

    } 

    - (void) setGraphData:(NSArray*)graphData{ 

//Check if we have any single weight entry in the array 
if(graphData && [graphData count] > 0) { 
    [self prepareGraphData:graphData]; 
    [self setRangeForGraph]; 
    [graph reloadData]; 
} 
    } 

- (NSArray *)sortedWeightEntriesByWeightDate:(NSArray *)unsortedArray { 

NSMutableArray *tempArray = [NSMutableArray array]; 

for(int i=0;i<[unsortedArray count];i++) { 

    NSDateFormatter *df = [[NSDateFormatter alloc]init]; 
    WeightEntry *entry = [unsortedArray objectAtIndex:i]; 

    [df setDateFormat:@"yyyy-MM-dd"]; 

    NSDate *date = [df dateFromString:entry.weightDate]; 

    NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 

    if(date) {  

     [dict setObject:entry forKey:@"entity"]; 

     [dict setObject:date forKey:@"date"]; 

     [tempArray addObject:dict]; 
    } 

    [df release]; 
} 

NSInteger counter = [tempArray count]; 

NSDate *compareDate; 

NSInteger index; 

for(int i = 0 ; i < counter; i++) { 

    index = i; 

    compareDate = [[tempArray objectAtIndex:i] valueForKey:@"date"]; 

    NSDate *compareDateSecond; 

    for(int j = i+1 ; j < counter; j++) 
    { 
     compareDateSecond=[[tempArray objectAtIndex:j] valueForKey:@"date"]; 

     NSComparisonResult result = [compareDate compare:compareDateSecond]; 
     if(result == NSOrderedDescending) 
     { 
      compareDate = compareDateSecond; 
      index=j; 
     } 
    } 
    if(i!=index) 
     [tempArray exchangeObjectAtIndex:i withObjectAtIndex:index]; 
} 

NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:0]; 
NSInteger counterIndex = [tempArray count]; 
for(int i = 0; i < counterIndex ; i++) { 
    [sortedArray addObject:[[tempArray objectAtIndex:i] valueForKey:@"entity"]]; 
} 
return [NSArray arrayWithArray:sortedArray]; 

} 


    - (void) prepareGraphData:(NSArray*)data{ 

data = [self sortedWeightEntriesByWeightDate:data]; 

NSNumber* minYValue = nil; 
NSNumber* maxYValue = nil; 


NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:[data count]]; 
NSUInteger i; 
for (i = 0; i < [data count]; i++) { 
    WeightEntry* weightEntry = [data objectAtIndex:i]; 

    if(i == 0){  
     maxYValue = [NSNumber numberWithFloat:weightEntry.weight]; 
     minYValue = [NSNumber numberWithFloat:weightEntry.weight]; 
    } 

    //id x = [NSNumber numberWithFloat:weightEntry.weight]; 
    //id y = [NSNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2]; 

    id x = [NSNumber numberWithFloat:i]; 
    id y = [NSNumber numberWithFloat:weightEntry.weight]; 

    if([y floatValue] > [maxYValue floatValue]) 
     maxYValue = y; 

    if([y floatValue] < [minYValue floatValue]) 
     minYValue = y; 

    //[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y",[NSNumber numberWithFloat:goalWeight],@"goalY",nil]]; 
    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y",nil]]; 
} 
self.dataForPlot = [NSArray arrayWithArray:contentArray]; 

[minYValues addObject:minYValue]; 
[maxYValues addObject:maxYValue]; 


lblHighValue.text = [NSString stringWithFormat:@"High = %0.2f", [maxYValue floatValue]]; 
lblLowValue.text = [NSString stringWithFormat:@"Low = %0.2f", [minYValue floatValue]]; 
lblRefrenceValue.text = [NSString stringWithFormat:@"Goal = %0.2f", self.refereceValue]; 

    } 

     // Update the Plot Space Range to cover all graphs 
    - (void) setRangeForGraph{ 
NSNumber* minimumYValue; 
NSNumber* maxmumYValue; 

if([minYValues count] > 0 && [maxYValues count] > 0){ 
    minimumYValue = [minYValues objectAtIndex:0]; 
    maxmumYValue = [maxYValues objectAtIndex:0]; 

    // Calculate minimum y value among all graphs. 
    for (int i = 0 ; i < [minYValues count] ; i++) { 
     if([[minYValues objectAtIndex:i] floatValue] < [minimumYValue floatValue]) 
      minimumYValue = [minYValues objectAtIndex:i]; 
    } 

    // Calculate maximum y value among all graphs. 
    for (int i = 0 ; i < [maxYValues count] ; i++) { 
     if([[maxYValues objectAtIndex:i] floatValue] > [maxmumYValue floatValue]) 
      maxmumYValue = [maxYValues objectAtIndex:i]; 
    } 

    NSDecimalNumber *high = [NSDecimalNumber decimalNumberWithDecimal:[maxmumYValue decimalValue]]; 
    high = [high decimalNumberByAdding:[NSDecimalNumber decimalNumberWithString:@"30"]]; 

    // Modify the y range for plot space to cover all values. 
    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:[high decimalValue]]; 
    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:CPDecimalFromInt([self.dataForPlot count])]; 

    CPPlotAreaFrame *area = (CPPlotAreaFrame *)graph.plotAreaFrame; 
    area.paddingLeft = 20; 
    area.paddingBottom = 10; 

    CPXYAxisSet *axisSet = (CPXYAxisSet*)graph.axisSet; 
    //axis.paddingLeft = 20.0; 
    axisSet.xAxis.paddingBottom = 50.0; 


    CPXYAxis *x = axisSet.xAxis; 
    x.majorIntervalLength = CPDecimalFromInteger([self.dataForPlot count]); 

    CPXYAxis *y = axisSet.yAxis; 
    y.majorIntervalLength = CPDecimalFromFloat([high floatValue]); 

    axisSet.xAxis.orthogonalCoordinateDecimal = CPDecimalFromFloat([minimumYValue floatValue]); 

     //axisSet.xAxis.labelOffset = 0.0; 
    CPLineStyle *lineStyle = [CPLineStyle lineStyle]; 
    lineStyle.lineColor = [CPColor colorWithCGColor:((UIColor*)kProtienColor).CGColor]; 
    lineStyle.lineWidth = 1.0f; 

     // style the graph with white text and lines 
    CPTextStyle *whiteText = [CPTextStyle textStyle]; 
    whiteText.color = [CPColor redColor];    


     //CPXYAxis *x = axisSet.xAxis; 
    x.majorIntervalLength = CPDecimalFromString(@"1"); 
    x.axisLineStyle = lineStyle; 
     //x.majorGridLineStyle=lineStyle; 
     //x.minorTicksPerInterval = 0; 
     //x.minorTickLineStyle = lineStyle; 

    x.title = @"Weight"; 
    x.titleOffset = 3.0f; 
    x.titleLocation = CPDecimalFromFloat(3.0f); 

    x.titleTextStyle = whiteText; 
    x.labelTextStyle = whiteText; 


    //y.majorIntervalLength = CPDecimalFromString(@"150"); 
    //y.minorTicksPerInterval = 10; 
    y.axisLineStyle = lineStyle; 
    y.title = @"Date"; 
    y.titleTextStyle = whiteText; 
    y.titleOffset = 0; 
    y.minorTickLineStyle = lineStyle; 
     //y.titleLocation = CPDecimalFromFloat(graph.frame.origin.y+10); 
     //y.majorGridLineStyle=lineStyle; 
     //y.labelTextStyle=whiteText; 

} 
    } 

    - (NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot { 
int nofOfRecords = 0; 

@try { 

    nofOfRecords = [self.dataForPlot count]; 
} 
@catch (NSException * e) { 
    NSLog(@"Exception while calculating graph index : %@", [e description]); 
} 
@finally { 
    //NSLog(@"Number of Records : %d For Graph Index : %d", nofOfRecords, graphIndex); 
    return nofOfRecords; 
} 
    } 

    - (NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { 
NSNumber *num = 0; 
//int plotIndex = [(NSString *)plot.identifier intValue]; 
if([self.dataForPlot count] > 0){ 
    if(![((NSString*)[plot identifier]) isEqualToString:kGoalWeightPlot]){ 
     num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")]; 
    }else { 
     if(fieldEnum == CPScatterPlotFieldX) 
      num = [[dataForPlot objectAtIndex:index] valueForKey:@"x"]; 
     else { 
      num = [NSNumber numberWithFloat:self.refereceValue]; 
     } 
    } 

} 
return num; 
    } 

스크린 샷 -

내가 y 축에 X 축 및 기본 라벨에 사용자 정의 레이블을 표시 할

My Graph

.

편집 : 나는 CPTest-iPhoneApp에서 막대 그래프 + XIB의 샘플 클래스를 추가하려고했습니다

. 막 대형 차트는 나타나지만 축 레이블은 나타나지 않습니다. 다음은 CPTest-iPhone 앱 및 내 스크린 샷 양식입니다.

CPTest-iPhoneApp

mine

답변

0

문제가 해결되었습니다. 내가해야 할 일은 처음부터 코어 플롯을 추가하는 것입니다. 나는 프로젝트에서 core-plot을 제거하고 다시 짜증나게한다! 작동 중

0
CPXYAxis *yy = axisSet.yAxis; 
yy.axisLineStyle.lineColor=[CPColor whiteColor]; 
yy.majorTickLineStyle = nil; 
yy.minorTickLineStyle = nil; 
yy.title = @"Y Axis"; 
yy.titleLocation = CPDecimalFromFloat(100.0f); 
yy.titleOffset = 245.0f; 

// 데이터 요소에 대한 일부 사용자 지정 라벨을 정의 yy.labelingPolicy = CPAxisLabelingPolicyNone;

NSArray *customTickLocations1 = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:0], [NSDecimalNumber numberWithInt:10],[NSDecimalNumber numberWithInt:20], [NSDecimalNumber numberWithInt:30], nil]; 

NSArray *yAxisLabels = [NSArray arrayWithObjects:@"a",@"b", @"c",nil]; 

NSUInteger labelLocation1 = 0; 

있는 NSMutableArray customLabels1 * = [NSMutableArray를 arrayWithCapacity [yAxisLabels 카운트]; (customTickLocations1에서의 NSNumber * tickLocation1) 용

{ 
    CPAxisLabel *newLabel1 = [[CPAxisLabel alloc] 

      initWithText: [yAxisLabels objectAtIndex:labelLocation1++] 

      textStyle:yy.labelTextStyle]; 

newLabel1.tickLocation = tickLocation1 decimalValue];

newLabel1.offset = yy.labelOffset + yy.majorTickLength;

[customLabels1 addObject : newLabel1];

[newLabel1 release];

} 
yy.axisLabels = [NSSet setWithArray:customLabels1]; 
+0

코드를 시도했지만 작동하지 않습니다. –

+0

오류/출력은 무엇입니까? – ravoorinandan

+0

이미 내 게시물에 이미지를 올렸습니다. 축 레이블을 제외한 모든 것을 얻고 있습니다. 나는 애플 리케이션에있는 것처럼 코어 플롯의 코드 샘플 애플 리케이션을 넣으려고했지만 제목 라벨을 얻는 데는 아무런 운이 없었습니다. 내 게시물의 이미지를 보면 내가 말하고자하는 것을 이해할 수 있습니다. –

관련 문제