gpt4 book ai didi

ios - 一次显示 2 个 CPTXYPlotSpace

转载 作者:行者123 更新时间:2023-11-28 22:26:15 25 4
gpt4 key购买 nike

我有下面的代码来显示两个 Y 轴(一个在右边,一个在左边)。我希望能够绘制每个不同数据集的每个图形(映射到相同的 X 值)。我似乎无法同时显示两个 CPTXYPlotSpaces。我该怎么做才能绘制到两个不同的绘图空间并同时查看绘制的线条。

    -(void)configureHost
{
CGRect bounds = self.view.bounds;
bounds.size.height = bounds.size.height;
self.hostView = [[CPTGraphHostingView alloc] initWithFrame:bounds];
self.hostView.allowPinchScaling = YES;
[self.view addSubview:self.hostView];
}

-(void)configureGraph
{
self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
[self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
self.hostView.hostedGraph = self.graph;

self.graph.paddingLeft = 20.0;
self.graph.paddingTop = 20.0;
self.graph.paddingRight = 20.0;
self.graph.paddingBottom = 20.0;


}

NSString *const kPlot1 = @"Plot 1";
NSString *const kPlot2 = @"Plot 2";

-(void)configurePlots
{
CGRect bounds = self.hostView.bounds;

self.plotSpace1 = (CPTXYPlotSpace *) self.graph.defaultPlotSpace;
self.plotSpace1.allowsUserInteraction = NO;

self.scatterPlot1 = [[CPTScatterPlot alloc]init];
self.scatterPlot1.identifier = kPlot1;
self.scatterPlot1.dataSource = self;
[self.graph addPlot:self.scatterPlot1 toPlotSpace:self.plotSpace1];

self.scatterPlot2 = [[CPTScatterPlot alloc]init];
self.scatterPlot2.identifier = kPlot2;
self.scatterPlot2.dataSource =self;
[self.graph addPlot:self.scatterPlot2 toPlotSpace:self.plotSpace2];

[self.plotSpace1 scaleToFitPlots:@[self.scatterPlot1, self.scatterPlot2]];
[self.plotSpace2 scaleToFitPlots:@[self.scatterPlot1, self.scatterPlot2]];

CPTGraph *graph = self.hostView.hostedGraph;
graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
[self setTitleDefaultsForGraph:graph withBounds:bounds];
[self setPaddingDefaultsForGraph:graph withBounds:bounds];
graph.plotAreaFrame.paddingTop = 20.0;
graph.plotAreaFrame.paddingBottom = 50.0;
graph.plotAreaFrame.paddingLeft = 50.0;
graph.plotAreaFrame.paddingRight = 50.0;
graph.plotAreaFrame.cornerRadius = 10.0;
graph.plotAreaFrame.masksToBorder = NO;
graph.plotAreaFrame.axisSet.borderLineStyle = [CPTLineStyle lineStyle];

// Setup plot space
self.plotSpace1.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(10.0)];
self.plotSpace1.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(10.0)];


self.plotSpace2 = [[CPTXYPlotSpace alloc] init];
self.plotSpace2.xRange = self.plotSpace1.xRange;
self.plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(10.0)];

[graph addPlotSpace:self.plotSpace2];


}

-(void)setPaddingDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds
{
// CGFloat boundsPadding = round( bounds.size.width / CPTFloat(20.0) ); // Ensure that padding falls on an integral pixel
//
// graph.paddingLeft = boundsPadding;
//
// if ( graph.titleDisplacement.y > 0.0 ) {
// graph.paddingTop = graph.titleTextStyle.fontSize * 2.0;
// }
// else {
// graph.paddingTop = boundsPadding;
// }
//
// graph.paddingRight = boundsPadding;
// graph.paddingBottom = boundsPadding;
graph.paddingTop = 0.0f;
graph.paddingBottom = 0.0f;
graph.paddingLeft = 0.0f;
graph.paddingRight = 0.0f;
}

-(void)setTitleDefaultsForGraph:(CPTGraph *)graph withBounds:(CGRect)bounds
{
graph.title = self.title;
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.color = [CPTColor grayColor];
textStyle.fontName = @"Helvetica-Bold";
textStyle.fontSize = round( bounds.size.height / CPTFloat(20.0) );
graph.titleTextStyle = textStyle;
graph.titleDisplacement = CPTPointMake( 0.0, textStyle.fontSize * CPTFloat(1.5) );
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
}

-(void)configureAxis
{
CPTGraph *graph = self.hostView.hostedGraph;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;

// Line styles
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 3.0;
axisLineStyle.lineCap = kCGLineCapRound;

// Text styles
CPTMutableTextStyle *axisTitleTextStyle = [CPTMutableTextStyle textStyle];
axisTitleTextStyle.fontName = @"Helvetica-Bold";
axisTitleTextStyle.fontSize = 14.0;

CPTXYAxis *x = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);//Where the Y Axis meets the X axis
x.majorIntervalLength = CPTDecimalFromDouble(1.5);//Interval for X Axis
x.minorTicksPerInterval = 4;
x.tickDirection = CPTSignNone;
x.axisLineStyle = axisLineStyle;
x.majorTickLength = 12.0;
x.majorTickLineStyle = axisLineStyle;
x.minorTickLength = 8.0;
x.title = @"X Axis";
x.titleTextStyle = axisTitleTextStyle;
x.titleOffset = 25.0;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];


// Label y with an automatic labeling policy.
axisLineStyle.lineColor = [CPTColor greenColor];

CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.minorTicksPerInterval = 9;
y.tickDirection = CPTSignNegative;
y.axisLineStyle = axisLineStyle;
y.majorTickLength = 6.0;
y.majorTickLineStyle = axisLineStyle;
y.minorTickLength = 4.0;

y.title = @"Y Axis";
y.titleTextStyle = axisTitleTextStyle;
y.titleOffset = 30.0;


// Label y2 with an equal division labeling policy.
axisLineStyle.lineColor = [CPTColor orangeColor];

CPTXYAxis *y2 = [[CPTXYAxis alloc] init];
y2.coordinate = CPTCoordinateY;
y2.plotSpace = self.plotSpace2;
y2.orthogonalCoordinateDecimal = CPTDecimalFromDouble(10.0);//Where the Y Axis meets the X axis
y2.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
y2.preferredNumberOfMajorTicks = 6;
y2.minorTicksPerInterval = 9;
y2.tickDirection = CPTSignPositive;
y2.axisLineStyle = axisLineStyle;
y2.majorTickLength = 6.0;
y2.majorTickLineStyle = axisLineStyle;
y2.minorTickLength = 4.0;
y2.title= @"Y2 Axis";
y2.titleTextStyle = axisTitleTextStyle;
y2.titleOffset = 30.0;

// Add the y2 axis to the axis set
graph.axisSet.axes = @[x, y, y2];
}

#pragma mark CPTPlotDataSource

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{

if ( [(NSString *)plot.identifier isEqualToString:kPlot1] ) {
return 10;
}
else if ( [(NSString *)plot.identifier isEqualToString:kPlot2] ) {
return 5;
} else {
return 0;
}
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
{

switch (fieldEnum) {


case CPTScatterPlotFieldX:
if ([plot.identifier isEqual:kPlot1]) {
return [NSNumber numberWithUnsignedInt:idx];
break;
}else if ([plot.identifier isEqual:kPlot2]){
return [NSNumber numberWithUnsignedInt:idx];
break;
}

case CPTScatterPlotFieldY:
if ([plot.identifier isEqual:kPlot1]) {
return @(idx *2);
} else if ([plot.identifier isEqual:kPlot2]) {
return @(idx +2);
}

}
return 0;
}

最佳答案

缩放后,您将(重新)初始化 plotSpace2。设置绘图空间后,将调用移至 [self.plotSpace2 scaleToFitPlots:...]

关于ios - 一次显示 2 个 CPTXYPlotSpace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18878872/

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