2013-12-20 57 views
0

我附上了我的圖形的屏幕截圖。Core-Plot正確格式化圖形

core-plot

我需要解決以下問題。

1)該圖表從60開始。所以我不再需要x軸下的0-60 y軸線。如何刪除該部分? Y軸應該從60開始向上。

2)如何添加x軸的勾號。由於我的標籤傾向於我需要正確添加它。

這裏是我的代碼

// Create graph from theme 
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; 
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; 
[graph applyTheme:theme]; 
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view; 
hostingView.collapsesLayers = NO; // Setting to YES reduces GPU memory usage, but can slow drawing/scrolling 
hostingView.hostedGraph  = graph; 

graph.paddingLeft = 10.0; 
graph.paddingTop = 10.0; 
graph.paddingRight = 10.0; 
graph.paddingBottom = 10.0; 

// Setup plot space 
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
plotSpace.allowsUserInteraction = YES; 
plotSpace.yRange    = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-100.0) length:CPTDecimalFromFloat(500.0)]; 
plotSpace.xRange    = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-2.25) length:CPTDecimalFromFloat(15.0)]; 

CPTMutableLineStyle *xLineStyle = [CPTMutableLineStyle lineStyle]; 
xLineStyle.miterLimit  = 1.0f; 
xLineStyle.lineWidth   = 3.0f; 
xLineStyle.lineColor = [CPTColor whiteColor]; 
// Axes 
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 
CPTXYAxis *x   = axisSet.xAxis; 
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(100)]; 
x.majorIntervalLength   = CPTDecimalFromString(@"1"); 
x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(60.0); 
x.titleLocation    = CPTDecimalFromFloat(0.5f); 
x.titleOffset     = 5.0f; 

x.labelingPolicy = CPTAxisLabelingPolicyNone; 
NSMutableArray *customTickLocations = [[NSMutableArray alloc] init]; 

if (selectedIndex == 0) { 
    self.xAxisLabels = [[[NSMutableArray alloc] init] autorelease]; 
    for (int i = 0; i < [items count]; i++) { 
     WeightDTO *weightDTO = [items objectAtIndex:i]; 

     [xAxisLabels addObject:weightDTO.dateAdded]; 
     [customTickLocations addObject:[NSString stringWithFormat:@"%d", i]]; 
    } 
} 

NSUInteger labelLocation  = 0; 
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]]; 
for (NSNumber *tickLocation in customTickLocations) { 
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle]; 
    newLabel.tickLocation = [tickLocation decimalValue]; 
    newLabel.offset  = x.labelOffset + x.majorTickLength; 
    newLabel.rotation  = M_PI/4; 
    [customLabels addObject:newLabel]; 
    [newLabel release]; 
} 

x.axisLabels = [NSSet setWithArray:customLabels]; 

CPTXYAxis *y = axisSet.yAxis; 
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(500)]; 
y.majorIntervalLength   = CPTDecimalFromString(@"30"); 
y.minorTicksPerInterval  = 5; 
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0); 

y.delegate    = self; 

// Create a blue plot area 
CPTScatterPlot *boundLinePlot = [[[CPTScatterPlot alloc] init] autorelease]; 
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
lineStyle.miterLimit  = 1.0f; 
lineStyle.lineWidth   = 3.0f; 
lineStyle.lineColor   = [CPTColor redColor]; 
boundLinePlot.dataLineStyle = lineStyle; 
boundLinePlot.identifier = @"Blue Plot"; 
boundLinePlot.dataSource = self; 
[graph addPlot:boundLinePlot]; 

// Do a blue gradient 
CPTColor *areaColor1  = [CPTColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8]; 
CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]]; 
areaGradient1.angle = -90.0f; 
boundLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue]; 

// Add plot symbols 
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle]; 
symbolLineStyle.lineColor = [CPTColor blackColor]; 
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
plotSymbol.fill   = [CPTFill fillWithColor:[CPTColor blueColor]]; 
plotSymbol.lineStyle  = symbolLineStyle; 
plotSymbol.size   = CGSizeMake(10.0, 10.0); 
boundLinePlot.plotSymbol = plotSymbol; 

回答

1

我假設你使用的是像configureHost,configurePlot,configureGraph,configureAxes方法...

你的第一個問題: 在configureGraph你可以這樣做:

// Set up plot space 
CGFloat xMin = 0.0f; 
CGFloat xMax = [dates count]; 
CGFloat yMin; 
CGFloat yMax; 

if (minAverage > 50) { 
    yMin = minAverage-50; 
    yMax = maxAverage+20; 
} else { 
    yMin = 0; 
    yMax = maxAverage+20; 
} 
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin) length:CPTDecimalFromFloat(xMax)]; 
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax-yMin)]; 

我已經計算了最小值和最大值之前(minAverage,maxAverage)並在每一端添加了一些空間。在X-AX配置了[日期數],在我的案件30

你的第二個問題: 在你的方法configureAxes做(標籤和蜱計算與CGFloat的dateCount開始了...)

// 3 - Configure the x-axis 
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet; 
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone; 
axisSet.xAxis.title = @"Day of the month"; 
axisSet.xAxis.titleTextStyle = axisTitleStyle; 
axisSet.xAxis.titleOffset = 15.0f; 

if (minAverage > 50) { 
    axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInt(minAverage-50); 
} 

axisSet.xAxis.axisLineStyle = axisLineStyle; 

axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone; 
axisSet.xAxis.labelTextStyle = axisTextStyle; 
axisSet.xAxis.majorTickLineStyle = axisLineStyle; 
axisSet.xAxis.majorTickLength = 2.0f; 
axisSet.xAxis.minorTickLength = 0; 
axisSet.xAxis.tickDirection = CPTSignNone;// CPTSignPositive;// CPTSignNegative; 

CGFloat dateCount = [sortedWaterlevels count]; 
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount]; 
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount]; 
NSInteger i = 0; 
for (NSString *date in dates) { 
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date textStyle:axisSet.xAxis.labelTextStyle]; 
    CGFloat location = i++; 
    location = location-0.5; 
    label.tickLocation = CPTDecimalFromCGFloat(location); //verschieben label 
    label.offset = axisSet.xAxis.majorTickLength; 
    if (i == 2 || i == 8 || i == 15 || i == 22 || i == 29) { 
     if (label) { 
      [xLabels addObject:label]; 
      [xLocations addObject:[NSNumber numberWithFloat:location]]; 
     } 
    } else { 
     if (label) { 
      [xLocations addObject:[NSNumber numberWithFloat:location]]; 
     } 
    } 
} 
axisSet.xAxis.axisLabels = xLabels; 
axisSet.xAxis.majorTickLocations = xLocations; 

顯示最近30天的滴答時間,而日期僅在從第二天開始的第7天打印。

+0

對於第一個它爲y.visibleRange而不是plotSpace.yRange工作。勾號也工作。謝謝。 – Dilshan

+0

不客氣:-) – thorb65