Core Plot未显示散点图线

时间:2013-07-30 16:11:20

标签: ios graph core-plot scatter-plot

我正在尝试使用Core Plot显示一个简单的散点图,但无论出于何种原因,都没有显示任何线条。我的委托方法调用(numberOfRecordsForPlot:numberForPlot:field:recordIndex:)。以下是我正在设置的图表:

CPTGraphHostingView* extendedHost = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, extendedView.frameBottom, self.view.frameWidth, roomForScrollingComponentAndGraph - extendedForecastView.frameHeight)];
[extendedHost setBackgroundColor:backgroundColor(1.f)];
[self.view addSubview:extendedHost];

CPTXYGraph* extendedGraph = [[CPTXYGraph alloc] initWithFrame:extendedHost.bounds];
extendedGraph.paddingBottom = graphPadding;
extendedGraph.paddingLeft = graphPadding;
extendedGraph.paddingRight = graphPadding;
extendedGraph.paddingTop = graphPadding;

CPTXYPlotSpace* extendedPlotSpace = (CPTXYPlotSpace*)extendedGraph.defaultPlotSpace;
extendedPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(20)];
extendedPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(100)];

[extendedGraph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];

CPTXYAxisSet* extendedAxisSet = (CPTXYAxisSet*)extendedGraph.axisSet;

CPTXYAxis* extendedX = extendedAxisSet.xAxis;
extendedX.minorTicksPerInterval = 0;
extendedX.titleTextStyle = style;
extendedX.orthogonalCoordinateDecimal = CPTDecimalFromInt(0);

NSMutableArray* extendedMajorTicks = [[NSMutableArray alloc] init];
for(int idx = 0; idx < 14; idx++) {
    [extendedMajorTicks addObject:@(idx)];
}
extendedX.majorTickLocations = [NSSet setWithArray:extendedMajorTicks];
extendedX.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;
extendedX.delegate = self;

CPTXYAxis* extendedY = extendedAxisSet.yAxis;
extendedY.preferredNumberOfMajorTicks = 4;
extendedY.minorTicksPerInterval = 1;
extendedY.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
extendedY.titleTextStyle = style;
extendedY.orthogonalCoordinateDecimal = CPTDecimalFromInt(0);
extendedY.delegate = self;

CPTScatterPlot* extendedPlot = [[CPTScatterPlot alloc] init];
extendedPlot.delegate = self;

CPTMutableLineStyle *extendedLineStyle = [CPTMutableLineStyle lineStyle];
extendedLineStyle.lineJoin = kCGLineJoinRound;
extendedLineStyle.lineCap = kCGLineCapRound;
extendedLineStyle.miterLimit = 2.f;
extendedLineStyle.lineWidth = 2.f;
extendedLineStyle.lineColor = [CPTColor colorWithCGColor:[UIColor whiteColor].CGColor];

extendedPlot.dataLineStyle = extendedLineStyle;
extendedPlot.dataSource = self;
extendedPlot.title = @"Extended";
_extendedPlot = extendedPlot;

[extendedGraph addPlot:extendedPlot];

_extendedGraph = extendedGraph;

[extendedHost setHostedGraph:_extendedGraph];

我觉得这是一个很小的东西我很想念,因为我之前在其他项目中使用过Core Plot而且我已经比较了我的代码而且我认为我没有遗漏任何重要的东西,但唉,我似乎是

1 个答案:

答案 0 :(得分:2)

绘图的绘图空间(extendedPlotSpace)为nil,直到绘图添加到图表中。将绘图空间设置移动到绘图是图形的一部分之后,或者使用graph.defaultPlotSpace代替。