开发者

s7graphview example

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 gi开发者_StackOverflow社区ving it a try ;)

Best regards, Paul Peelen


Core Plot does indeed support custom tick labels on the X axis:

s7graphview example


(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.


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... ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜