我正在尝试使用核心绘图绘制折线图,并且遇到x轴标签问题。 这是我的代码:
graph = [[CPTXYGraph alloc] initWithFrame:self._graphView.bounds];
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[graph applyTheme:theme];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self._graphView;
[hostingView setHostedGraph:graph];
[self.view addSubview:hostingView];
graph.paddingLeft = 10.0;
graph.paddingTop = 10.0;
graph.paddingRight = 10.0;
graph.paddingBottom = 10.0;
graph.plotAreaFrame.paddingLeft = 55.0;
graph.plotAreaFrame.paddingTop = 50.0;
graph.plotAreaFrame.paddingRight = 20.0;
graph.plotAreaFrame.paddingBottom = 90.0;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt([examValues count])];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromDouble([self getYRange] + 10)];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;
CPTXYAxisSet *axisSet = (id)graph.axisSet;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[examValues count]];
static CPTMutableTextStyle *labelTextStyle = nil;
labelTextStyle = [[CPTMutableTextStyle alloc] init];
labelTextStyle.color = [CPTColor blackColor];
labelTextStyle.fontSize = 10.0f;
int index = 1;
for (SmartAssetInspectionResultHistoryDTO *result in examValues) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:result._inspectiondate textStyle:labelTextStyle];
newLabel.tickLocation = CPTDecimalFromInt(index);
newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
newLabel.rotation = M_PI / 4;
[customLabels addObject:newLabel];
[newLabel release];
index ++;
}
axisSet.xAxis.axisLabels = [NSSet setWithArray:customLabels];
axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"20");
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromInt(0);
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.labelOffset = 3.0f;
CPTScatterPlot *plot = [[[CPTScatterPlot alloc] init] autorelease];
CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle];
plot.identifier = @"Date Plot";
dataLineStyle.lineWidth = 1.0f;
dataLineStyle.lineColor = [CPTColor redColor];
plot.dataLineStyle = dataLineStyle;
plot.dataSource = self;
CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(5.0, 5.0);
plot.plotSymbol = greenCirclePlotSymbol;
[graph addPlot:plot];
一些意见:
examValues <--- A NSMutableArray storing some data .
SmartAssetInspectionResultHistoryDTO <--- Detail info object stored in examValues .
result._inspectiondate <--- Date info I want to display in the x axis' labels .
我的麻烦:x轴下方没有显示日期,但浮动值如“1.0”,“2.0”...... 谁能告诉我问题出在哪里?
答案 0 :(得分:1)
只需将您的labelsPolicy更改为none即可。
“axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone
”