【问题标题】:Core Plot not showing scatter plot line核心图未显示散点图线
【发布时间】:2013-07-30 21:38:06
【问题描述】:

我正在尝试使用 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,并且我比较了我的代码,我认为我没有错过任何重要的东西,但是唉,我似乎是.

【问题讨论】:

  • 绘图空间范围是否包含您的绘图数据? x 和 y 的默认范围是 [0, 1]。
  • 我之前没有这样做,但是在添加了绘图空间范围之后(我在上面编辑了我的代码;范围是在靠近底部创建绘图之后设置的),我没有看到任何变化。数据各不相同,但我知道它会在我发布的范围内。

标签: ios graph core-plot scatter-plot


【解决方案1】:

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

【讨论】:

  • 好吧,正确设置绘图的绘图空间修复了我的刻度位置问题;刻度设置正确,但绘图区域并没有集中在足够宽的区域以看到它们。不过,我的情节仍然没有显示。我已经发布了显示刻度的新代码。
  • 什么是lineStyle?如果是nil,则绘图不会绘制数据线。
  • lineStyle 是我在两个绘图之间共享的 CPTMutableLineStyle,因此它的定义实际上不在我的代码 sn-p 中。如您现在所见,我已更改代码以对两个绘图使用单独的线型。 NSLog-ing 线型显示它不是nil。
  • 出于好奇,我尝试在我的图表中使用虚拟数据,而不是来自我们 Web 服务的数据。我遇到的问题是因为起初我们的数据是nil,所以所有的y值都是0。加载数据时,我在绘图上调用reloadData,但数据不是重新加载。
  • 你是从主线程调用-reloadData吗?你的数据源方法又被调用了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多