objective-c - 核心情节: unable to get legend titles in barchart

标签 objective-c ios core-plot

我正在尝试根据这样的示例在条形图中添加图例

 CPTBarPlot* barPlot = [[CPTBarPlot alloc] init];
    barPlot.dataSource = self;
    barPlot.baseValue = CPTDecimalFromString(@"0");
    barPlot.barOffset = CPTDecimalFromFloat(1.0f);
    barPlot.barCornerRadius = 2.0f;
    barPlot.identifier = @"Bar Plot 2";
    [barChart addPlot:barPlot toPlotSpace:plotSpace];


    // Add legend
    CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
    theLegend.numberOfColumns = 1;
    theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
    theLegend.borderLineStyle = [CPTLineStyle lineStyle];
    theLegend.cornerRadius = 5.0;
    CPTMutableTextStyle* legendStyle = [[CPTMutableTextStyle alloc] init];
    legendStyle.fontSize = 18.0f;

    theLegend.textStyle = legendStyle;

barChart.legend = theLegend;

barChart.legendAnchor = CPTRectAnchorRight;
barChart.legendDisplacement = CGPointMake(-50.0, 80.0);

这是在图例中设置标题的方法

 //legend title
    -(NSString *)legendTitleForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index
    {
        if (index == 0) {

        return @"Major";
    }

        if (index == 1) {
        return @"Minor" ;
        }
    if (index == 2) {
        return @"Normal";   }

    return [NSString stringWithFormat:@"Bar  %u", index];
}

但我只能看到标题为“条形图 2”的矩形框中的一行以及标题之前的一个黑色矩形框。 barPlot.identifier 中设置的一个。但是所需的输出是显示三行,以 Major、minor、normal 作为标题。我觉得方法 legendTitleForBarPlot 没有被调用,因为结果是相同的也评论了这个方法之后。

This the sceen capture of the output

最佳答案

尝试使用

更改值
 barChart.legendDisplacement = CGPointMake(-50.0, 80.0);

因为你的x位移太大,我不知道你的图表的范围。 所有酒吧都会出现传说。所以记录数就是你的传奇数。

我从来没有添加过图例,但可以为此提供建议。

关于objective-c - 核心情节: unable to get legend titles in barchart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9890501/

相关文章:

objective-c - 未收到 NSWindowWillCloseNotifications

ios - 为 UIWebView 的白色反弹区域添加颜色

ios - UILabel:背景相关颜色

ios - 将参数传递到 block 中

ios - 如何计算曲线绘制的三点之间的点数?

iphone - 在 objective-c 中改组数组

ios - 滚动表格单元格图像后,快速显示以前的图像。虽然图像 url 显示正确

macos - CorePlot 滚动实时绘图

objective-c - Xcode 4.4.1 & 核心情节 : Apple LLVM compiler error when compiling Core Plot demo apps

iphone - 如何在Graph中自定义CorePlot的绘图区