2012-02-16 117 views
0

有沒有一種方法可以將coreplot scatterplot x軸與epochtime一起使用?這是我使用的繪圖範圍。在X軸coreplot上使用Epoch時間

 plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1329375688) length:CPTDecimalFromFloat(100)]; 

我在讀取文件中的紀元值。

-(void)plot1{ 

    // Start with some simple sanity checks before we kick off 
    if ((self.scatterPlotView1 == nil) || (self.graphData == nil)) { 
    NSLog(@"TUTSimpleScatterPlot: Cannot initialise plot without scatterPlotView1 or data."); 
    return; 
} 

if ((self.graph != nil) ) { 
    NSLog(@"TUTSimpleScatterPlot: Graph object already exists."); 
    return; 
} 

// Create a graph object which we will use to host just one scatter plot. 

CPTTimeFormatter *dateTimeFormatterForXAxis; 

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
[formatter setMaximumFractionDigits:0]; 


_graph=[[CPTXYGraph alloc] initWithFrame:CGRectMake(0,500,750,500)]; 

self.scatterPlotView1.hostedGraph = _graph; 

// Add some padding to the graph, with more at the bottom for axis labels. 
self.graph.plotAreaFrame.paddingTop = 20.0f; 
self.graph.plotAreaFrame.paddingRight = 10.0f; 
self.graph.plotAreaFrame.paddingBottom = 30.0f; 
self.graph.plotAreaFrame.paddingLeft= 35.0f; 

// Tie the graph we've created with the hosting view. 
self.scatterPlotView1.hostedGraph = self.graph; 

// If you want to use one of the default themes - apply that here. 
[self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]]; 

// Create a line style that we will apply to the axis and data line. 
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
CPTMutableLineStyle *gridStyle = [CPTMutableLineStyle lineStyle]; 
CPTMutableLineStyle *plotStyle = [CPTMutableLineStyle lineStyle]; 
lineStyle.lineColor = [CPTColor redColor]; 
lineStyle.lineWidth = 2.0f; 
gridStyle.lineColor = [CPTColor redColor]; 
gridStyle.lineWidth = 1.0f; 
plotStyle.lineColor = [CPTColor blackColor]; 
plotStyle.lineWidth = 1.0;  

// Create a text style that we will use for the axis labels. 
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; 
textStyle.fontName = @"Helvetica"; 
textStyle.fontSize = 14; 
textStyle.color = [CPTColor blackColor]; 

// Create the plot symbol we're going to use. 
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
plotSymbol.lineStyle = plotStyle; 
plotSymbol.size = CGSizeMake(2.0, 2.0); 
// Setup some floats that represent the min/max values on our axis. 
float xAxisMin = 0; 
float xAxisMax = 10; 
float yAxisMin = 0; 
float yAxisMax = 100; 




// We modify the graph's plot space to setup the axis' min/max values. 
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace; 
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1329375688) length:CPTDecimalFromFloat(500)]; 
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)]; 

// Modify the graph's axis with a label, line style, etc. 
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet; 
axisSet.xAxis.visibleRange =[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(yAxisMin) length:CPTDecimalFromDouble(yAxisMax)]; 
axisSet.yAxis.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(yAxisMin) length:CPTDecimalFromDouble(yAxisMax)]; 


axisSet.xAxis.title = @"Time(per sec)"; 
axisSet.xAxis.titleTextStyle = textStyle; 
axisSet.xAxis.axisLineStyle = lineStyle; 
axisSet.xAxis.titleOffset = 30.0f; 
axisSet.xAxis.majorTickLineStyle = lineStyle; 
axisSet.xAxis.minorTickLineStyle = lineStyle; 
axisSet.xAxis.majorGridLineStyle = lineStyle; 
axisSet.xAxis.minorGridLineStyle=gridStyle; 
axisSet.xAxis.gridLinesRange = axisSet.xAxis.visibleRange; 
axisSet.xAxis.labelOffset = 6.0f; 
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(2.0f); 
axisSet.xAxis.minorTicksPerInterval = 5; 
axisSet.xAxis.minorTickLength = 10.0f; 
axisSet.xAxis.majorTickLength = 7.0f; 
axisSet.xAxis.labelTextStyle = textStyle; 
axisSet.xAxis.labelFormatter = formatter; 



axisSet.yAxis.title = @"Uterus Ccontraction"; 
axisSet.yAxis.titleTextStyle = textStyle; 
axisSet.yAxis.titleOffset = 20.0f; 
axisSet.yAxis.axisLineStyle = lineStyle; 
axisSet.yAxis.majorTickLineStyle = lineStyle; 
axisSet.yAxis.minorTickLineStyle = lineStyle; 
axisSet.yAxis.labelTextStyle = textStyle; 
axisSet.yAxis.labelFormatter = formatter; 
axisSet.yAxis.majorGridLineStyle= lineStyle; 
axisSet.yAxis.minorGridLineStyle=gridStyle; 
axisSet.yAxis.gridLinesRange = axisSet.yAxis.visibleRange; 


axisSet.yAxis.labelOffset = 3.0f; 
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(10.0f); 
axisSet.yAxis.minorTicksPerInterval = 1; 
axisSet.yAxis.minorTickLength = 5.0f; 
axisSet.yAxis.majorTickLength = 7.0f; 

// Add a plot to our graph and axis. We give it an identifier so that we 
// could add multiple plots (data lines) to the same graph if necessary. 
CPTScatterPlot *plot = [[CPTScatterPlot alloc] init]; 
plot.dataSource = self; 
plot.identifier = @"mainplot"; 
plot.dataLineStyle = plotStyle; 
plot.plotSymbol = plotSymbol; 
//[self.graph reloadData]; 
[self.graph setNeedsDisplay]; 
[self.graph addPlot:plot]; 

} 


    //Delegate method that returns the number of points on the plot 
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
    { 
if ([plot.identifier isEqual:@"mainplot"]) 
{ 
    return [self.graphData count]; 
} 

return 0; 
    } 


    -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex: (NSUInteger)index 
    { 
    if ([plot.identifier isEqual:@"mainplot"]) 
     { 
    //NSValue *value = [self.graphData objectAtIndex:index]; 
    //CGPoint point = [value CGPointValue]; 

    if(self.scatterPlotView1){ 
    NSDictionary *entry = [self.graphData objectAtIndex:index]; 
    CGFloat x = [[entry objectForKey:@"X"] floatValue]; 
    CGFloat y = [[entry objectForKey:@"Y"] floatValue]; 
    CGPoint point = CGPointMake(x, y); 


    // FieldEnum determines if we return an X or Y value. 
    if (fieldEnum == CPTScatterPlotFieldX) 
    { 
     return [NSNumber numberWithFloat:point.x]; 
    } 
    else // Y-Axis 
    { 
     return [NSNumber numberWithFloat:point.y]; 
    } 
    } else if(self.scatterPlotView2){ 
      NSDictionary *entry = [self.graphData objectAtIndex:index]; 
      CGFloat x = [[entry objectForKey:@"X"] floatValue]; 
      CGFloat z = [[entry objectForKey:@"Y"] floatValue]; 
      CGPoint point1 = CGPointMake(x, z); 


      // FieldEnum determines if we return an X or Y value. 
      if (fieldEnum == CPTScatterPlotFieldX) 
      { 
       return [NSNumber numberWithFloat:point1.x]; 
      } 
      else // Y-Axis 
      { 
       return [NSNumber numberWithFloat:point1.y]; 
      } 


    } 
} 

return [NSNumber numberWithFloat:0]; 
} 

在i設定爲上面提到我最終得到的稠圖表x軸繪圖範圍。是否因爲我把劇情範圍作爲浮動?

回答

2

這是一個有效的繪圖範圍。沒有更多的信息,很難說它是否代表了預期的範圍。

關於添加的代碼的評論:您在數據源中做了大量不必要的工作。如果你有很多數據,這會對你的情節表現產生不利影響。嘗試這樣的:

NSDictionary *entry = [self.graphData objectAtIndex:index]; 
switch (fieldEnum) { 
case CPTScatterPlotFieldX: 
    return [entry objectForKey:@"X"]; 
    break; 
case CPTScatterPlotFieldY: 
    return [entry objectForKey:@"Y"]; 
    break; 
} 
+0

你好埃裏克非常感謝!我現在更新了代碼 – Siddharthan 2012-02-18 07:06:51

+0

無論你有沒有正確的'xRange',都取決於數據。如果你所有的x值(至少是你想看到的)都在1329375688和1329376188(1329375688 + 500)之間,那麼這就是你想要的。 – 2012-02-19 15:30:08

+0

是的埃裏克,Xvalues在幾秒鐘內上升1,直到24小時。 – Siddharthan 2012-02-19 15:57:55