CorePlot - 使用自定义标签的散点图问题

时间:2015-10-11 14:47:51

标签: ios objective-c iphone core-plot

我试图绘制一个散点图,并在探索了一段时间后让轴标签正确。我使用标签策略为none并添加了我自己的自定义标签。但由于某些原因,它只是绘制了一条直线,即使我使用不同的样本数据。这是模拟器图表的图片

http://tinypic.com/r/farkvb/8

我正在使用的代码:

- (void)viewDidLoad {


[super viewDidLoad];
self.sortedYAxisArray = [[NSMutableArray alloc] init];
NSDate *today = [NSDate date];


// All intervals taken from Google
NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd MMM yy"];
self.startDate = [formatter stringFromDate:yesterday];
self.endDate = [formatter stringFromDate:today];
self.yAxisLabels = [[NSMutableArray alloc] initWithObjects:@"1.23234",@"1.23456",@"1.23231",@"1.23453",@"1.23239",@"1.23459",@"1.23233",@"1.23456",@"1.23234",@"1.23456",@"1.23234",@"1.23456",@"1.23234",@"1.23456",@"1.23234",@"1.23456",@"1.23234",@"1.23456",@"1.23234",@"1.23456",@"1.23234",@"1.23456",@"1.22212",@"1.21223", nil];

[self.sortedYAxisArray addObjectsFromArray:self.yAxisLabels];
[self.sortedYAxisArray sortUsingSelector:@selector(compare:)];
self.yMinValue = [[self.sortedYAxisArray firstObject]doubleValue];
self.yMaxValue = [[self.sortedYAxisArray lastObject]doubleValue];
NSLog(@"first object >> %@ last Object >> %@",[self.sortedYAxisArray firstObject],[self.sortedYAxisArray lastObject]);
NSLog(@"min and max values >>> %f %f",self.yMinValue,self.yMaxValue);

[self configureHost];
[self configureGraph];
[self configureAxis];
[self configurePlot];

// Do any additional setup after loading the view, typically from a nib.
  }

 - (void)configureHost {

self.chartingView.alpha = 0.02;

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.frame = self.chartingView.frame;
[self.view addSubview:visualEffectView];
self.hostView = [[CPTGraphHostingView alloc] initWithFrame:self.chartingView.frame];
[self.view addSubview:self.hostView];




    }
    - (void)configureGraph {

CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame: self.hostView.bounds];
graph.plotAreaFrame.paddingLeft = 60.0f;
graph.plotAreaFrame.paddingBottom = 40.0f;
graph.plotAreaFrame.borderLineStyle = nil;
graph.backgroundColor = [UIColor clearColor].CGColor;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
NSTimeInterval xLow       = 0.0;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xLow) length:CPTDecimalFromDouble(24.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(self.yMinValue) length:CPTDecimalFromDouble(self.yMaxValue - self.yMinValue)];
self.hostView.hostedGraph = graph;


  }
  - (void)configureAxis {

    // NSDate *refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:31556926 * 10];

CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 1.0f;
axisLineStyle.lineColor = [CPTColor blackColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor colorWithComponentRed:36/255.0f green:44/255.0f blue:89/255.0f alpha:1.0f];
axisTextStyle.fontName  = @"Helvetica-Bold";
axisTextStyle.fontSize  =  12.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineColor = [CPTColor darkGrayColor];
majorGridLineStyle.lineWidth = 0.2f;
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineColor = [CPTColor lightGrayColor];
minorGridLineStyle.lineWidth = 0.3f;

// 2 - Get axis set
   CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;

axisSet.yAxis.tickDirection = CPTSignNegative;
axisSet.xAxis.tickDirection = CPTSignNone;
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;

//Configuring X-axis
CPTXYAxis *x = axisSet.xAxis;
x.axisLineStyle = axisLineStyle;
x.labelTextStyle = axisTextStyle;
x.majorTickLineStyle = axisLineStyle;
x.majorIntervalLength = CPTDecimalFromFloat(oneDay/6);
x.majorTickLength = 3.0f;
x.minorTickLength = 1.0f;
x.minorTickLineStyle = tickLineStyle;
x.minorTicksPerInterval = 5.0f;
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.labelOffset = 15.0f;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:0],
                                [NSDecimalNumber numberWithInt:6],
                                [NSDecimalNumber numberWithInt:12],
                                [NSDecimalNumber numberWithInt:18],
                                [NSDecimalNumber numberWithInt:23],
                                 nil];
NSMutableArray *minorCustomTickLocations = [[NSMutableArray alloc] init];
for (int i =0 ; i <24 ; i++) {
    [minorCustomTickLocations addObject:[NSDecimalNumber numberWithInt:i]];

}
NSArray *customXAxisLabels = [NSArray arrayWithObjects:self.startDate,@"6 AM",@"12 PM",@"6 PM",self.endDate, nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[customXAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [customXAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
    [customLabels addObject:newLabel];

}
x.axisLabels = [NSSet setWithArray:customLabels];
x.majorTickLocations = [NSSet setWithArray:customTickLocations];
x.minorTickLocations = [NSSet setWithArray:minorCustomTickLocations];

//Configuring Y-axis
CPTXYAxis *y=axisSet.yAxis;
//y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(-0.5);
y.axisLineStyle = axisLineStyle;
y.labelTextStyle = axisTextStyle;
y.majorTickLineStyle = axisLineStyle;
y.majorIntervalLength = CPTDecimalFromFloat(5.0f);
y.majorTickLength = 3.0f;
y.minorTickLength = 1.0f;
y.minorTickLineStyle = tickLineStyle;
y.minorTicksPerInterval = 5.0f;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.labelOffset = 3.0f;


NSMutableArray *customYAxisLabels = [NSMutableArray arrayWithCapacity:[self.yAxisLabels count]];
NSMutableArray *customYTickLocations = [[NSMutableArray alloc] init];


for(int i =0; i<5;i++) {

    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%f",(self.yMinValue + i*(self.yMaxValue - self.yMinValue))]  textStyle:axisSet.yAxis.labelTextStyle];
    NSDecimal location = CPTDecimalFromFloat(self.yMinValue + i*(self.yMaxValue - self.yMinValue));
    newLabel.tickLocation = location;
    newLabel.offset = axisSet.yAxis.labelOffset + axisSet.yAxis.majorTickLength;
    [customYAxisLabels addObject:newLabel];
    [customYTickLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];

}
y.axisLabels = [NSSet setWithArray:customYAxisLabels];
y.majorTickLocations = [NSSet setWithArray:customYTickLocations];



}
- (void)configurePlot {

CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
dataSourceLinePlot.identifier = @"Data Source Plot";

CPTGraph *graph = self.hostView.hostedGraph;


CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
NSTimeInterval xLow       = 0.0;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xLow) length:CPTDecimalFromDouble(24.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(self.yMinValue) length:CPTDecimalFromDouble(self.yMaxValue - self.yMinValue)];

CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth              = 2.0;
lineStyle.lineColor              = [CPTColor colorWithComponentRed:36/255.0f green:44/255.0f blue:89/255.0f alpha:1.0f];
dataSourceLinePlot.dataLineStyle = lineStyle;





// Add plot symbols
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [CPTColor whiteColor];
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol plotSymbol];
plotSymbol.fill               = [CPTFill fillWithColor:[CPTColor blueColor]];
plotSymbol.lineStyle          = symbolLineStyle;
plotSymbol.size               = CGSizeMake(1.0, 1.0);
dataSourceLinePlot.plotSymbol = plotSymbol;
dataSourceLinePlot.dataSource = self;

[graph addPlot:dataSourceLinePlot toPlotSpace:plotSpace];

 }

  #pragma mark - CPTPlotDataSource methods
 -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [self.yAxisLabels count];
 }

  -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSInteger valueCount = [self.yAxisLabels count];
switch (fieldEnum) {
    case CPTScatterPlotFieldX:
        if (index < valueCount) {
            return [NSNumber numberWithUnsignedInteger:index];
        }
        break;

    case CPTScatterPlotFieldY:
        if ([plot.identifier isEqual:@"Data Source Plot"] == YES) {
            NSLog(@"y-axis label value >> %@",[self.yAxisLabels objectAtIndex:index]);
            return [self.yAxisLabels objectAtIndex:index];
        }
        break;
}
return [NSDecimalNumber zero];

}

1 个答案:

答案 0 :(得分:3)

水平网格线,刻度线和标签位于customTickLocations数组(0,6,12,18和23)中给出的位置。绘图点的y值与轴标签文本匹配。由于这些值从一个点到下一个点仅变化0.00001,因此您无法在0到30的范围内看到差异。