1
votes

I'm learning Core Plot and I've come to draw the following graph of the sinus function with Core Plot 1.2:

Sinus Plot with Core Plot

As you see, the axes aren't drawn, even if I configure my axes as in the following snippet.

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;

My question is: Why the axes aren't shown? How can I force CorePlot to draw them?.

Here's the full relevant code:

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

1 Answers

3
votes

Adding the following line:

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

to viewDidLoad solved the issue.

My explanation is that the axis set is a property of the CPTGraphHostingView (here self.graph), and not of the plot. I was changing the plot's layer background color instead of changing the graph's layer background color. Since the axes still had the same color as the background of their containing layer, they weren't visible.

For future reference, a simple working version of viewDidLoad, is the following:

-(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];

}

I hope this will help CorePlot rookies like my current self.