本文介绍了在X轴coreplot上使用Epoch时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使用核心图散点图X轴与时代?这是我使用的绘图范围。

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

我正在读取文件中的纪元值。


$ b ((self.scatterPlotView1))$ b pre - (void)plot1 {

//在我们启动
之前先进行一些简单的理智检查== null)||(self.graphData == nil)){
NSLog(@TUTSimpleScatterPlot:无法初始化没有scatterPlotView1或数据的绘图。
return; ((self.graph!= nil)){
NSLog(@TUTSimpleScatterPlot:Graph object has already exists。);
}


return;
}

//创建一个图形对象,我们将用它来托管一个散点图。

CPTTimeFormatter * dateTimeFormatterForXAxis;

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


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

self.scatterPlotView1.hostedGraph = _graph;

//在图形中添加一些填充,轴标签底部的填充更多。
self.graph.plotAreaFrame.paddingTop = 20.0f;
self.graph.plotAreaFrame.paddingRight = 10.0f;
self.graph.plotAreaFrame.paddingBottom = 30.0f;
self.graph.plotAreaFrame.paddingLeft = 35.0f;

//将我们创建的图形与托管视图绑定。
self.scatterPlotView1.hostedGraph = self.graph;

//如果您想使用其中一个默认主题 - 请在此处应用。
[self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];

//创建一个我们将应用于轴和数据线的线条样式。
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;

//创建一个我们将用于轴标签的文本样式。
CPTMutableTextStyle * textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @Helvetica;
textStyle.fontSize = 14;
textStyle.color = [CPTColor blackColor];

//创建我们要使用的剧情符号。
CPTPlotSymbol * plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.lineStyle = plotStyle;
plotSymbol.size = CGSizeMake(2.0,2.0);
//设置一些浮点数,表示我们轴上的最小/最大值。
float xAxisMin = 0;
float xAxisMax = 10;
float yAxisMin = 0;
float yAxisMax = 100;




//我们修改图形的绘图空间来设置轴的最小/最大值。
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)];

//用标签,线条样式等修改图形的坐标轴
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 = @时间(每秒);
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;

//为我们的图形和坐标轴添加一个绘图。我们给它一个标识符,以便我们
//可以根据需要将多个图(数据行)添加到同一个图中。
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];

}


//返回图上点数的委托方法
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
if([plot.identifier isEqual:@mainplot])
{
return [self.graphData count];
}

返回0; (NSNumber *)numberForPlot:(CPTPlot *)绘图字段:(NSUInteger)fieldEnum recordIndex:(NSUInteger)索引
{


- 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决定我们是否返回一个X或Y值。
if(fieldEnum == CPTScatterPlotFieldX)
{
return [NSNumber numberWithFloat:point.x];
}
else // Y轴
{
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决定我们是否返回一个X或Y值。
if(fieldEnum == CPTScatterPlotFieldX)
{
return [NSNumber numberWithFloat:point1.x];
}
else // Y轴
{
return [NSNumber numberWithFloat:point1.y];
}


}
}

return [NSNumber numberWithFloat:0];

$ / code>

当我如上所述设置xaxis绘图范围时,我最终得到了一个浓缩图形。是因为我把绘图区域作为浮点数?

解决方案

这是一个有效的绘图范围。很难判断它是否代表预期的范围,但没有更多的信息。



对添加的代码的评论:您在数据源中做了大量不必要的工作。如果你有很多数据,这会对你的情节表现产生不利影响。尝试像这样:

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


Is there a way I can use coreplot scatterplot x-axis with epochtime? This is the plot range that I used.

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

I'm reading the epoch values from a file.

 -(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];
}

When i set the xaxis plot range as mentioned above I end up getting a condensed graph. Is it because im taking the plot range as float ?

解决方案

That's a valid plot range. It's hard to tell if it represents the intended range without more information.

Comment about the added code: You're doing a lot of unnecessary work in the datasource. If you have a lot of data, this will adversely affect the performance of your plot. Try something like this:

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

这篇关于在X轴coreplot上使用Epoch时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:01