gpt4 book ai didi

iphone - 核心图 : Updating X Axis range in real time plotting

转载 作者:行者123 更新时间:2023-12-01 18:00:03 25 4
gpt4 key购买 nike

我已经检查了大多数似乎最相关的问题,但没有一个真正涉及我目前遇到的一些麻烦。我的问题是,虽然我知道无论我如何尝试更新它都需要更新 plotSpace.xRange,但要么什么都没有发生,要么我崩溃了。

我目前正在做的是我在 RealTimeScatterPlot.m 文件中声明了 2 个全局变量(xMinVal,xMaxVal),这些是我在绘图初始化时用于 plotSpace.xRange 计算的变量。然后,托管绘图 View 的 View Controller 包含一个函数,该函数每秒生成并绘制一个随机点。在这个函数中,我跟踪查看是否有超过 25 个(我的默认 x 轴范围目前是从 0 到 50)点,如果有,我会尝试将 RealTimeScatterPlot.m 中的全局值加一,这理论上应该将我的图形 View 移动 1。第一次增量发生,尽管 View 没有移动,我也不能再次增量,因为值保持不变。如果看到代码有助于让我知道,我会编辑它,但它都非常简单/通用。我在想,这可能不是我应该实现的方式,而不是编码问题。

任何见解将不胜感激!

最佳答案

您可以轻松地做您想做的事情,例如更新 tableview 数据。
这是我所做的部分代码,您可以引用。希望这可以帮助你。

-(void) fetchArrayOffeedback {

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Pulse"
inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];

NSPredicate *predictate = [NSPredicate predicateWithFormat:@"sessionKey == %@", appDelegateiPhone.strSessionId];

[fetchRequest setPredicate:predictate];

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"date" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[sort release];

NSError *error;

chartData = (NSMutableArray*)[appDelegate.managedObjectContext
executeFetchRequest:fetchRequest error:&error];

[chartData retain];
NSLog(@"array count :%d", [chartData count]);

[graph reloadData];

CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:CPDecimalFromFloat(fiveHour*3)];
}

- (void)constructScatterPlotForFeedback
{
[graph release];

graph = [[CPXYGraph alloc] initWithFrame:CGRectZero];
graph.delegate = self;
CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme];
[graph applyTheme:theme];
feedbackChart.hostedGraph = graph;
[feedbackChart setBackgroundColor:[UIColor clearColor]];

graph.paddingLeft = 0.0;
graph.paddingTop = 0.0;
graph.paddingRight = 0.0;
graph.paddingBottom = 0.0;

graph.plotAreaFrame.paddingLeft = 5.0;
graph.plotAreaFrame.paddingTop = 10.0;
graph.plotAreaFrame.paddingRight = 10.0;
graph.plotAreaFrame.paddingBottom = 10.0;

NSTimeInterval oneHour = 60 * 60;

CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;

float xRange;
xRange = [chartData count] + 1.0;

if (xRange>5) {
xRange = 5.0;
}
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:CPDecimalFromFloat(fiveHour*3)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-20.0) length:CPDecimalFromFloat(40.0)];


CPLineStyle *gridline = [CPLineStyle lineStyle];
gridline.lineColor = [CPColor grayColor];
gridline.lineWidth = 1.0f;

CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPDecimalFromFloat(fiveHour);
x.minorTicksPerInterval = 0;
x.labelOffset=0;

NSDate *refDate = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

dateFormatter.dateFormat = @"HH:mm:ss";

CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = refDate;
x.labelFormatter = timeFormatter;

CPXYAxis *y = axisSet.yAxis;

y.minorTicksPerInterval = 0;
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0.0");
y.majorIntervalLength = CPDecimalFromString(@"5");
y.majorGridLineStyle = nil;

y.visibleRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-20.0f) length:CPDecimalFromFloat(40.0f)];
CPConstraints yConstraints = {CPConstraintNone, CPConstraintFixed};
y.isFloatingAxis=YES;
y.constraints=yConstraints;

CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = @"FeedBack Plot";
dataSourceLinePlot.dataLineStyle.lineWidth = 3.f;
dataSourceLinePlot.dataLineStyle.lineColor = [CPColor blackColor];
dataSourceLinePlot.dataLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:1.0f], [NSNumber numberWithFloat:1.0f], nil];
dataSourceLinePlot.dataSource = self;

dataSourceLinePlot.opacity = 0.0f;
dataSourceLinePlot.cachePrecision = CPPlotCachePrecisionDecimal;

CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[dataSourceLinePlot addAnimation:fadeInAnimation forKey:@"animateOpacity"];

CPColor *areaColor1 = [CPColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8];
CPGradient *areaGradient1 = [CPGradient gradientWithBeginningColor:areaColor1 endingColor:[CPColor clearColor]];
areaGradient1.angle = -90.0f;

NSUInteger i;

[graph addPlot:dataSourceLinePlot];

NSMutableArray *contentArray1 = [NSMutableArray arrayWithCapacity:100];

NSMutableArray *customTickLocations = [[NSMutableArray alloc] init];
NSMutableArray *xAxisLabels = [[NSMutableArray alloc] init];
for ( i = 0; i < [chartData count]; i++ ) {
//for ( i = 0; i < 10; i++ ) {
[customTickLocations addObject:[NSDecimalNumber numberWithInt:i]];
[xAxisLabels addObject:[NSString stringWithFormat:@"%0.1f", 5.0 * i]];
Pulse *objPulse1 = (Pulse *)[chartData objectAtIndex:i];
// id x = [NSNumber numberWithFloat:i*fiveHour];
id x = [NSNumber numberWithFloat:i];
id y = [NSNumber numberWithFloat:[objPulse1.feedBack floatValue]];
NSLog(@"point of gragh for Performance x:%@, y : %@ ", x, y);
[contentArray1 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}

NSLog(@"Axis lable count : %d", [xAxisLabels count]);
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[chartData count]];
NSUInteger labelLocation = 0;

for (NSNumber *tickLocation in customTickLocations) {
NSLog(@">>>>>>>>>>>>>>>>> tick location");
CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength - 5.0f;
newLabel.rotation = 0;
[customLabels addObject:newLabel];
[newLabel release];
}

x.axisLabels = [NSSet setWithArray:customLabels];

arrScatter = contentArray1;
[arrScatter retain];
}

-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{

if ( [(NSString *)plot.identifier isEqualToString:@"FeedBack Plot"] ) {
return [chartData count];
}

return 0;
}

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
switch ( fieldEnum ) {
case CPScatterPlotFieldX:
return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index*fiveHour];
case CPScatterPlotFieldY:
{
Pulse *objPulse1 = (Pulse *)[chartData objectAtIndex:index];
return [NSNumber numberWithFloat:[objPulse1.feedBack floatValue]];
}
}
return nil;
}

-(CGPoint)plotSpace:(CPPlotSpace *)space willDisplaceBy:(CGPoint)displacement {

return CGPointMake(displacement.x, 0);
}


/*
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSDecimalNumber *num = nil;

if ( [(NSString *)plot.identifier isEqualToString:@"FeedBack Plot"] ) {
if ( [arrScatter count] != 0) {
num = [[arrScatter objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
}
}
return num;
}
*/

-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index {

Pulse *objPulse1 = (Pulse *)[chartData objectAtIndex:index];
float value = [objPulse1.feedBack floatValue];
//CPTextLayer *textLayer = [[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"%d", value]];

CPTextStyle *textStyle = [CPTextStyle textStyle];
textStyle.color = [CPColor blackColor];

CPTextLayer *textLayer = [[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"%0.1f", value] style:textStyle];
return textLayer;
}

关于iphone - 核心图 : Updating X Axis range in real time plotting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11350625/

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