无法在核心图中的x轴和y轴标签上显示任何内容 [英] unable to show anything on x-axis and y-axis labels in core-plot

查看:68
本文介绍了无法在核心图中的x轴和y轴标签上显示任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在应用程序中使用核心图,过去一周来,我试图在x轴和y轴上显示标签。但是还没有成功。我在这里将我的代码和屏幕截图一起发布。如果有人知道解决此问题的任何解决方法,请紧急通知我。

I'm using core plot in an application and from past one week I am trying to show labels on x-axis and y-axis. But haven't succeed yet. I'm posting my code here with screen shot. If someone knows any solution to fix the problem let me know urgently.

代码-

 -(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;
   }

屏幕截图-

我想显示自定义标签

编辑:

我尝试添加示例CPTest-iPhoneApp的barChart + XIB的类。出现条形图,但没有轴标签。这是CPTest-iPhone应用程序和我的屏幕截图。

I've tried adding sample class of barChart+XIB from CPTest-iPhoneApp. The bar chart appears but axis label's don't. Here is the screenshot form CPTest-iPhone app and mine.

推荐答案

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

I have resolved my problem. All I have to do is start adding core-plot from beginning. I removed core-plot from my project again add voila! it's working

这篇关于无法在核心图中的x轴和y轴标签上显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆