我正在尝试根据下面的示例在我的条形图中添加图例
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];
}但我只能看到标题为'Bar Plot 2‘的矩形框中的一行和标题前的一个黑色矩形框。在barPlot.identifier.But中设置的一项所需的输出是显示三行,标题为主要、次要、正常。我感觉legendTitleForBarPlot方法没有被调用,因为注释此方法后的结果也是一样的。

发布于 2012-03-28 14:06:57
尝试使用以下命令更改值
barChart.legendDisplacement = CGPointMake(-50.0, 80.0);因为你的x位移太大了,我不知道你的图形的范围。此外,在所有酒吧都会看到传说。因此,记录的数量就是您的图例编号。
我没有添加任何时间的图例,但可以给出一个建议。
发布于 2012-12-13 04:32:31
我为提出了另一个相当古老的问题而道歉,我理解现在帮助TechnocraT可能已经太晚了。
然而,我也遇到了同样的问题,图例标题在我的条形图中不能正确显示。最初的图例样例将会出现,我选择了背景颜色等,但实际上没有说明图形线代表什么。
@Eric Skroch在他检查方法是否在数据源类中实现的评论中是完全正确的(这并不奇怪)。或者更确切地说,确保绘图的代理设置正确。
在我的例子中,我一直在遵循Ray Wenderlich网站上典型的优秀的核心绘图教程,这将绘图、图形和轴的配置分成了不同的方法。当我开始配置我的Legends时,我只是把相关的代码放错了地方,并把它放在了为我的类(在我的例子中是self)设置图形委托之前。
简而言之,一旦我将图例配置代码放在graph.delegate=self;语句之后,一切都是正常的。
Animal451
这不是一个好的答案,但它是一个答案。
https://stackoverflow.com/questions/9890501
复制相似问题