2010-06-08 42 views
15

Tôi có sẵn một loạt từ điển có chứa các giá trị NSDateNSNumber. Tôi muốn vẽ ngày tháng trên trục X. Để vẽ đồ thị, tôi cần cung cấp xRanges để vẽ với một số giá trị thập phân. Tôi không hiểu làm cách nào tôi có thể cung cấp các giá trị NSDate cho xRange (thấp và dài).Sử dụng cốt truyện cốt lõi cho iPhone, ngày vẽ trên trục x

Và những gì nên có mặt ở đó trong phương pháp này:

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 

Ý tôi là như thế nào giá trị ngày của tôi sẽ được trả lại như NSNumber? Tôi nghĩ rằng tôi nên sử dụng một số khoảng thời gian trên đó, nhưng những gì cần được chuyển đổi chính xác? Bất kỳ ai có thể giải thích cho tôi yêu cầu chính xác để vẽ ngày tháng trên xAxis là gì?

Tôi vẽ âm mưu của mình ở một nửa chế độ xem.

+0

Hey xmax Tôi đang làm điều tương tự trong dự án của mình. Bạn đã tìm thấy bất kỳ cách giải quyết nào khác không? – sandy

+0

Tôi đang gặp vấn đề mà bạn phải đối mặt. vì vậy bạn sẽ hướng dẫn tôi những gì bạn làm trong tình huống đó. –

+0

LƯU Ý, một điều, trong iOS hiện đại, cách duy nhất được đề xuất bởi Apple để sử dụng trình định dạng ngày là: https://stackoverflow.com/a/42370648/294884 – Fattie

Trả lời

8

Hãy xem chương trình DatePlot trong thư mục mẫu. Nó cho thấy cách định dạng nhãn trục làm ngày.

+0

Infact Tôi đang làm theo ví dụ đó. nhưng sử dụng mẹo đó để vẽ đồ thị nhưng đồ thị không hiển thị. vì nó không nằm trong phạm vi hoặc một cái gì đó sai trái với việc cung cấp các giá trị. thực sự, dữ liệu của tôi chứa ngày có từ 3 đến 6 tháng qua. Tôi không nhận được chính xác những gì tôi nên cung cấp ngày tham khảo khi chuyển đổi NSdate thành NSDecimalNumber tương đương. Và tôi đang bối rối hơn plotSpace.xRange. những gì tôi nên cung cấp.? Tôi có nghĩa là làm thế nào tôi có thể tính toán phạm vi ngày để cung cấp cho xRange từ nguồn dữ liệu của tôi ..? – xmax

+0

Và một điều nữa là, tôi không thể hiển thị ngày đúng trên xAxis, với cốt truyện ví dụ trong cốt lõi. nếu tôi phải vẽ xAxis trên ví dụ StockPlot cốt truyện cốt lõi, thì nó sẽ như thế nào ..? – xmax

+2

Ngày tham chiếu có thể là bất kỳ thứ gì bạn muốn. Nó xác định điểm zero cho trục. Vị trí bắt đầu cho phạm vi trục là sự khác biệt giữa ngày đầu tiên của bạn trên trục và ngày tham chiếu, tính bằng giây. Độ dài phạm vi là độ dài của phạm vi tính bằng giây (có 86400 giây trong một ngày). –

36

Ở đây bạn đi, DatePlot hoạt động trên iOS. Đừng quên kiểm tra nó là chính xác.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    // If you make sure your dates are calculated at noon, you shouldn't have to 
    // worry about daylight savings. If you use midnight, you will have to adjust 
    // for daylight savings time. 
    NSDate *refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:31556926 * 10]; 
    NSTimeInterval oneDay = 24 * 60 * 60; 

    // Invert graph view to compensate for iOS coordinates 
    CGAffineTransform verticalFlip = CGAffineTransformMakeScale(1,-1); 
    self.view.transform = verticalFlip; 

    // allocate the graph within the current view bounds 
    graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds]; 

    // assign theme to graph 
    CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; 
    [graph applyTheme:theme]; 

    // Setting the graph as our hosting layer 
    CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds]; 

    [self.view addSubview:hostingView]; 

    hostingView.hostedGraph = graph; 

    graph.paddingLeft = 20.0; 
    graph.paddingTop = 20.0; 
    graph.paddingRight = 20.0; 
    graph.paddingBottom = 150.0; 

    // setup a plot space for the plot to live in 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
    NSTimeInterval xLow = 0.0f; 
    // sets the range of x values 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow) 
                length:CPTDecimalFromFloat(oneDay*5.0f)]; 
    // sets the range of y values 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) 
                length:CPTDecimalFromFloat(5)]; 

    // plotting style is set to line plots 
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
    lineStyle.lineColor = [CPTColor blackColor]; 
    lineStyle.lineWidth = 2.0f; 

    // X-axis parameters setting 
    CPTXYAxisSet *axisSet = (id)graph.axisSet; 
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneDay); 
    axisSet.xAxis.minorTicksPerInterval = 0; 
    axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1"); //added for date, adjust x line 
    axisSet.xAxis.majorTickLineStyle = lineStyle; 
    axisSet.xAxis.minorTickLineStyle = lineStyle; 
    axisSet.xAxis.axisLineStyle = lineStyle; 
    axisSet.xAxis.minorTickLength = 5.0f; 
    axisSet.xAxis.majorTickLength = 7.0f; 
    axisSet.xAxis.labelOffset = 3.0f; 

    // added for date 
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    dateFormatter.dateStyle = kCFDateFormatterShortStyle; 
    CPTTimeFormatter *timeFormatter = [[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease]; 
    timeFormatter.referenceDate = refDate; 
    axisSet.xAxis.labelFormatter = timeFormatter; 

    // Y-axis parameters setting  
    axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"0.5"); 
    axisSet.yAxis.minorTicksPerInterval = 2; 
    axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(oneDay); // added for date, adjusts y line 
    axisSet.yAxis.majorTickLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLineStyle = lineStyle; 
    axisSet.yAxis.axisLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLength = 5.0f; 
    axisSet.yAxis.majorTickLength = 7.0f; 
    axisSet.yAxis.labelOffset = 3.0f; 


    // This actually performs the plotting 
    CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease]; 

    CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle]; 
    //xSquaredPlot.identifier = @"X Squared Plot"; 
    xSquaredPlot.identifier = @"Date Plot"; 

    dataLineStyle.lineWidth = 1.0f; 
    dataLineStyle.lineColor = [CPTColor redColor]; 
    xSquaredPlot.dataLineStyle = dataLineStyle; 
    xSquaredPlot.dataSource = self; 

    CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
    greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]]; 
    greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0); 
    xSquaredPlot.plotSymbol = greenCirclePlotSymbol; 

    // add plot to graph 
    [graph addPlot:xSquaredPlot]; 

    // Add some data 
    NSMutableArray *newData = [NSMutableArray array]; 
    NSUInteger i; 
    for (i = 0; i < 5; i++) { 
     NSTimeInterval x = oneDay*i; 
     id y = [NSDecimalNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2]; 
     [newData addObject: 
     [NSDictionary dictionaryWithObjectsAndKeys: 
      [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX], 
      y, [NSNumber numberWithInt:CPTScatterPlotFieldY], 
      nil]]; 
     NSLog(@"%@",newData); 
    } 
    plotData = [newData retain]; 

} 

#pragma mark - Plot Data Source Methods 

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{ 
    return plotData.count; 
} 

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{ 
    NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]]; 
    return num; 
} 
+0

hey, điều này đã giúp tôi rất nhiều. Tôi chỉ không thể tìm ra, làm thế nào để thực sự quyết định giá trị nào được viết trên trục x mà NSTimeInterval nào? – JonasG

+1

Xin chào, @zambono, loại biểu tượng 'plotData' là gì? Tôi muốn biết định nghĩa của nó. – Sabbath

+1

Với CorePlot 1.1, bạn không cần đảo ngược trục y nữa. Đoạn mã trên hoạt động nếu bạn lấy các dòng 'verticalFlip' ra. –

Các vấn đề liên quan