2011-03-18 72 views
0

我在应用程序中使用核心图并从过去的一周开始尝试在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; 
    } 

截图 -

My Graph

我想表明对y轴X轴和默认标签自定义标签。

编辑:

我试着从CPTest-iPhoneApp即可将样品类BARCHART + XIB的。条形图出现,但轴标签不显示。以下是CPTest-iPhone应用程序和我的截图。

CPTest-iPhoneApp

mine

回答

0

我已经解决了我的问题。我所要做的就是从头开始添加核心图。我从我的项目中删除了核心图再次添加瞧!它的工作

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计数]];

为(*的NSNumber在tickLocation1 customTickLocations1)

{ 
    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

试过你的代码,但它不起作用。 – 2011-03-31 14:25:00

+0

你得到的错误/输出是什么! – ravoorinandan 2011-04-05 07:15:32

+0

我已在帖子中发布图片。我正在获取除轴标签之外的所有内容。我试图将核心情节代码的示例应用程序放在我的应用程序中,但没有获得标题标签的运气。看到我的文章中的图片,你会明白我的意思 – 2011-04-07 03:15:25