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

标签 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];

}

最佳答案

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

关于ios - CorePlot-使用自定义标签的散点图问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33066318/

相关文章:

ios - 降序排列 NSArray

ios - 如何在 playground 中使用 cocoapods?

objective-c - SKShapeNode 中风不在 Swift 中绘制

ios - 当元素被激活时如何获取indexpath.row?

iphone - 自定义标注 View 不显示 map 中心

ios - 如何将 `Int8?` 转换为 `Int?` ?

ios - 放置在 UITableViewController 顶部时看不到搜索栏

objective-c - CGImage Masking 在 iOS 12 上停止工作

ios - 无法对非字符串的内容执行子字符串操作

iphone - 替换对象索引 :withObject: not working when passing in a NSString object