0
我正在試圖很好地佈局我的條形圖。我希望不超過10個水平條全部錨定到頂部,並設置條寬度和條間距。CorePlot - 條形圖中條形的間距
#define kBarHeight 20
#define kBarSpacing 8
myPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt([self.data count]*(kBarHeight+kBarSpacing)) length:CPTDecimalFromInt([self.data count]*(kBarHeight+kBarSpacing) * -1)];
的底片是因爲我想也讓this effect
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSNumber *num = @0;
switch (fieldEnum) {
case CPTBarPlotFieldBarTip:
if ([plot.identifier isEqual:@"dataset1"]) {
num = [NSNumber numberWithInteger:[self.dataSet1[index] count]];
} else {
num = [NSNumber numberWithInteger:[self.dataSet2[index] count]];
}
break;
default:
num = [NSNumber numberWithInteger:(index*(kBarHeight + kBarSpacing)) + kBarHeight/2 ];
break;
}
return num;
}
我以爲上面會工作,但所有的酒吧都在靠近圖表底部海誓山盟的頂部,並在中途切斷,但它們仍然間隔均勻,不聽我設定的酒吧間距值。 。思考?
編輯:全圖形設置:
#pragma mark -
#pragma mark - Chart behavior
-(void)initPlot {
[self configureGraph];
[self configurePlots];
[self configureAxes];
}
-(void)configureGraph {
CPTGraph *dataSet2Graph = [[CPTXYGraph alloc] initWithFrame:self.dataSet2Graph.bounds];
CPTGraph *dataSet1Graph = [[CPTXYGraph alloc] initWithFrame:self.dataSet1Graph.bounds];
dataSet2Graph.plotAreaFrame.masksToBorder = NO;
dataSet1Graph.plotAreaFrame.masksToBorder = NO;
dataSet2Graph.paddingLeft = 150.0;
dataSet1Graph.paddingLeft = 150.0;
self.dataSet2Graph.hostedGraph = dataSet2Graph;
self.dataSet1Graph.hostedGraph = dataSet1Graph;
CPTXYPlotSpace *dataSet2PlotSpace = (CPTXYPlotSpace *) dataSet2Graph.defaultPlotSpace;
CSGeoStat *stat = (CSGeoStat *) self.dataSet2Data[0]; //Biggest dataSet2 count
dataSet2PlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(stat.count * 1.1)];
dataSet2PlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt([self.dataSet2Data count]) length:CPTDecimalFromInt(-1 * [self.dataSet2Data count])];
CPTXYPlotSpace *dataSet1PlotSpace = (CPTXYPlotSpace *) dataSet1Graph.defaultPlotSpace;
stat = (CSGeoStat *) self.dataSet1Data[0]; //Biggest tz count
dataSet1PlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(stat.count * 1.1)];
dataSet1PlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt([self.dataSet1Data count]*(kBarHeight+kBarSpacing)) length:CPTDecimalFromInt([self.dataSet1Data count]*(kBarHeight+kBarSpacing) * -1)];
[self.dataSet2Graph setFrame:CGRectMake(self.dataSet2Graph.frame.origin.x,
self.dataSet2Graph.frame.origin.y,
self.dataSet2Graph.frame.size.width,
(int)([self.dataSet2Data count]*(kBarHeight+kBarSpacing)*1.2))];
[self.dataSet1Graph setFrame:CGRectMake(self.dataSet1Graph.frame.origin.x,
self.dataSet1Graph.frame.origin.y,
self.dataSet1Graph.frame.size.width,
(int)([self.dataSet1Data count]*(kBarHeight+kBarSpacing)*1.2))];
}
-(void)configurePlots {
CPTBarPlot *dataSet2Plot = [[CPTBarPlot alloc] init];
dataSet2Plot.barsAreHorizontal = YES;
dataSet2Plot.barCornerRadius = CPTFloat(1.0);
dataSet2Plot.identifier = @"CountryPlot";
dataSet2Plot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:19/255 green:221/255 blue:187/255 alpha:1]];
CPTBarPlot *dataSet1Plot = [[CPTBarPlot alloc] init];
dataSet1Plot.barsAreHorizontal = YES;
dataSet1Plot.barCornerRadius = CPTFloat(1.0);
dataSet1Plot.identifier = @"TimeZonePlot";
dataSet1Plot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:19/255 green:221/255 blue:188/255 alpha:1]];
CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init];
barLineStyle.lineColor = [CPTColor clearColor];
barLineStyle.lineWidth = 0.5;
CPTGraph *dataSet2Graph = self.dataSet2Graph.hostedGraph;
CPTGraph *dataSet1Graph = self.dataSet1Graph.hostedGraph;
dataSet2Plot.dataSource = self;
dataSet2Plot.delegate = self;
dataSet2Plot.barWidth = CPTDecimalFromDouble(kBarHeight);
dataSet2Plot.barWidthsAreInViewCoordinates = YES;
dataSet2Plot.baseValue = CPTDecimalFromString(@"0");
dataSet2Plot.lineStyle = barLineStyle;
dataSet2Plot.barOffset = CPTDecimalFromFloat(0.25f);
dataSet2Plot.labelOffset = -5.0;
[dataSet2Graph addPlot:dataSet2Plot toPlotSpace:dataSet2Graph.defaultPlotSpace];
dataSet1Plot.dataSource = self;
dataSet1Plot.delegate = self;
dataSet1Plot.baseValue = CPTDecimalFromString(@"0");
dataSet1Plot.barWidth = CPTDecimalFromDouble(kBarHeight);
dataSet1Plot.barWidthsAreInViewCoordinates = YES;
dataSet1Plot.lineStyle = barLineStyle;
dataSet1Plot.labelOffset = -5.0;
dataSet1Plot.barOffset = CPTDecimalFromFloat(0.25f);
[dataSet1Graph addPlot:dataSet1Plot toPlotSpace:dataSet1Graph.defaultPlotSpace];
}
-(void)configureAxes {
CPTXYAxisSet *dataSet2AxisSet = (CPTXYAxisSet *) self.dataSet2Graph.hostedGraph.axisSet;
CPTXYAxis *x = dataSet2AxisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.axisLineStyle = nil;
CPTXYAxis *y = dataSet2AxisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.axisLineStyle = nil;
NSMutableArray *customLabels = [[NSMutableArray alloc]init];
[self.dataSet2Data enumerateObjectsUsingBlock:^(CSGeoStat *stat, NSUInteger idx, BOOL *stop) {
NSString *labelText = stat.term;
CPTMutableTextStyle *textStyle = [y.labelTextStyle mutableCopy];
textStyle.color = [CPTColor whiteColor];
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:labelText textStyle:textStyle];
label.tickLocation = CPTDecimalFromInt((idx*(kBarHeight + kBarSpacing)) + kBarHeight/2);
label.offset = y.labelOffset + y.majorTickLength;
label.rotation = M_PI * 2;
[customLabels addObject:label];
}];
y.axisLabels = [NSSet setWithArray:customLabels];
CPTXYAxisSet *dataSet1AxisSet = (CPTXYAxisSet *) self.dataSet1Graph.hostedGraph.axisSet;
CPTXYAxis *x2 = dataSet1AxisSet.xAxis;
x2.labelingPolicy = CPTAxisLabelingPolicyNone;
x2.axisLineStyle = nil;
CPTXYAxis *y2 = dataSet1AxisSet.yAxis;
y2.labelingPolicy = CPTAxisLabelingPolicyNone;
y2.axisLineStyle = nil;
NSMutableArray *customLabels2 = [[NSMutableArray alloc]init];
[self.dataSet1Data enumerateObjectsUsingBlock:^(CSGeoStat *stat, NSUInteger idx, BOOL *stop) {
NSString *labelText = stat.term;
CPTMutableTextStyle *textStyle = [y2.labelTextStyle mutableCopy];
textStyle.color = [CPTColor whiteColor];
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:labelText textStyle:textStyle];
label.tickLocation = CPTDecimalFromInt((idx*(kBarHeight + kBarSpacing)) + kBarHeight/2);
label.offset = y2.labelOffset + y2.majorTickLength;
label.rotation = M_PI * 2;
[customLabels2 addObject:label];
}];
y2.axisLabels = [NSSet setWithArray:customLabels2];
}
從數據源返回的數字是您期望的嗎?如果是這樣,問題出現在軸或繪圖設置中。 –
是的,我認爲是。我現在已經附加完整的圖表設置 –
兩個託管視圖是否正確放置在其父視圖中?尺寸是否正確? (託管視圖將使託管的圖形適合託管視圖。)由於託管視圖和圖形具有相同的名稱,很難說出發生了什麼。兩個情節空間的'yRange'是不同的。那是故意的嗎? –