2012-04-26 89 views
2

我用下面的方法來爲我的圖上顯示的標籤:如何向右移動標籤Core Plot?

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index{ 
... 
CPTTextLayer *label=[[CPTTextLayer alloc] initWithText:stringValue style:textStyle]; 
} 

,並將每個指標應返回標籤

我知道它是可以移動的標籤上或下使用:

plot.labelOffset=10; 

問題是:我如何將標籤向右移動一點? 我試圖用

label.paddingLeft=50.0f;

,但它不工作。

回答

1

在你的例子中添加填充確實有效,但可能不符合你的期望。散點圖和條形圖會將標籤居中放置在每個數據點上(具有正偏移量)。填充使整個標籤更寬,所以當居中時,測試顯示在側面。這很難控制,特別是如果標籤文本長度不同。

有一個懸而未決的問題來解決這個問題(issue 266)。沒有保證什麼時候它會被修復,但這是我們正在看的東西。

+0

有同樣的問題。想要將標籤置於左側。是否有任何解決方案? – 2012-06-20 06:43:10

+1

增加標籤文本層內的'paddingRight'。 – 2012-06-21 00:06:58

1

我遇到了同樣的問題,並提出了一個不同的解決方案。

我決定做的是創建一個使用CPTAxisLabel方法initWithContentLayer標籤:

CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:labelStr style:axisTextStyle]; 

CGSize textSize = [textLayer sizeThatFits]; 

// Calculate the padding needed to center the label under the plot record. 
textLayer.paddingLeft = barCenterLeftOffset - textSize.width/2.0; 

CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithContentLayer:textLayer]; 

這裏barCenterLeftOffset是情節記錄的中心偏移。

我寫了一篇關於這個的文章:

http://finalize.com/2014/09/18/horizontal-label-positioning-in-core-plot-and-other-miscellaneous-topics/

我創建了一個使用該解決方案可以在這裏找到一個演示項目:

https://github.com/scottcarter/algorithms

1

你也可以繼承CPTTextLayer和包括抵消。

@interface WPTextLayer : CPTTextLayer 

@property (nonatomic) CGPoint offset; 

@end 

@implementation WPTextLayer 

-(void)setPosition:(CGPoint)position 
{ 
    CGPoint p = CGPointMake(position.x + self.offset.x, position.y + self.offset.y); 
    [super setPosition:p]; 
} 

然後使用:

WPTextLayer *tLayer = [[WPTextLayer alloc] initWithText:@"blah" style:textStyle]; 
tLayer.offset = CGPointMake(3, -3); 
return tLayer; 

有可能是這一點,我不知道後果,但似乎迄今進行的工作。