gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:48:31 26 4
gpt4 key购买 nike

我试图绘制一个散点图,经过一段时间的探索后,轴标签正确了。我将标签策略设置为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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com