我有散点图和条形图的图表,我有两个y轴。我想能够在没有y轴移动的情况下水平滚动。当我只有一个y轴时,我的代码可以工作,但是当我添加第二个时。第二个仍在上下移动。你有什么想法吗?
这是我的代码:
-(void) configureAxes:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme{
[super configureAxes:layerHostingView withTheme:theme];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) layerHostingView.hostedGraph.axisSet;
CPTAxis *x = axisSet.xAxis;
CPTAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.labelingPolicy= CPTAxisLabelingPolicyNone;
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:self.selectedExplYear.exploitation.count];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:self.selectedExplYear.exploitation.count];
for (int i = 0; i<self.selectedExplYear.exploitation.count; i++) {
SCExploitation *tech = self.selectedExplYear.exploitation[i];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMMM"];
NSDate *dateExploitation = [NSDate dateWithTimeIntervalSince1970:tech.exploitationDate.doubleValue/1000];
NSString *mois = [df stringFromDate:dateExploitation];
CPTAxisLabel *label = [[CPTAxisLabel alloc]initWithText:[NSString stringWithFormat:@"%@", mois] textStyle:x.labelTextStyle];
CGFloat location = i;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = x.majorTickLength;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
NSNumberFormatter *Xformatter = [[NSNumberFormatter alloc] init];
[Xformatter setGeneratesDecimalNumbers:NO];
[Xformatter setNumberStyle:NSNumberFormatterDecimalStyle];
y.labelFormatter = Xformatter;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) layerHostingView.hostedGraph.defaultPlotSpace;
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 14.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 14.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor];
tickLineStyle.lineWidth = 2.0f;
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
x.axisLabels=xLabels;
x.majorTickLocations = xLocations;
axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:5];
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(250)];
CPTXYAxis *y2 = [[CPTXYAxis alloc] init];
y2.coordinate = CPTCoordinateY;
y2.plotSpace = self.scatterGraphPlotSpace;
y2.axisConstraints = [CPTConstraints constraintWithUpperOffset:0];
y2.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
y2.axisLineStyle = axisLineStyle;
y2.majorTickLineStyle = axisLineStyle;
y2.title = @"Km";
y2.majorIntervalLength = CPTDecimalFromFloat((80 - 0) / 5.0f);
y2.labelFormatter = Xformatter;
y2.titleTextStyle = axisTextStyle;
y2.labelTextStyle = axisTextStyle;
y2.tickDirection = CPTSignNone;
y2.tickLabelDirection = CPTSignPositive;
y2.orthogonalCoordinateDecimal = CPTDecimalFromFloat(5.0);
y2.titleOffset = 16.0 * CPTFloat(-2.1);
y2.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(80)];
self.scatterGraphPlotSpace.xRange = plotSpace.xRange;
layerHostingView.hostedGraph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
}
-(void)configurePlots:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme
{
CPTGraph *graph = layerHostingView.hostedGraph;
CPTBarPlot *barPlotTotalDetenteEau = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
barPlotTotalDetenteEau.identifier = DETENTEEAUIDENTIFIER;
CPTMutableLineStyle *lsEauPotable = [[CPTMutableLineStyle alloc]init];
lsEauPotable.lineColor = [CPTColor lightGrayColor];
lsEauPotable.lineWidth = 0.5;
barPlotTotalDetenteEau.lineStyle = lsEauPotable;
barPlotTotalDetenteEau.dataSource =self;
barPlotTotalDetenteEau.delegate = self;
barPlotTotalDetenteEau.zPosition = 90;
barPlotTotalDetenteEau.labelOffset = LABELOFFSET;
barPlotTotalDetenteEau.title = NSLocalizedString(@"exploitation_detente_eau", nil);
barPlotTotalDetenteEau.barWidth = CPTDecimalFromDouble(BARWIDTH);
barPlotTotalDetenteEau.barOffset = CPTDecimalFromDouble(0);
[graph addPlot:barPlotTotalDetenteEau toPlotSpace:graph.defaultPlotSpace];
CPTBarPlot *barPlotTotalControleGaz = [CPTBarPlot tubularBarPlotWithColor:[CPTColor yellowColor] horizontalBars:NO];
barPlotTotalControleGaz.identifier = CONTROLEGAZIDENTIFIER;
CPTMutableLineStyle *lsNouveau = [[CPTMutableLineStyle alloc]init];
lsNouveau.lineColor = [CPTColor lightGrayColor];
lsNouveau.lineWidth = 0.5;
barPlotTotalControleGaz.lineStyle = lsNouveau;
barPlotTotalControleGaz.dataSource =self;
barPlotTotalControleGaz.delegate = self;
barPlotTotalControleGaz.zPosition = 100;
barPlotTotalControleGaz.labelOffset = LABELOFFSET;
barPlotTotalControleGaz.title = NSLocalizedString(@"exploitation_controle_gaz", nil);
barPlotTotalControleGaz.barWidth = CPTDecimalFromDouble(BARWIDTH);
barPlotTotalControleGaz.barOffset = CPTDecimalFromDouble(BARWIDTH+0.01);
[graph addPlot:barPlotTotalControleGaz toPlotSpace:graph.defaultPlotSpace];
self.scatterGraphPlotSpace = [[CPTXYPlotSpace alloc] init];
self.scatterGraphPlotSpace.allowsUserInteraction =YES;
// self.scatterGraphPlotSpace.delegate =self;
CPTScatterPlot *controlerPlot = [[CPTScatterPlot alloc] init];
controlerPlot.dataSource = self;
controlerPlot.delegate = self;
controlerPlot.title = NSLocalizedString(@"km_fuite", nil);
controlerPlot.identifier = KMCONTROLERIDENTIFIER;
controlerPlot.plotSymbolMarginForHitDetection = 15.0f;
CPTColor *effectivePlotColor = [CPTColor redColor];
[graph addPlot:controlerPlot toPlotSpace:self.scatterGraphPlotSpace];
CPTBarPlot *barPlotTotalControleEau = [CPTBarPlot tubularBarPlotWithColor:[CPTColor greenColor] horizontalBars:NO];
barPlotTotalControleEau.identifier = CONTROLEEAUIDENTIFIER;
CPTMutableLineStyle *lsProvisoire = [[CPTMutableLineStyle alloc]init];
lsProvisoire.lineColor = [CPTColor lightGrayColor];
lsProvisoire.lineWidth = 0.5;
barPlotTotalControleEau.lineStyle = lsProvisoire;
barPlotTotalControleEau.dataSource = self;
barPlotTotalControleEau.delegate = self;
barPlotTotalControleEau.zPosition = 100;
barPlotTotalControleEau.labelOffset = LABELOFFSET;
barPlotTotalControleEau.title = NSLocalizedString(@"exploitation_controle_eau", nil);
barPlotTotalControleEau.barWidth = CPTDecimalFromDouble(BARWIDTH);
barPlotTotalControleEau.barOffset = CPTDecimalFromDouble(0);
[graph addPlot:barPlotTotalControleEau toPlotSpace:graph.defaultPlotSpace];
CPTBarPlot *barPlotTotalDetenteGaz = [CPTBarPlot tubularBarPlotWithColor:[CPTColor grayColor] horizontalBars:NO];
barPlotTotalDetenteGaz.identifier = DETENTEGAZIDENTIFIER;
CPTMutableLineStyle *lsAugmentation = [[CPTMutableLineStyle alloc]init];
lsAugmentation.lineColor = [CPTColor lightGrayColor];
lsAugmentation.lineWidth = 0.5;
barPlotTotalDetenteGaz.lineStyle = lsAugmentation;
barPlotTotalDetenteGaz.dataSource =self;
barPlotTotalDetenteGaz.delegate = self;
barPlotTotalDetenteGaz.zPosition = 70;
barPlotTotalDetenteGaz.labelOffset = LABELOFFSET;
barPlotTotalDetenteGaz.title = NSLocalizedString(@"exploitation_detente_gaz", nil);
barPlotTotalDetenteGaz.barWidth = CPTDecimalFromDouble(BARWIDTH);
barPlotTotalDetenteGaz.barOffset = CPTDecimalFromDouble(BARWIDTH+0.01);
[graph addPlot:barPlotTotalDetenteGaz toPlotSpace:graph.defaultPlotSpace];
NSArray *plots = @[barPlotTotalDetenteEau,barPlotTotalControleEau,barPlotTotalControleGaz,barPlotTotalDetenteGaz];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
[plotSpace scaleToFitPlots:plots];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.3f)];
xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f) length:CPTDecimalFromFloat(4.0f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.4f)];
plotSpace.yRange = yRange;
plotSpace.delegate = self;
self.scatterGraphPlotSpace.globalYRange = plotSpace.globalYRange;
CPTScatterPlot *visuelPlot = [[CPTScatterPlot alloc] init];
visuelPlot.dataSource = self;
visuelPlot.delegate = self;
visuelPlot.title = NSLocalizedString(@"km_visuel", nil);
visuelPlot.identifier = KMVISUELIDENTIFIER;
visuelPlot.plotSymbolMarginForHitDetection = 15.0f;
CPTColor *budgeteePlotColor = [CPTColor orangeColor];
CPTMutableLineStyle *budgeteelLineStyle = [CPTMutableLineStyle lineStyle]; // [budgeteePlot.dataLineStyle mutableCopy];
budgeteelLineStyle.lineWidth = 2.5;
budgeteelLineStyle.lineColor = budgeteePlotColor;
visuelPlot.dataLineStyle = budgeteelLineStyle;
CPTPlotSymbol *budgeteeSymbol = [CPTPlotSymbol ellipsePlotSymbol];
budgeteeSymbol.fill = [CPTFill fillWithColor:budgeteePlotColor];
budgeteeSymbol.lineStyle = budgeteelLineStyle;
budgeteeSymbol.size = CGSizeMake(6.0f, 6.0f);
visuelPlot.plotSymbol = budgeteeSymbol;
CPTMutableLineStyle *effectiveLineStyle = [CPTMutableLineStyle lineStyle];
effectiveLineStyle.lineWidth = 2.5;
effectiveLineStyle.lineColor = effectivePlotColor;
controlerPlot.dataLineStyle = effectiveLineStyle;
CPTPlotSymbol *effectiveSymbol = [CPTPlotSymbol ellipsePlotSymbol];
effectiveSymbol.fill = [CPTFill fillWithColor:effectivePlotColor];
effectiveSymbol.lineStyle = effectiveLineStyle;
effectiveSymbol.size = CGSizeMake(6.0f, 6.0f);
controlerPlot.plotSymbol = effectiveSymbol;
[self.scatterGraphPlotSpace scaleToFitPlots:[NSArray arrayWithObjects:controlerPlot, visuelPlot, nil]];
[graph addPlot:visuelPlot toPlotSpace:self.scatterGraphPlotSpace];
CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(250)];
plotSpace.globalYRange = globalYRange;
self.scatterGraphPlotSpace.yRange =[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(80)];
CPTMutablePlotRange *y2Range = [self.scatterGraphPlotSpace.yRange mutableCopy];
[y2Range expandRangeByFactor:CPTDecimalFromCGFloat(1.4f)];
self.scatterGraphPlotSpace.yRange = y2Range;
[layerHostingView.hostedGraph addPlotSpace:self.scatterGraphPlotSpace];
}
-(void)configureGraph:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme
{
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:layerHostingView.bounds];
graph.plotAreaFrame.masksToBorder = NO;
layerHostingView.hostedGraph = graph;
if(self.selectedExplYear.exploitation.count> 3){
graph.defaultPlotSpace.allowsUserInteraction = YES;
}
[graph applyTheme:self.theme];
graph.paddingBottom = 60.0f;
graph.paddingLeft = 30.0f;
graph.paddingTop = 30.0f;
graph.paddingRight = 30.0f;
graph.title = nil;
}
-(CGPoint)plotSpace:(CPTPlotSpace *)space willDisplaceBy:(CGPoint)displacement
{
return CGPointMake(displacement.x, 0);
}
-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate
{
if (space == self.scatterGraphPlotSpace) {
if (coordinate == CPTCoordinateY) {
newRange = self.scatterGraphPlotSpace.yRange;
}
}else{
if (coordinate == CPTCoordinateY) {
newRange = ((CPTXYPlotSpace*)space).yRange;
}
}
return newRange;
}
非常感谢
答案 0 :(得分:0)
为delegate
设置self.scatterGraphPlotSpace
。该行代码在您发布的代码中被注释掉。