[英]Core Plot: Axis labels imposes on each other
我正在使用自定義標簽,當x軸標簽強加於另一個x軸標簽時,我遇到了問題,當用戶縮小散點圖(實時)時,我無法找到隱藏這些標簽的方法。
請參閱下面的打印屏幕:我想隱藏“2012年8月”標簽。
我怎樣才能做到這一點?
以下是我使用的代碼:
CPTXYAxis *x = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
x.majorIntervalLength = CPTDecimalFromInteger(150);
x.minorTicksPerInterval = 5;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0f];
x.labelingPolicy=CPTAxisLabelingPolicyNone;
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[objects count]];
NSMutableSet *xMajorLocations = [NSMutableSet setWithCapacity:[objects count]];
for (NSInteger i = 0; i < [objects count]; i++) {
NSManagedObject *theLine = [objects objectAtIndex:i];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *sPeriodText = @"";
[dateFormatter setDateFormat:@"MMMM yyyy"];
sPeriodText = [dateFormatter stringFromDate:[theLine valueForKey:@"period_start"]];
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:sPeriodText textStyle:labelTextStyle];
newLabel.tickLocation = CPTDecimalFromInteger(labelLocation++);
newLabel.offset = x.labelOffset + x.majorTickLength;
[customLabels addObject:newLabel];
[xMajorLocations addObject:[NSNumber numberWithFloat:labelLocation-1]];
}
x.axisLabels = [NSSet setWithArray:customLabels];
x.majorTickLocations = xMajorLocations;
謝謝!
PS我試圖使用CPTAxis的labelExclusionRanges,但它不適用於自定義標簽。
了解如何制作它。 因為我使用日期格式作為x軸標簽,我需要使用CPTTimeFormatter + preferredNumberOfMajorTicks,而不是自定義標簽!
這是下面的代碼:
...
NSManagedObject *theLineFirst = [objects objectAtIndex:0];
NSManagedObject *theLineLast = [objects objectAtIndex:[objects count]-1];
NSDate *refDate = [NSDate dateWithTimeInterval:0 sinceDate:[theLineFirst valueForKey:@"period_start"]];
NSTimeInterval secondsBetween = [[theLineLast valueForKey:@"period_start"] timeIntervalSinceDate:[theLineFirst valueForKey:@"period_start"]];
NSTimeInterval onePart = secondsBetween/[objects count];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.delegate = self;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(-onePart) length:CPTDecimalFromInteger(secondsBetween+onePart*2)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(minCData) length:CPTDecimalFromInteger(abs(maxCData-minCData))];
plotSpace.globalXRange = plotSpace.xRange;
plotSpace.globalYRange = plotSpace.yRange;
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
x.majorIntervalLength = CPTDecimalFromInteger(5);
x.minorTicksPerInterval = 0;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0f];
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
// added for date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM yyyy"];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = refDate;
axisSet.xAxis.labelFormatter = timeFormatter;
x.preferredNumberOfMajorTicks = 4;
...
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [objects count];
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSManagedObject *theLineFirst = [objects objectAtIndex:0];
NSManagedObject *theLine = [objects objectAtIndex:index];
NSTimeInterval secondsBetween = [[theLine valueForKey:@"period_start"] timeIntervalSinceDate:[theLineFirst valueForKey:@"period_start"]];
switch (fieldEnum) {
case CPTScatterPlotFieldX:
return [NSNumber numberWithDouble:secondsBetween];
case CPTScatterPlotFieldY:
return [NSNumber numberWithDouble:[[theLine valueForKey:@"cdata"] doubleValue]];
}
return [NSDecimalNumber zero];
}
就這樣!
Core Plot尚未自動處理此問題。 您必須使用繪圖區域的可用寬度和標簽大小來決定何時顯示每個刻度線的標簽,並在適當的位置省略自定義標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.