gpt4 book ai didi

iphone - CorePlot - 设置 y 轴范围以包括所有数据点

转载 作者:可可西里 更新时间:2023-11-01 03:30:15 25 4
gpt4 key购买 nike

我在我的 iOS 应用程序中实现了 CorePlot 图,但是我无法根据集合中的数据点动态调整 y 轴的大小。一些数据集的范围为 10-50,而其他数据集的范围为 400-500。在这两种情况下,我都希望 y 原点 (0) 可见。

我尝试过使用 scaletofitplots 方法:

[graph.defaultPlotSpace scaleToFitPlots:[graph allPlots]];

但这对图表没有影响,它们仍然显示默认的 Y 轴范围 0 到 1。

我可以更改 y 轴的唯一方法是通过以下方式手动进行:

graph.defaultPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)) length:CPTDecimalFromFloat(500.0f)];

但是,如您所想,这对于范围较小 (10-50) 的图表效果不佳。

是否有任何方法可以从我的数据集中检索最高 y 值,以便我可以手动将最大 y 轴设置为该值?或者有更好的选择吗?

编辑:这是我正在使用的文件:

#import "TUTSimpleScatterPlot.h"

@implementation TUTSimpleScatterPlot

@synthesize hostingView = _hostingView;
@synthesize graph = _graph;
@synthesize graphData = _graphData;

-(id)initWithHostingView:(CPTGraphHostingView *)hostingView andData:(NSMutableArray *)data {
self = [super init];

if (self != nil) {
self.hostingView = hostingView;
self.graphData = data;
self.graph = nil;
}
return self;
}

-(void)initialisePlot {

if ((self.hostingView == nil) || (self.graphData == nil)) {
NSLog(@"TUTSimpleScatterPlot: Cannot Initialise plot with hosting view and data");
return;
}

if (self.graph != nil) {
NSLog(@"TUTSimpleScatterPlot: Graph Object already exists.");
}
NSDate *refDate = [NSDate date];
NSTimeInterval oneDay = 24 * 60 * 60;

CGRect frame = [self.hostingView bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:frame];

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
self.graph.plotAreaFrame.paddingBottom = 20.0f;
self.graph.plotAreaFrame.paddingLeft = 35.0f;


self.hostingView.hostedGraph = self.graph;

CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 2.0f;

CPTMutableLineStyle *lineStyleThin = [CPTMutableLineStyle lineStyle];
lineStyleThin.lineColor = [CPTColor whiteColor];
lineStyleThin.lineWidth = 1.0f;

CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"HelveticaNeue-Medium";
textStyle.fontSize = 10;
textStyle.color = [CPTColor whiteColor];

CPTMutableLineStyle *plotSymbolLineStyle = [CPTMutableLineStyle lineStyle];
plotSymbolLineStyle.lineColor = [CPTColor whiteColor];
plotSymbolLineStyle.lineWidth = 1.0f;

CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.lineStyle = plotSymbolLineStyle;
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.4 green:0.6 blue:0.8 alpha:1.0]];
plotSymbol.size = CGSizeMake(8.0, 8.0);

CPTScatterPlot *plot = [[CPTScatterPlot alloc] init];
plot.dataSource = self;
plot.identifier = @"mainPlot";

[plotSpace scaleToFitPlots:[graph allPlots]];

CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromDouble(615)];
plotSpace.yRange = yRange;

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(oneDay - (oneDay * 6.3f)) length:CPTDecimalFromFloat(oneDay * 6.6f)];
plotSpace.allowsUserInteraction = YES;

CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0f) length:CPTDecimalFromDouble(615.0f)];
plotSpace.globalYRange = globalYRange;

// Modify the graph's axis with a label, line style, etc
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;

axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 1.0f;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneDay*1);
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");
axisSet.xAxis.minorTicksPerInterval = 0;
axisSet.xAxis.majorTickLength = 0.0f;
axisSet.xAxis.majorGridLineStyle = majorGridLineStyle;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM"];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = refDate;
axisSet.xAxis.labelFormatter = timeFormatter;

axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyleThin;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"50.0");
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTickLength = 3.0;
axisSet.yAxis.majorTickLength = 5.0;
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0);
axisSet.yAxis.majorGridLineStyle = majorGridLineStyle;

axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];

CPTMutableLineStyle *theLineStyle = [CPTMutableLineStyle lineStyle];
theLineStyle.lineColor = [CPTColor colorWithComponentRed:0.3 green:0.6 blue:0.9 alpha:1.0];
theLineStyle.lineWidth = 2.0f;

plot.dataLineStyle = theLineStyle;
plot.plotSymbol = plotSymbol;

[self.graph addPlot:plot];

}

最佳答案

看起来您正在正确使用 -scaleToFitPlots:。如果需要,此方法将更新绘图数据。在调用数据源之前,确保正确的数据可用于数据源。

根据新代码进行编辑:

-scaleToFitPlots: 在您将绘图添加到图表之前被调用。因此 -allPlots 数组是空的,没有可缩放的。在绘图构建并添加到图形后移动缩放操作。

关于iphone - CorePlot - 设置 y 轴范围以包括所有数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9808538/

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