在x轴上移动日期 [英] Moving date on x-axis

查看:112
本文介绍了在x轴上移动日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩核心绘图,并尝试创建一个动态日期x轴。从日期绘图示例中我已经设法创建一个静态日期轴,但是希望随时创建一个两分钟的窗口并更新xRange值。我不知道如何传递日期作为x轴的最小和长度值,并在x轴上显示时间。



我看过例子,可以使用 NSTimeInterval (如果这是怎么做...)。



以下是图片(如果有帮助)





以下是我到目前为止的尝试;有人可以告诉我如何实现这一点?

   - (void)loadView {
// Alloc&初始化主视图,由于显示分辨率为1024x768,以后的标签需要20关。
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]];

//设置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;
//从主题
创建图形图形= [(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;

//设置散点图空间
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)];

//行样式
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格式化
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 = @时间轴;


// Y轴转换格式
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;


//创建使用数据源方法的图形
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];
//定时器每2秒重新加载图形并重新绘制x轴

Timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(testingTimer :) userInfo:nil repeats:YES];

}


- (NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{
return mydata.count;


- (NSNumber *)numberForPlot:(CPPlot *)绘图字段:(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 *)计时器{

//生成随机数并添加到mydata数组
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)];

}


解决方案

对于xRange,您希望绘图开始,例如,如果您有5分钟(假设每秒1个条目)加载的情节,并且想要在3分钟开始,您的位置将为3 * 60。如果您只想显示2分钟的数据,您的长度将始终为2 * 60。



您可能还想更改x.majorIntervalLength,该值控制多长时间沿x轴放置一个主要刻度。你的主要刻度通常有一个与之相关联的标签,所以你想要将这个值降低到更适合只有2分钟数据的东西。


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)];

}

解决方案

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

这篇关于在x轴上移动日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆