在 x 轴上移动日期
我一直在玩 Core Plot 并尝试创建动态日期 x 轴。从日期图示例中,我已成功创建一个静态日期轴,但希望随时创建一个两分钟的窗口并更新 xRange 值。我不确定如何将日期作为 xRange 最小值和长度值传递以及如何在 x 轴上显示时间。
我已经看过示例,但我无法利用 NSTimeInterval (如果这是如何做到的......)。
下面是图片(如果有帮助)
下面是我迄今为止的尝试;有人可以建议我如何实现这一目标吗?
- (void)loadView {
// Alloc & Init Main View and since the display resolution is 1024x768 take 20 off for labels later
UIView *tmpView = [ [ UIView alloc ] initWithFrame:CGRectMake(0, 0, 1024.0,768.0) ];
[ tmpView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];
[ tmpView setBackgroundColor:[ UIColor redColor ] ];
// Alloc Graph View
graphView = [ [ CPGraphHostingView alloc ] initWithFrame:CGRectMake(0, 0, 1024.0,768.0) ];
[ tmpView addSubview:[ graphView autorelease ] ];
// Set MainView
[ self setView:[ tmpView autorelease ] ];
}
-(void)viewDidLoad{
[super viewDidLoad];
NSDate *refDate = [NSDate date];
// NSTimeInterval oneDay = 24 * 60 * 60;
NSTimeInterval oneHour = 60 * 60;
NSTimeInterval fivemin= 5 * 60;
// Create graph from theme
graph = [(CPXYGraph *) [CPXYGraph alloc] initWithFrame:self.view.bounds];
CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
[graph applyTheme:theme];
graphView.hostedGraph = graph;
//padding
graph.paddingLeft = 20.0;
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 20.0;
graph.plotAreaFrame.paddingTop=10.0;
graph.plotAreaFrame.paddingLeft=50.0;
graph.plotAreaFrame.paddingRight=35.0;
graph.plotAreaFrame.paddingBottom=50.0;
// Setup scatter plot space
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
NSTimeInterval xLow = 0.0f;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(xLow) length:CPDecimalFromFloat(oneHour)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(100.0f)];
//Line Styles
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor redColor];
lineStyle.lineWidth = 2.0f;
CPLineStyle *majorGridLineStyle = [CPLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
CPLineStyle *minorGridLineStyle = [CPLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPColor whiteColor] colorWithAlphaComponent:0.1];
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
// X-Axes formatting
CPXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPDecimalFromFloat(oneHour);
x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
x.minorTicksPerInterval = 0;
x.labelOffset=0;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = refDate;
x.labelFormatter = timeFormatter;
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.title=@"Time Axis";
//Y-Axes formatting
CPXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = [ [ NSDecimalNumber decimalNumberWithString:@"10.0" ] decimalValue ];
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
y.minorTicksPerInterval = 5;
y.labelOffset = 0.0;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.preferredNumberOfMajorTicks = 10;
y.minorTickLineStyle = nil;
y.labelTextStyle = nil;
y.visibleRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(100.0f)];
CPConstraints yConstraints = {CPConstraintFixed, CPConstraintFixed};
y.isFloatingAxis=YES;
y.constraints=yConstraints;
// Create a plot that uses the data source method
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = @"Date Plot";
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
mydata = [[NSMutableArray alloc]initWithObjects:
[NSDecimalNumber numberWithInt:0],
nil ];
//a timer to re-load the graph every 2 seconds and re-draw x-axis
Timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(testingTimer:) userInfo:nil repeats:YES];
}
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{
return mydata.count;
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
switch ( fieldEnum ) {
case CPScatterPlotFieldX:
return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
case CPScatterPlotFieldY:
return [mydata objectAtIndex:index];
}
return nil;
}
-(void) testingTimer: (NSTimer *) Timer{
//generating random number and add to mydata array
testdata=arc4random() % 100;
[mydata addObject:[NSNumber numberWithInt:testdata]];
[graph reloadData];
count++;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(count) length:CPDecimalFromFloat(5*30.0f)];
}
I have been playing with Core Plot and trying to create a dynamic date x-axis. From the date plot example I have managed to create a static date axis, but would like to create a two-minute window at any time and update the xRange values. I am not sure on how to pass dates as the xRange min and length values and display time on the x-axis.
I have looked at examples, but I haven't been able to utilize NSTimeInterval
(if this is how to do it...).
Below is the picture (if it helps)
Below is my attempt so far; can someone please advise me on how to achieve this?
- (void)loadView {
// Alloc & Init Main View and since the display resolution is 1024x768 take 20 off for labels later
UIView *tmpView = [ [ UIView alloc ] initWithFrame:CGRectMake(0, 0, 1024.0,768.0) ];
[ tmpView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];
[ tmpView setBackgroundColor:[ UIColor redColor ] ];
// Alloc Graph View
graphView = [ [ CPGraphHostingView alloc ] initWithFrame:CGRectMake(0, 0, 1024.0,768.0) ];
[ tmpView addSubview:[ graphView autorelease ] ];
// Set MainView
[ self setView:[ tmpView autorelease ] ];
}
-(void)viewDidLoad{
[super viewDidLoad];
NSDate *refDate = [NSDate date];
// NSTimeInterval oneDay = 24 * 60 * 60;
NSTimeInterval oneHour = 60 * 60;
NSTimeInterval fivemin= 5 * 60;
// Create graph from theme
graph = [(CPXYGraph *) [CPXYGraph alloc] initWithFrame:self.view.bounds];
CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
[graph applyTheme:theme];
graphView.hostedGraph = graph;
//padding
graph.paddingLeft = 20.0;
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 20.0;
graph.plotAreaFrame.paddingTop=10.0;
graph.plotAreaFrame.paddingLeft=50.0;
graph.plotAreaFrame.paddingRight=35.0;
graph.plotAreaFrame.paddingBottom=50.0;
// Setup scatter plot space
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
NSTimeInterval xLow = 0.0f;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(xLow) length:CPDecimalFromFloat(oneHour)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(100.0f)];
//Line Styles
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor redColor];
lineStyle.lineWidth = 2.0f;
CPLineStyle *majorGridLineStyle = [CPLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
CPLineStyle *minorGridLineStyle = [CPLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPColor whiteColor] colorWithAlphaComponent:0.1];
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
// X-Axes formatting
CPXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPDecimalFromFloat(oneHour);
x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
x.minorTicksPerInterval = 0;
x.labelOffset=0;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = refDate;
x.labelFormatter = timeFormatter;
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.title=@"Time Axis";
//Y-Axes formatting
CPXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = [ [ NSDecimalNumber decimalNumberWithString:@"10.0" ] decimalValue ];
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
y.minorTicksPerInterval = 5;
y.labelOffset = 0.0;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.preferredNumberOfMajorTicks = 10;
y.minorTickLineStyle = nil;
y.labelTextStyle = nil;
y.visibleRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(100.0f)];
CPConstraints yConstraints = {CPConstraintFixed, CPConstraintFixed};
y.isFloatingAxis=YES;
y.constraints=yConstraints;
// Create a plot that uses the data source method
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = @"Date Plot";
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
mydata = [[NSMutableArray alloc]initWithObjects:
[NSDecimalNumber numberWithInt:0],
nil ];
//a timer to re-load the graph every 2 seconds and re-draw x-axis
Timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(testingTimer:) userInfo:nil repeats:YES];
}
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{
return mydata.count;
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
switch ( fieldEnum ) {
case CPScatterPlotFieldX:
return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
case CPScatterPlotFieldY:
return [mydata objectAtIndex:index];
}
return nil;
}
-(void) testingTimer: (NSTimer *) Timer{
//generating random number and add to mydata array
testdata=arc4random() % 100;
[mydata addObject:[NSNumber numberWithInt:testdata]];
[graph reloadData];
count++;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(count) length:CPDecimalFromFloat(5*30.0f)];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
xRange 的位置是您希望绘图开始的位置,例如,如果您为绘图加载了 5 分钟(假设每秒 1 个条目)的数据,并且您希望在第 3 分钟开始,则您的位置将为 3*60。如果您只想显示 2 分钟的数据,则长度始终为 2*60。
您可能还想更改 x.majorIntervalLength,该值控制沿 x 轴放置主要刻度的频率。您的主要刻度通常有一个与之关联的标签,因此您需要将该值降低到更适合仅 2 分钟数据的值
the location for the xRange is where you want the plot to start, for example if you had 5 minutes(assuming 1 entry per second) of data loaded for the plot and you wanted to start at minute 3 your location would be 3*60. your length would always be 2*60 if you only want to show 2 minutes of data.
you probably want to change x.majorIntervalLength as well, that value controls how often a major tick is placed along the x axis. your major tick usually has a label associated with it, so you'd want to lower that value to something more appropriate for only 2 minutes of data