gpt4 book ai didi

objective-c - 在 CorePlot 中标记条形图

转载 作者:行者123 更新时间:2023-12-03 17:50:53 26 4
gpt4 key购买 nike

我有一个水平条形图,使用 Coreplot其中我在栏内有带有白色文本的标签像这样

Long Bar

但是,当条形图太短而无法包含标签时,我希望文本为黑色,而不是像这样偏移。

Short Bar

是否有任何方法可以检查标签是否被剪裁或比较标签的宽度与条形长度?到目前为止我有这个

-(CPTTextLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
NSNumberFormatter *numberFormatter = [NSNumberFormatter percentFormatter];
NSDictionary *bar = [self.dataSource objectAtIndex:index];
NSDecimalNumber *xValue = [bar valueForKey:@"Value"];
NSString *stringLabel = [numberFormatter stringFromNumber:xValue];

CPTMutableTextStyle *whiteStyle = [[CPTMutableTextStyle alloc] init];
whiteStyle.color = [CPTColor whiteColor];
whiteStyle.fontSize = CPTFloat(12.0);
whiteStyle.fontName = @"HelveticaNeue-Medium";

CPTMutableTextStyle *darkStyle = [[CPTMutableTextStyle alloc] init];
darkStyle.color = [CPTColor blackColor];
darkStyle.fontSize = CPTFloat(12.0);
darkStyle.fontName = @"HelveticaNeue-Medium";

CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:stringLabel];

if (**isLabelClipped**) {
plot.labelOffset = 2.0f;
textLayer.textStyle = darkStyle;
} else {
plot.labelOffset = -2.0f;
textLayer.textStyle = whiteStyle;
}
return textLayer;
}

编辑正如 Eric 建议我尝试从绘图空间获取屏幕尺寸,我的想法是我会使用 xValue 和barsBase 之间的差异,所以我添加了以下内容

NSDecimal plotPoint[2];
plotPoint[CPTCoordinateX] = xValue.decimalValue;

NSNumber *barBase = [self numberForPlot:plot
field:CPTBarPlotFieldBarBase
recordIndex:index];

plotPoint[CPTCoordinateY] = barBase.decimalValue;
CGPoint dataPoint = [plot.plotSpace plotAreaViewPointForPlotPoint:plotPoint numberOfCoordinates:2];
NSLog(@"Data Point:%@", NSStringFromPoint(dataPoint));

但这给出了类似的值

{531.46951532736807, 57.166666666666664}

barBase 值 (57..) 看起来位于绘图区域的最左侧,而不是预期的 0 轴。

最佳答案

知道条形长度(来自xValue)后,您可以使用绘图空间来确定其在屏幕上的大小。创建textLayer后,检查其边界大小。比较两者并决定是将标签放置在条形内部还是外部。

关于objective-c - 在 CorePlot 中标记条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27048857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com