0

我正在尝试使用 core-plot 绘制折线图并且遇到 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' ...谁能告诉我问题出在哪里?

4

1 回答 1

1

只需尝试将您的 labelingPolicy 更改为 none..“ axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone

于 2012-07-26T07:22:23.450 回答