Core Plot: Two plots sharing the same x axis
Asked Answered
L

2

6

I am trying to set up two charts similar to the AAPlot sample provided with Core Plot. It basically consists of a stock chart, which is shown almost correctly and a second chart (volume chart), which should be placed directly below the stock chart. Both charts should share the same x axis.

Unfortunately the volume chart's data is not being plotted in my app.

Even though I intensely compared the sample source code with mine, I do not find the source of my error.

Could you please point me to right direction?

This is my code:

- (void)configureChart
{
    self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
    self.hostView.hostedGraph = self.graph;
    self.graph.paddingLeft = 0.0f;
    self.graph.paddingTop = 0.0f;
    self.graph.paddingRight = 0.0f;
    self.graph.paddingBottom = 0.0f;
    self.graph.axisSet = nil;

    // 2 - Set up text style
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.color = [CPTColor grayColor];
    textStyle.fontName = @"Helvetica-Bold";
    textStyle.fontSize = 16.0f;

    // 3 - Configure title
    NSString *title = self.issue.company.companyName;
    _graph.title = title;    
    _graph.titleTextStyle = textStyle;
    _graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;    
    _graph.titleDisplacement = CGPointMake(0.0f, -12.0f);      

    // 4 - Set theme
    [self.graph applyTheme:[CPTTheme themeNamed:kCPTStocksTheme]];

    _graph.plotAreaFrame.cornerRadius  = 0.0f;
    CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
    borderLineStyle.lineColor           = [CPTColor whiteColor];
    borderLineStyle.lineWidth           = 2.0f;
    _graph.plotAreaFrame.borderLineStyle = borderLineStyle;


    // Axes
    CPTXYAxisSet *xyAxisSet        = (id)self.graph.axisSet;
    CPTXYAxis *xAxis               = xyAxisSet.xAxis;
    CPTMutableLineStyle *lineStyle = [xAxis.axisLineStyle mutableCopy];
    lineStyle.lineCap   = kCGLineCapButt;
    xAxis.axisLineStyle = lineStyle;
    xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;

    CPTXYAxis *yAxis = xyAxisSet.yAxis;
    yAxis.axisLineStyle = nil;

    // OHLC plot
    CPTMutableLineStyle *whiteLineStyle = [CPTMutableLineStyle lineStyle];
    whiteLineStyle.lineColor = [CPTColor whiteColor];
    whiteLineStyle.lineWidth = 1.0f;
    CPTTradingRangePlot *ohlcPlot = [[CPTTradingRangePlot alloc] initWithFrame:self.graph.bounds];
    ohlcPlot.identifier = @"OHLC";
    ohlcPlot.lineStyle  = whiteLineStyle;
    CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
    whiteTextStyle.color    = [CPTColor whiteColor];
    whiteTextStyle.fontSize = 8.0;
    ohlcPlot.labelTextStyle = whiteTextStyle;
    ohlcPlot.labelOffset    = 5.0;
    ohlcPlot.stickLength    = 2.0f;
    ohlcPlot.dataSource     = self;
    ohlcPlot.plotStyle      = CPTTradingRangePlotStyleOHLC;
    [self.graph addPlot:ohlcPlot];

    // Add volume plot space
    CPTXYPlotSpace *volumePlotSpace = [[CPTXYPlotSpace alloc] init];
    volumePlotSpace.identifier = @"Volume";
    [_graph addPlotSpace:volumePlotSpace];

    CPTBarPlot *volumePlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor whiteColor] horizontalBars:NO];
    volumePlot.dataSource = self;

    lineStyle            = [volumePlot.lineStyle mutableCopy];
    lineStyle.lineColor  = [CPTColor whiteColor];
    volumePlot.lineStyle = lineStyle;

    volumePlot.fill       = nil;
    volumePlot.barWidth   = CPTDecimalFromFloat(1.0f);
    volumePlot.identifier = @"Volume Plot";
    [_graph addPlot:volumePlot toPlotSpace:volumePlotSpace];


    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.graph.defaultPlotSpace;
    [plotSpace scaleToFitPlots:[self.graph allPlots]];
}
Limitative answered 12/7, 2012 at 13:49 Comment(0)
B
1

You never set the plot space ranges for the volume plot space. It still has the default x and y ranges of [0, 1].

Bergeman answered 13/7, 2012 at 13:28 Comment(0)
N
3

What happens when you just do [_graph addPlot:volumePlot] instead of [_graph addPlot toPlotSpace:volumePlotSpace]?

I'm thinking that you may have added some extra restraints using all of those various plot spaces and that is keeping both of your plots from showing up.

I'm not entirely sure though to be honest, I've just been learning Core Plot as well, most solutions I've come up are a result of lots of playing around and googling. I'll edit this post if I think of something else.

Nagging answered 12/7, 2012 at 19:38 Comment(4)
The problem remains the same when changing the line of code. Basically, I just ported the source code from the AAPlot sample, but it does not work in my set-up.Limitative
I know there are some differences between how the plots are required to be set up, but you dont initialize the volume plot with the bounds of the graph. Also, have you ran through the debugger, does your volumePlot actually exist or contain information?Nagging
The data for the volumePlot does exist, the datasource delivers the correct data. I have set up the volume plot (in my code: valuation plot) the same way as in the Core Plot sample.Limitative
I'm sure the data for volumePlot exists, because you are using the same data as in the OHLC plot, right? But does volumePlot actually get initialized correctly, or is the plot itself broken? You need to try to determine if the error is in how you're attempting to display the volume plot, or with the plot itself. Just because its the same as the example doesn't mean its going to work in your situation. I would be surprised if you set up your application exactly like the example.Nagging
B
1

You never set the plot space ranges for the volume plot space. It still has the default x and y ranges of [0, 1].

Bergeman answered 13/7, 2012 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.