0

I am looking for an graph library for the iPhone which allowes me to set texts as values on the X-axis. After a quick look at core-plot, it seems its uncapable to do so. I stumbled over s7graphview but can't seem to get it working.

Is there anyone who did got it working, and might be able to share it with me? Or any links to some examples? It think my brain just shut off cause its late, but I am giving it a try ;)

Best regards, Paul Peelen

Kshitiz Ghimire
  • 1,716
  • 3
  • 18
  • 37
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168

2 Answers2

2

Core Plot does indeed support custom tick labels on the X axis: alt text
(source: sunsetlakesoftware.com)

To produce these custom labels, you just need to set the labeling policy of the X axis to CPAxisLabelingPolicyNone and provide your own. For example, you can do the following to replicate the above drawing (in the Core Plot iPhone test application):

x.labelRotation = M_PI/4;
x.labelingPolicy = CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [[NSMutableArray alloc] initWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations)
{
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = x.labelOffset + x.majorTickLength;
    newLabel.rotation = M_PI/4;
    [customLabels addObject:newLabel];
    [newLabel release];
}

x.axisLabels =  [NSSet setWithArray:customLabels];

There's no need to subclass CPAxis to add these labels.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • Thanks brad for this great help. May be core-plot will be more helpful for we like newBees, if we get the updated tutorial for some more enhanced functionalities, if we get the picture shown by you in the tutorial, it will be of great help – Madhup Singh Yadav Dec 12 '09 at 04:20
  • Yes, we need to have some better documentation, but unfortunately the framework is changing fast enough that it might be difficult to keep up to date. We're working on pulling together a stable release, and walkthrough documentation would be part of that package. Also note that this tutorial was written by a third party. – Brad Larson Dec 12 '09 at 15:37
1

You can change the label names as

-(NSArray *)newAxisLabelsAtLocations:(NSArray *)locations
{
    NSMutableArray *newLabels = [[NSMutableArray alloc] initWithCapacity:locations.count];
    for ( NSDecimalNumber *tickLocation in locations ) {
        NSString *labelString = [self.labelFormatter stringForObjectValue:tickLocation];
        CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText:[NSString stringWithFormat:@"Day %@",labelString] textStyle:self.labelTextStyle];
        //[newLabel setTextColor:[CPColor whiteColor]];
        newLabel.tickLocation = [tickLocation decimalValue];
        newLabel.rotation = self.labelRotation;
        switch ( self.tickDirection ) {
            case CPSignNone:
                newLabel.offset = self.labelOffset + self.majorTickLength / 2.0f;
                break;
            case CPSignPositive:
            case CPSignNegative:
                newLabel.offset = self.labelOffset + self.majorTickLength;
                break;
        }
        [newLabels addObject:newLabel];
        [newLabel release];
    }
    return newLabels;
}

in the CPAxis.m in core-plot

Hope this helps... ;)

Madhup Singh Yadav
  • 8,110
  • 7
  • 51
  • 84
  • allright... but I still have the following errors: error: incompatible type for argument 1 of 'setMajorIntervalLength:' error: request for member 'axisLabelOffset' in something not a structure or union error: incompatible type for argument 1 of 'setMajorIntervalLength:' error: request for member 'axisLabelOffset' in something not a structure or union error: request for member 'bounds' in something not a structure or union error: request for member 'defaultPlotSymbol' in something not a structure or union error: request for member 'bounds' in something not a structure or union :( – Paul Peelen Dec 11 '09 at 18:53
  • Added an new question for just those problems: http://stackoverflow.com/questions/1890207/coreplot-problems – Paul Peelen Dec 11 '09 at 19:08