核心绘图:共享相同X轴的两个绘图

6
我正在尝试设置两个与Core Plot提供的AAPlot示例类似的图表。它基本上由一个股票图表组成,几乎正确显示,以及第二个图表(成交量图表),应该直接放置在股票图表下方。两个图表应该共享同一X轴。
不幸的是,我的应用程序中未绘制出成交量图表的数据。
即使我强烈比较了示例源代码和我的代码,也找不到错误源头。
请问你能指点我一下吗?
以下是我的代码:
- (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]];
}
2个回答

3
当你只使用[_graph addPlot:volumePlot]而不是[_graph addPlot toPlotSpace:volumePlotSpace]会发生什么?
我认为你可能使用了所有这些不同的绘图空间添加了一些额外的限制,这使得两个绘图都无法显示出来。
但说实话,我并不完全确定。我也在学习Core Plot,大部分解决方案都是通过大量尝试和搜索获得的。如果我想到了其他内容,我会编辑这篇文章。

当改变代码行时,问题仍然存在。基本上,我只是从AAPlot示例中移植了源代码,但它在我的设置中无法工作。 - AlexR
我知道在绘制图形时有一些不同之处,但是你不需要使用图形的边界来初始化体积图。另外,你是否通过调试器运行过程序?你的volumePlot是否真实存在或包含信息? - Karoly S
volumePlot的数据确实存在,数据源提供了正确的数据。我已经按照Core Plot示例中的方式设置了volume plot(在我的代码中称为valuation plot)。 - AlexR
我确定volumePlot的数据是存在的,因为你使用的是与OHLC图相同的数据,对吗? 但是,volumePlot是否被正确初始化,或者说这个图本身就有问题呢?你需要尝试确定错误是在你如何尝试显示音量图还是图形本身上。仅仅因为它与示例相同,并不意味着它会在你的情况下工作。如果您设置应用程序与示例*完全相同,我会感到惊讶。 - Karoly S

1

您没有为音量绘图空间设置绘图范围。它仍然具有默认的x和y范围[0,1]。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接