我正在尝试根据这样的示例在条形图中添加图例
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
没有被调用,因为结果是相同的也评论了这个方法之后。
最佳答案
尝试使用
更改值 barChart.legendDisplacement = CGPointMake(-50.0, 80.0);
因为你的x位移太大,我不知道你的图表的范围。 所有酒吧都会出现传说。所以记录数就是你的传奇数。
我从来没有添加过图例,但可以为此提供建议。
关于objective-c - 核心情节: unable to get legend titles in barchart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9890501/