Core Plot: two sections that share the same x axis

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

Unfortunately, the volume chart data is not displayed in my application.

Despite the fact that I intensively compared the source code of the sample with mine, I could not find the source of my error.

Could you point me in the 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]]; } 
+6
source share
2 answers

You never set spatial ranges for a spatial volume section. It still has x and y ranges by default [0, 1].

+1
source

What happens when you simply execute [_graph addPlot: volumePlot] instead of [_graph addPlot toPlotSpace: volumePlotSpace]?

I think that you may have added some additional restrictions using all these different plot spaces, and this does not allow you to display both of your plots.

I'm not quite sure, although, to be honest, I just studied Core Plot, most of the solutions I came up with are the result of many games and search queries. I will edit this post if I think about something else.

+3
source

Source: https://habr.com/ru/post/920321/


All Articles