gpt4 book ai didi

ios - 为什么 CPTPlotSpaceAnnotation 出现在离绘图符号很远的地方

转载 作者:行者123 更新时间:2023-11-28 22:43:08 32 4
gpt4 key购买 nike

我使用 plotSymbolWasSelectedAtRecordIndex 在绘图符号上单击时显示注释,但对于某些绘图符号,它们看起来很远,而对于一些靠近符号的符号,就像它应该的那样。例如,当单击图中的最小点时,CTPPlotSpaceAnnotiation 出现在屏幕左侧,而不是该点附近。

编辑:我想出了问题所在。 NSNumber x 没有值,因为变量 dates 是一个 NSString,我认为它必须先转换为 NSDate,然后再转换为 NSNumber。我应该从变量日期中获取月数吗?,以便在变量 anchor 中使用它,以便注释显示在绘图符号附近。

另一个编辑:就像我上面说的,我将 NSString 日期转换为 NSDate,然后转换为 NSNumber,当我现在调试时,它会显示我认为的天数。但是现在当我点击绘图符号时,注释不再出现。例如,如果日期是 07.2010,则 x 的值为 299624400.0,注释不再起作用。

这是代码:

-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
if ( symbolTextAnnotation ) {
[graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
symbolTextAnnotation = nil;
}

// Setup a style for the annotation
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPTColor whiteColor];
hitAnnotationTextStyle.fontSize = 16.0f;
hitAnnotationTextStyle.fontName = @"Helvetica-Bold";

// Determine point of symbol in plot coordinates
NSNumber *x = [[CPDStockPriceStore sharedInstance].dates objectAtIndex:index];
NSNumber *y = [[CPDStockPriceStore sharedInstance].prices objectAtIndex:index];
NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
NSString *currentValue = [formatter stringFromNumber:[[CPDStockPriceStore sharedInstance].prices objectAtIndex:index]];
currentValue = [currentValue stringByAppendingString:@"-"];

NSString *currentValue1 = [[CPDStockPriceStore sharedInstance].dates objectAtIndex:index];

// Now add the annotation to the plot area
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:[currentValue stringByAppendingString:currentValue1] style:hitAnnotationTextStyle];
symbolTextAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
symbolTextAnnotation.contentLayer = textLayer;
symbolTextAnnotation.displacement = CGPointMake(0.0f, 10.0f);
[graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];

}

这是绘图、x 轴和 y 轴的代码:-(void)configurePlots {

// 1 - Get graph and plot space
graph = self.hostView.hostedGraph;
plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
// 2 - Create the three plots

aaplPlot = [[CPTScatterPlot alloc] init];
aaplPlot.dataSource = self;

aaplPlot.delegate = self;
aaplPlot.plotSymbolMarginForHitDetection = 20.0f;

aaplPlot.identifier = CPDTickerSymbolAAPL;
CPTColor *aaplColor = [CPTColor redColor];
[graph addPlot:aaplPlot toPlotSpace:plotSpace];

msftPlot = [[CPTScatterPlot alloc] init];
msftPlot.dataSource = self;
msftPlot.identifier = CPDTickerSymbolMSFT;
CPTColor *msftColor = [CPTColor clearColor];
[graph addPlot:msftPlot toPlotSpace:plotSpace];
// 3 - Set up plot space

NSArray *array = [[NSArray alloc] initWithObjects:aaplPlot, msftPlot, nil];
[plotSpace scaleToFitPlots:array];

CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
plotSpace.yRange = yRange;

// 4 - Create styles and symbols
CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy];
aaplLineStyle.lineWidth = 2.5;
aaplLineStyle.lineColor = aaplColor;
aaplPlot.dataLineStyle = aaplLineStyle;
CPTMutableLineStyle *aaplSymbolLineStyle = [CPTMutableLineStyle lineStyle];
aaplSymbolLineStyle.lineColor = aaplColor;
CPTPlotSymbol *aaplSymbol = [CPTPlotSymbol ellipsePlotSymbol];
aaplSymbol.fill = [CPTFill fillWithColor:aaplColor];
aaplSymbol.lineStyle = aaplSymbolLineStyle;
aaplSymbol.size = CGSizeMake(6.0f, 6.0f);
aaplPlot.plotSymbol = aaplSymbol;

}

-(void)configureAxes {

// 1 - Create styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor];
tickLineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure x-axis
CPTAxis *x = axisSet.xAxis;
x.title = @"Ziua Lunii";
x.titleTextStyle = axisTitleStyle;
x.titleOffset = 15.0f;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.labelTextStyle = axisTextStyle;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 4.0f;
x.tickDirection = CPTSignNegative;
CGFloat dateCount = [[[CPDStockPriceStore sharedInstance] datesInMonth] count];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
NSInteger i = 0;

for (NSString *date in [[CPDStockPriceStore sharedInstance] datesInMonth]) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date textStyle:x.labelTextStyle];
label.rotation = M_PI/4;
CGFloat location = i++;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = x.majorTickLength;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
// 4 - Configure y-axis
CPTAxis *y = axisSet.yAxis;
y.title = @"Pret";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = -50.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = 23.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 4.0f;
y.minorTickLength = 2.0f;
y.tickDirection = CPTSignPositive;
NSInteger majorIncrement = 100;
NSInteger minorIncrement = 50;
CGFloat yMax = 1300.0f; // should determine dynamically based on max price
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
NSMutableSet *yMinorLocations = [NSMutableSet set];
for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
NSUInteger mod = j % majorIncrement;
if (mod == 0) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j);
label.tickLocation = location;
label.offset = - y.majorTickLength - y.labelOffset;
if (label) {
[yLabels addObject:label];
}
[yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
} else {
[yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
}
}
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;
y.minorTickLocations = yMinorLocations;

}

最佳答案

看起来您正在使用 x 值的数据索引。这很容易;根本不需要转换日期:

NSNumber *x = [NSNumber numberWithUnsignedInteger:index];

关于ios - 为什么 CPTPlotSpaceAnnotation 出现在离绘图符号很远的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13930557/

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