Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

iphone, using an array to define in core-plot range

I'm almost done with a core-plot graph I've been working on for a couple of day now. There is something I am still not able to do (and I cannot find documentation on this), is to change the x axis labels to what I need. Today, I have an x axis with integer label beeing displayed every 5 values: "5 10 15...", I need to have labels coresponding to the last 24 hours. For instance if it's 15:00, I would need labels like: "15 16 17 ... 23 0 1 2 .. 15" I was thinking of using a NSArray for this and passing it to the plotSpace.xRange but I do not know if this is the good way to do it. Here is my code:

            CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
            plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-15)
                                                           length:CPDecimalFromFloat(xmax + 15)];
            plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-1000)
                                                           length:CPDecimalFromFloat(4300)];


            // Setup axis
            CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
            CPLineStyle *lineStyle = [CPLineStyle lineStyle];
            lineStyle.lineColor = [CPColor whiteColor];
            lineStyle.lineWidth = 1.0f;
            CPTextStyle *cyanStyle = [CPTextStyle textStyle];
            cyanStyle.color = [CPColor cyanColor];
            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
            [formatter setMaximumFractionDigits:0];

            axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"5"] decimalValue];
            axisSet.xAxis.minorTicksPerInterval = 0;
            axisSet.xAxis.majorTickLineStyle = lineStyle;
            axisSet.xAxis.minorTickLineStyle = lineStyle;
            axisSet.xAxis.axisLineStyle = lineStyle;
            axisSet.xAxis.majorTickLength = 5.0f;
            axisSet.xAxis.labelOffset = 3.0f;
            axisSet.xAxis.labelExclusionRanges = [NSArray arrayWithObjects:
                                      [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-15) 
                                                                  length:CPDecimalFromFloat(15)], 
                                      nil];
            axisSet.xAxis.visibleRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromInteger(0) length:CPDecimalFromInteger(xmax)];
            axisSet.xAxis.labelFormatter = formatter;
            axisSet.xAxis.title = @"Hour";
            axisSet.xAxis.titleOffset = 25.0f;
            axisSet.xAxis.titleLocation = CPDecimalFromFloat(25.0f);
            axisSet.xAxis.titleTextStyle = cyanStyle;

Any help would be really welcome :) Thanks a lot, Luc

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I finally found the solution using custom label:

            NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:0],
                                                                     [NSDecimalNumber numberWithInt:5],
                                                                     [NSDecimalNumber numberWithInt:10],
                                                                     [NSDecimalNumber numberWithInt:15],
                                                                     [NSDecimalNumber numberWithInt:20],
                                                                     [NSDecimalNumber numberWithInt:25],
                                                                     [NSDecimalNumber numberWithInt:30],
                                                                     [NSDecimalNumber numberWithInt:35],                                                
                                                                     [NSDecimalNumber numberWithInt:40],                                                
                                                                     nil];
            NSArray *xAxisLabels = [NSArray arrayWithObjects:@"15",@"18",@"21",@"0",@"3",@"6",@"9",@"12",@"15",nil];

            NSUInteger labelLocation = 0;
            NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
            for (NSNumber *tickLocation in customTickLocations) {
                CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:axisSet.xAxis.labelTextStyle];
                newLabel.tickLocation = [tickLocation decimalValue];
                newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
                [customLabels addObject:newLabel];
                [newLabel release];
            }

            axisSet.xAxis.axisLabels =  [NSSet setWithArray:customLabels];

Still have some thing to clarify but this is surely the way to go :) Luc


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...