私はCorePlotライブラリにかなり慣れていません。アプリの 1 つで、CorePlot を使用してチャートの棒グラフを描画しています。チャートの各棒グラフのデータ ラベルも表示します。labelOffset
棒グラフ内の位置を設定するために使用しています(つまりlabelOffset= -10.0
)。labelOffset = 10.0
デフォルトのバー。
しかし、CPTPlot でこの奇妙な動作が見られます。棒グラフにデータ ラベルが 2 回描画されます。スクリーンショットは次のとおりです。
コード スニペットを次に示します。
-(CPTTextLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
plot.labelFormatter = nil;
plot. labelTextStyle = nil;
if (self.showValuesAsPlotLabels)
{
if (self.showValuesAsPlotLabels)
{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setGeneratesDecimalNumbers:NO];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMinimumFractionDigits:2];
CGFloat yValue = [self.chartData[index][1] floatValue];
//return [NSString stringWithFormat:@"%f", yValue];
NSString *labelStr = [numberFormatter stringFromNumber:@(yValue)];
/* Check to see how tall is bar chart so that we can accordingly position data label */
CGFloat yBaseOffsetValue = self.yAxisIntervalLength;
CGFloat compareValue = (yBaseOffsetValue * self.divisor);
UIColor *dataLabelColor;
float dataLabelOffset = -10.0f;
if (yValue > compareValue) {
dataLabelColor = [UIColor whiteColor];
plot.labelOffset = dataLabelOffset;
} else {
dataLabelColor = [UIColor blackColor];
plot.labelOffset = (0.0f - dataLabelOffset);
}
return [[CPTTextLayer alloc] initWithText:labelStr style:[CPTTextStyle textStyleWithAttributes:@{NSForegroundColorAttributeName: dataLabelColor}]];
}
if (self.plotLabels == nil || self.plotLabels.count < index)
{
return nil;
}
NSString *identifier = (self.plotLabels)[index];
CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:identifier];
return label;
}
ここで何が欠けているか、間違っていますか?
事前に感謝します。
EDIT
エリックの提案の後plot.identifier
、プロット固有の yValues をチェックし、それに応じて dataLabel を返すためにlabelOffset
使用しようとしましtextStyle
た。ただし、現在、棒グラフで次の動作が見られます。
dataLabelForPlot:recordIndex:
の同じインスタンスを使用していると思いますCPTTextLayer
か?CPTTextLayer
for それぞれの異なるインスタンスを持つことができる方法はありますCPTBarPlot
か?
ありがとう