1

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

带核心图的窦图

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

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))];
}
4

1 回答 1

3

添加以下行:

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

viewDidLoad解决这个问题。

我的解释是轴集是CPTGraphHostingView(here 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 新手。

于 2013-05-15T16:43:12.733 回答