我们使用Core-Plot实现了2Y散点图 - 输出有一个用X和Y轴绘制的图表,然后是顶部的一些空白区域,然后绘制X和Y2轴图表。
我们希望图表具有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];
}
答案 0 :(得分:2)
plotSpace
和plotSpace2
是同一个对象(graph.defaultPlotSpace
)。如果您想要两个不同的y比例,请创建一个新的绘图空间并将其添加到图表中。
CPTXYPlotSpace *plotSpace2 = [[CPTXYPlotSpace alloc] init];
[graph addPlotSpace:plotSpace2];