0
我們已經使用Core-Plot實現了2Y散點圖 - 輸出結果是用X和Y軸繪製了一張圖表,然後在頂部繪製了一些空白區域,然後繪製了X和Y2軸圖表。Core-Plot:2Y繪圖縮放問題
我們希望圖表具有X和Y軸,X軸和Y2軸圖表重疊,以便用戶可以正確查看。
示例代碼是在這裏:
-(void)configureGraph {
graph = [[CPTXYGraph alloc] initWithFrame:CGRectMake(0, 0, self.hostView.bounds.size.width, self.hostView.bounds.size.height)];
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
self.hostView.hostedGraph = graph;
[graph setPaddingTop:5.0f];
[graph setPaddingLeft:5.0f];
[graph setPaddingRight:5.0f];
[graph setPaddingBottom:5.0f];
[graph.plotAreaFrame setPaddingTop:5.0f];
[graph.plotAreaFrame setPaddingRight:20.0f];
[graph.plotAreaFrame setPaddingLeft:20.0f];
[graph.plotAreaFrame setPaddingBottom:25.0f];
graph.plotAreaFrame.borderLineStyle = nil;
}
-(void)configurePlots {
CPTColor *your_color = [CPTColor colorWithComponentRed:126 green:230 blue:255 alpha:0];
int cntSelectedVar = 0;
graph = self.hostView.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
CPTXYPlotSpace *plotSpace2 = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = NO;
plotSpace2.allowsUserInteraction = NO;
//
// 1 - Get graph and plot space
graph.plotAreaFrame.fill = [CPTFill fillWithColor:your_color];
// 2 - Create the three plots
CPTScatterPlot *aaplPlot = [[CPTScatterPlot alloc] init];
aaplPlot.dataSource = self;
aaplPlot.identifier = variableID ;
CPTColor *aaplColor = [self GetRGBForPlotSymbol:cntSelectedVar];
[graph addPlot:aaplPlot toPlotSpace:plotSpace];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
plotSpace.yRange = yRange;
// 4 - Create styles and symbols
CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy];
aaplLineStyle.lineWidth = 0;
aaplLineStyle.lineColor = aaplColor;
aaplPlot.dataLineStyle = aaplLineStyle;
CPTMutableLineStyle *aaplSymbolLineStyle = [CPTMutableLineStyle lineStyle];
aaplSymbolLineStyle.lineColor = aaplColor;
CPTPlotSymbol *aaplSymbol = [CPTPlotSymbol ellipsePlotSymbol];
aaplSymbol.fill = [CPTFill fillWithColor:aaplColor];
aaplSymbol.lineStyle = aaplSymbolLineStyle;
aaplSymbol.size = CGSizeMake(3.0f, 3.0f);
aaplPlot.plotSymbol = aaplSymbol;
cntSelectedVar++;
CPTScatterPlot *googPlot= [[CPTScatterPlot alloc] init];
googPlot.dataSource = self;
googPlot.identifier = variableID;
[self.hostView.hostedGraph addPlot:googPlot];
CPTColor *googColor = [self GetRGBForPlotSymbol:cntSelectedVar];
plotSpace2.xRange = plotSpace.xRange;
CPTMutablePlotRange *y2Range = [[CPTMutablePlotRange alloc] initWithLocation:CPTDecimalFromDouble(FinalMinValueY2) length:CPTDecimalFromDouble(FinalMaxValueY2)];
plotSpace2.yRange = y2Range ;
CPTMutableLineStyle *googLineStyle = [googPlot.dataLineStyle mutableCopy];
googLineStyle.lineWidth = 0;
googLineStyle.lineColor = googColor;
googPlot.dataLineStyle = googLineStyle;
CPTMutableLineStyle *googSymbolLineStyle = [CPTMutableLineStyle lineStyle];
googSymbolLineStyle.lineColor = googColor;
CPTPlotSymbol *googSymbol = [CPTPlotSymbol ellipsePlotSymbol];
googSymbol.fill = [CPTFill fillWithColor:googColor];
googSymbol.lineStyle = googSymbolLineStyle;
googSymbol.size = CGSizeMake(3.0f, 3.0f);
googPlot.plotSymbol = googSymbol;
[graph addPlot:googPlot toPlotSpace:plotSpace2];
cntSelectedVar++;
[plotSpace scaleToFitPlots:[graph allPlots]];
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor blackColor];
axisTitleStyle.fontName = @"Helvetica";
axisTitleStyle.fontSize = 10.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle ];
axisLineStyle.lineWidth = 1.0f;
axisLineStyle.lineColor = [CPTColor blackColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor blackColor];
axisTextStyle.fontName = @"Helvetica";
axisTextStyle.fontSize = 8.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
gridLineStyle.lineColor = [CPTColor grayColor];
gridLineStyle.lineWidth = 1.0f;
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) (id)self.hostView.hostedGraph.axisSet;
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(OrignalMinValueY);
CPTXYAxis *x = axisSet.xAxis;
CPTXYAxis *y = axisSet.yAxis;
x.titleTextStyle = axisTitleStyle;
x.titleOffset = 12.0f;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.labelTextStyle = axisTextStyle;
x.majorTickLineStyle = axisLineStyle;
x.tickDirection = CPTSignNegative;
x.minorTickLineStyle = nil;
x.coordinate = CPTCoordinateX;
CGFloat dateCount = [Reportdates count];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
// CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:st textStyle:x.labelTextStyle];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSMutableSet *xMajorLocations = [NSMutableSet set];
[formatter setPositiveFormat:@"0.##" ];
for (double j = FinalMinValueX; j <= FinalMaxValueX; j += FinalIntervalValueX) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[formatter stringFromNumber:[ NSNumber numberWithDouble:j ] ] textStyle:x.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j);
label.tickLocation = location;
label.offset = x.labelOffset;
if (label) {
[xLabels addObject:label];
}
[xMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
}
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
// 4 - Configure y-axis
//y.title = @"Price";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = -40.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.majorTickLineStyle = axisLineStyle;
y.tickDirection = CPTSignPositive;
y.separateLayers= YES;
double majorIncrement = FinalIntervalValueY;
double minorIncrement = FinalIntervalValueY;
CGFloat yMax = FinalMaxValueY; // should determine dynamically based on max price
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
NSMutableSet *yMinorLocations = [NSMutableSet set];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setPositiveFormat:@"0.##" ];
for (double j = FinalMinValueY; j <= yMax; j += minorIncrement) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[formatter stringFromNumber:[ NSNumber numberWithDouble:j ] ] textStyle:y.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j);
label.tickLocation = location;
label.offset = -20;
if (label) {
[yLabels addObject:label];
}
[yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
}
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;
y.minorTickLocations = yMinorLocations;
if([ApplicationSettingOBJ.GB_VariableArrayY2 count ]>0)
{
CPTXYAxis *y2 = [[CPTXYAxis alloc] init] ;
y2.titleTextStyle = axisTitleStyle;
y2.coordinate = CPTCoordinateY;
y2.plotSpace = plotSpace2;
y2.labelingPolicy = CPTAxisLabelingPolicyNone;
y2.separateLayers= NO;
y2.majorGridLineStyle = nil;
y2.labelTextStyle = axisTextStyle;
y2.majorTickLineStyle = axisLineStyle;
y2.tickDirection = CPTSignNone;
y2.orthogonalCoordinateDecimal =CPTDecimalFromFloat(dateCount);
majorIncrement = FinalIntervalValueY2;
minorIncrement = FinalIntervalValueY2;
yMax = FinalMaxValueY2; // should determine dynamically based on max price
NSMutableSet *yLabels1 = [NSMutableSet set];
NSMutableSet *yMajorLocations1 = [NSMutableSet set];
NSMutableSet *yMinorLocations1 = [NSMutableSet set];
for (double j = FinalMinValueY2; j <= yMax; j += minorIncrement) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[formatter stringFromNumber:[ NSNumber numberWithDouble:j ] ] textStyle:y2.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j);
label.tickLocation = location;
label.offset = -20;
if (label) {
[yLabels1 addObject:label];
}
[yMajorLocations1 addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
}
y2.axisLabels = yLabels1;
y2.majorTickLocations = yMajorLocations1;
y2.minorTickLocations = yMinorLocations1;
axisSet.axes = [NSArray arrayWithObjects:x, y,y2, nil];
}
thnx eric ...當我嘗試使用CPTXYPlotSpace * plotSpace2 = [[CPTXYPlotSpace alloc] init];在屏幕上出現與x軸相鄰的單個曲線圖,並在y2軸的曲線圖中將所有繪圖符號都遺留下來。 –
你可以看到屏幕截圖:[link](https://www.dropbox.com/s/8p92wxtgvsbef8m/screenshotafterinit.png)prioryly它看起來像這樣.... [link](https:// www .dropbox.com/s/h32e9w9qmlpnic1/Prevscreenshot.png) –
您從不設置'plotSpace2'的'yRange'。它仍然使用[0,1]的默認值。 –