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

2 回答 2

3

当您只执行 [_graph addPlot:volumePlot] 而不是 [_graph addPlot toPlotSpace:volumePlotSpace] 时会发生什么?

我在想你可能已经使用所有这些不同的情节空间添加了一些额外的限制,这使得你的两个情节都不会出现。

老实说,我并不完全确定,我也刚刚在学习 Core Plot,我提出的大多数解决方案都是大量玩耍和谷歌搜索的结果。如果我想到别的东西,我会编辑这篇文章。

于 2012-07-12T19:38:22.533 回答
1

您永远不会为体积绘图空间设置绘图空间范围。它仍然具有 [0, 1] 的默认 x 和 y 范围。

于 2012-07-13T13:28:33.950 回答