gpt4 book ai didi

ios - Core Plot 不画轴,为什么?

转载 作者:行者123 更新时间:2023-12-01 19:11:08 25 4
gpt4 key购买 nike

我正在学习 Core Plot,我已经用 Core Plot 1.2 绘制了下图的 sinus 函数:

Sinus Plot with Core Plot

如您所见,即使我按照以下代码段配置轴,也不会绘制轴。

CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor blackColor];

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
axisSet.xAxis.axisLineStyle = axisLineStyle;
axisSet.yAxis.axisLineStyle = axisLineStyle;

我的问题是: 为什么没有显示坐标轴?如何强制 CorePlot 绘制它们? .

这是完整的相关代码:
-(void)viewDidLoad
{
[super viewDidLoad];

self.graph = [[CPTXYGraph alloc] initWithFrame:self.view.frame];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
hostingView.hostedGraph = self.graph;


CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-4) length:CPTDecimalFromFloat(8)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-2.0) length:CPTDecimalFromFloat(4.0)];


CPTScatterPlot *sinusPlot = [[CPTScatterPlot alloc] initWithFrame:self.graph.frame];
sinusPlot.dataSource = self;
sinusPlot.backgroundColor = [CPTColor whiteColor].cgColor;

CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor blackColor];

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
axisSet.xAxis.axisLineStyle = axisLineStyle;
axisSet.yAxis.axisLineStyle = axisLineStyle;

[self.graph addPlot:sinusPlot];


}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return 101;
}


-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
{
static double pi = 3.14159;
double x = pi*((((double)idx) - 51.0)/50.0);

if (fieldEnum == CPTScatterPlotFieldX)
return [NSNumber numberWithDouble:x];
else
return [NSNumber numberWithDouble:sin(x + ((x<0)?2*pi:0))];
}

最佳答案

添加以下行:

self.graph.backgroundColor = [CPTColor whiteColor].cgColor;

viewDidLoad解决了这个问题。

我的解释是轴集是 CPTGraphHostingView 的属性。 (这里 self.graph ),而不是情节。我正在更改绘图的图层背景颜色,而不是更改图形的图层背景颜色。由于坐标区的颜色仍与其包含层的背景颜色相同,因此它们不可见。

为了将来引用, viewDidLoad 的简单工作版本, 如下:
-(void)viewDidLoad
{
[super viewDidLoad];

self.graph = [[CPTXYGraph alloc] initWithFrame:self.view.frame];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
hostingView.hostedGraph = self.graph;

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-4) length:CPTDecimalFromFloat(8)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-2.0) length:CPTDecimalFromFloat(4.0)];

CPTScatterPlot *sinusPlot = [[CPTScatterPlot alloc] initWithFrame:self.graph.frame];
sinusPlot.dataSource = self;

self.graph.backgroundColor = [CPTColor whiteColor].cgColor;

[self.graph addPlot:sinusPlot];

}

我希望这会帮助像我现在的自己一样的 CorePlot 新手。

关于ios - Core Plot 不画轴,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16533908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com