带有Xcode for iPhone数据的散点图不与轴同步,主要厚度未显示

时间:2014-02-25 10:51:28

标签: iphone xcode core-plot scatter-plot

我是Stackoverflow的初学者和iPhone的Xcode。 英语不是我的母语,所以请耐心等待。 我一直试图在日期(x轴)和燃油经济性值(y轴)之间制作图表。

我正在使用核心图1.4。 我的问题是:

  1. 数据以某种方式不直接与x轴同步。
  2. 不显示主要清单。 (解决)
  3. 有没有办法让图表只显示正位轴? (解决)
  4. 我做了很多NSLog来调试数据。我搜索谷歌(包括这个网站)的教程如何使用自定义标签以及如何制作散点图。)

    我有以下数据:

    (
        {
        0 = "31-Jan-2014";
        1 = 10;
    },
        {
        0 = "02-Feb-2014";
        1 = "10.07";
    },
        {
        0 = "24-Feb-2014";
        1 = "8.75";
    }
    )
    

    上面的数据我得到了奇怪的结果。请参阅附带的图表 这是我的图表的图片:

    Screenshot

    图中的

    ,y数据未映射到x轴。

    这是相关代码

        - (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
    {
        NSLog(@"Number of Records for Plot = %lu",(unsigned long)[self.plotData count]);
        return [_plotData count];
    }
    
    - (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
    {
        //NSDecimalNumber *result = [[_plotData objectAtIndex:idx]objectForKey:[NSNumber numberWithInt:fieldEnum]];
        NSNumber *result;
            switch (fieldEnum) {
            case CPTScatterPlotFieldX:
    //                NSDate * observationDate = [[_plotData objectAtIndex:idx]objectForKey:[NSNumber numberWithInt:fieldEnum]];
    //                NSTimeInterval secondsSince1970 = [_observationDate timeIntervalSince1970];
                    result = [[_plotData objectAtIndex:idx]objectForKey:[NSNumber numberWithInt:fieldEnum]];
    
                break;
            case CPTScatterPlotFieldY:
                result = [[_plotData objectAtIndex:idx] objectForKey:[NSNumber numberWithInt:fieldEnum]];
    }
    
    //result = [[_plotData objectAtIndex:idx] objectForKey:[NSNumber numberWithInt:fieldEnum]];
         NSLog(@"Number tobe  Plot with index = %@ %lu, %lu",result,(unsigned long)idx,(unsigned long)fieldEnum);
        return result;
    
    }
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self initPlot];
    
    
    
        // Do any additional setup after loading the view.
    }
    
    - (void)initPlot
    {
        //NSDate *refDate       = [NSDate dateWithNaturalLanguageString:@"12:00 Oct 29, 2009"];
        [self fetchResultOneWeek];
        [self configureHost];
        [self configureFEGraph];
        [self configurePlots];
        [self configureAxis];
    }
    
    - (void)configureHost
    {
        CGRect frame1 = CGRectMake(0,80,320,200);
        FESubView = [[CPTGraphHostingView alloc]initWithFrame:frame1];
        FESubView.allowPinchScaling = YES;
        [self.view addSubview:FESubView];
        CGRect frame2 = CGRectMake(0,410,320,400);
        GasPriceSubView = [[CPTGraphHostingView alloc]initWithFrame:frame2];
        GasPriceSubView.allowPinchScaling = YES;
        [self.view addSubview:GasPriceSubView];
    }
    
    - (void)configureFEGraph
    {
        CPTGraph *graph = [[CPTXYGraph alloc]initWithFrame:FESubView.bounds];
        [graph applyTheme:[CPTTheme themeNamed:kCPTStocksTheme]];
        FESubView.hostedGraph = graph;
    
        graph.title = @"Fuel Economy Graph";
        CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
        titleStyle.color = [CPTColor whiteColor];
        titleStyle.fontName = @"Helvetica-Bold";
        titleStyle.fontSize = 14.0f;
        graph.titleTextStyle = titleStyle;
        graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
        graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
    
        // 4 - Set padding for plot area
        [graph.plotAreaFrame setPaddingLeft:10.0f];
        [graph.plotAreaFrame setPaddingBottom:10.0f];
        [graph.plotAreaFrame setPaddingTop:10.0f];
        [graph.plotAreaFrame setPaddingRight:10.0f];
    
        // 5 - Enable user interactions for plot space
        CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
        plotSpace.allowsUserInteraction = YES;
    
    }
    
    - (void)configurePlots
    {
        NSMutableArray *dataTobePlotted = [NSMutableArray array];
    
        NSUInteger i;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"dd-MMM-YYY"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0:07"]];
        //NSTimeInterval oneDay = 24 * 60 * 60;
    
       for (i = 0;i < [[self.fullTransaction valueForKey:@"tDate"]count]; i++)
        {
            NSDate *observationDate = [[self.fullTransaction valueForKey:@"tDate"]objectAtIndex:i];
             NSLog(@"observationDate is : %@",observationDate);
            NSTimeInterval x = [observationDate timeIntervalSince1970];
            NSLog(@"x is : %f",x);
            id y = [[self.fullTransaction valueForKey:@"tFuelEconomy"]objectAtIndex:i];
             NSLog(@"y is : %@",y);
            [dataTobePlotted addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:x],[NSNumber numberWithInt:CPTScatterPlotFieldX],y,[NSNumber numberWithInt:CPTScatterPlotFieldY],nil]];
            //[dataTobePlotted addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSDecimalNumber numberWithFloat:x],[NSNumber numberWithInt:CPTScatterPlotFieldX],y,[NSNumber numberWithInt:CPTScatterPlotFieldY],nil]];
            NSLog(@" int X is %@",[NSNumber numberWithInt:CPTScatterPlotFieldX]);
            NSLog(@" int Y is %@",[NSNumber numberWithInt:CPTScatterPlotFieldY]);
            NSLog(@"ScatterPlotField X is %@",[NSNumber numberWithInt:CPTScatterPlotFieldX]);
            NSLog(@"ScatterPlotField Y is %@",[NSNumber numberWithInt:CPTScatterPlotFieldY]);
            NSLog(@"Data to be Plotted: %@",dataTobePlotted);
        }
        self.plotData = dataTobePlotted;
    
        CPTGraph *graph = FESubView.hostedGraph;
        [graph applyTheme:[CPTTheme themeNamed:kCPTStocksTheme]];
    
        CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
            //create plot
        CPTScatterPlot *fePlot = [[CPTScatterPlot alloc]init];
        fePlot.dataSource = self;
        CPTColor *feColor = [CPTColor greenColor];
        [graph addPlot:fePlot];
    
        //setup plot space
        [plotSpace scaleToFitPlots:[NSArray arrayWithObjects: fePlot,Nil]];
        plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(@"3.0") length:CPTDecimalFromString(@"10.0")];
        plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(@"8.0") length:CPTDecimalFromString(@"2.0")];
    
        //set up plot space
        //[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:fePlot,nil]];
        CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
        [xRange expandRangeByFactor:CPTDecimalFromCGFloat(2.0f)];
        plotSpace.xRange = xRange;
        CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
        [yRange expandRangeByFactor:CPTDecimalFromCGFloat(5.0f)];
        plotSpace.yRange = yRange;
    
    
    //    //create styles and symbols
        CPTMutableLineStyle *feLineStyle = [fePlot.dataLineStyle mutableCopy];
        CPTColor *areaColor = [CPTColor colorWithComponentRed:0.3 green:1.0 blue:0.3 alpha:0.3];
        CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor:areaColor endingColor:[CPTColor clearColor]];
        areaGradient.angle = -90.0f;
        CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient];
        fePlot.areaFill = areaGradientFill;
    
        feLineStyle.lineWidth = 2.5;
        feLineStyle.lineColor = feColor;
        fePlot.dataLineStyle = feLineStyle;
        CPTMutableLineStyle *feSymbolLineStyle = [CPTMutableLineStyle lineStyle];
        feSymbolLineStyle.lineColor = feColor;
        CPTPlotSymbol *feSymbol = [CPTPlotSymbol ellipsePlotSymbol];
        feSymbol.fill = [CPTFill fillWithColor:feColor];
        feSymbol.lineStyle = feSymbolLineStyle;
        feSymbol.size = CGSizeMake(6.0f, 6.0f);
        fePlot.plotSymbol = feSymbol;
    }
    
    
    - (void)configureAxis
    {
      // 1 - Create styles
            // 2 - Get axis set
        CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
        axisTitleStyle.color = [CPTColor whiteColor];
        axisTitleStyle.fontName = @"Helvetica-Bold";
        axisTitleStyle.fontSize = 12.0f;
        CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
        axisLineStyle.lineWidth = 2.0f;
        axisLineStyle.lineColor = [CPTColor whiteColor];
        CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
        axisTextStyle.color = [CPTColor whiteColor];
        axisTextStyle.fontName = @"Helvetica-Bold";
        axisTextStyle.fontSize = 11.0f;
        CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
        tickLineStyle.lineColor = [CPTColor whiteColor];
        tickLineStyle.lineWidth = 2.0f;
        CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
        tickLineStyle.lineColor = [CPTColor blackColor];
        tickLineStyle.lineWidth = 1.0f;
        CPTMutableTextStyle *labelXTextStyle = [CPTMutableTextStyle textStyle];
        labelXTextStyle.fontName = @"Helvetica";
        labelXTextStyle.fontSize = 10;
        labelXTextStyle.color = [CPTColor whiteColor];
    
        // 2 - Get Axis Set
        CPTXYAxisSet *axisSet = (CPTXYAxisSet *) FESubView.hostedGraph.axisSet;
    
        // 3 Configure x-Axis
        CPTXYAxis *x          = axisSet.xAxis;
        x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"4");
        x.title = @"Date";
        x.titleTextStyle = axisTitleStyle;
        x.titleOffset = 47.0f;
        //x.title = @"Date";
        x.minorTicksPerInterval       = 0;
        x.majorTickLength = 10.0f;
        x.tickDirection = CPTSignPositive;
    
        //NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15],
        //                                [NSDecimalNumber numberWithInt:20], [NSDecimalNumber numberWithInt:25],[NSDecimalNumber numberWithInt:30],nil];
    
        NSInteger i;
        NSMutableArray *customTickLocations = [NSMutableArray array];
        for (i = 0; i < [[self.fullTransaction valueForKey:@"tDate"]count];i++)
        {
            [customTickLocations addObject:[NSDecimalNumber numberWithInt:((i+1)*5)]];
        }
    
        CPTPlotRange *xAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(@"0.0") length:CPTDecimalFromString(@"24.0")];
          x.visibleRange=xAxisRange;
    
        NSLog(@"dates are %@", [self.fullTransaction valueForKey:@"tDate"]);
        NSLog(@"Transaction FE %@", [self.fullTransaction valueForKey:@"tFuelEconomy"]);
        NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[self.plotData count]];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"dd-MMM-YYY"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0:07"]];
        NSMutableSet *xLocations = [NSMutableSet setWithCapacity:[[self.fullTransaction valueForKey:@"tDate"]count]];
        x.labelingPolicy=CPTAxisLabelingPolicyNone;
        x.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue];
        NSInteger location;
        for (location = 0; location < [[self.fullTransaction valueForKey:@"tDate"]count];location++)
        {
            CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [dateFormatter stringFromDate:[[self.fullTransaction valueForKey:@"tDate"] objectAtIndex:location]] textStyle:labelXTextStyle];
            NSLog(@"Custom Labels = %@",newLabel);
            NSLog(@"Real Date = %@",[[self.fullTransaction valueForKey:@"tDate"] objectAtIndex:location]);
            NSLog(@"After assigned date = %@",[dateFormatter stringFromDate:[[self.fullTransaction valueForKey:@"tDate"] objectAtIndex:location]]);
            newLabel.tickLocation = CPTDecimalFromInt([[customTickLocations objectAtIndex:location] integerValue]);
            newLabel.offset =  2;
            newLabel.rotation = (1*M_PI)/4;
            [customLabels addObject:newLabel];
            [xLocations addObject:[NSNumber numberWithInteger:[[customTickLocations objectAtIndex:location] integerValue]]];
    
        }
        x.axisLabels = [NSSet setWithArray:customLabels];
        NSLog(@"Custom Labels = %@",customLabels);
        x.majorTickLocations = xLocations;
        //x.majorTickLocations = customTickLocations;
        x.majorTickLength = 5.0f;
        NSLog(@"Major TickLocations: %@",customTickLocations);
    
        CPTXYAxis *y = axisSet.yAxis;
        //y.orthogonalCoordinateDecimal = CPTDecimalFromCGFloat(0.0);
        y.majorIntervalLength         = CPTDecimalFromString(@"1.0");
        y.minorTicksPerInterval       = 5;
        y.majorTickLength = 5.0f;
        y.preferredNumberOfMajorTicks = 5;
        y.labelTextStyle = labelXTextStyle;
    }
    

    以下是idlog和id y数据

    的nslog结果
    2014-02-25 16:09:01.855 Fuel Eco[12079:70b] x is : 31-Jan-2014
    2014-02-25 16:09:01.855 Fuel Eco[12079:70b] y is : 10
    2014-02-25 16:09:01.855 Fuel Eco[12079:70b]  int X is 0
    2014-02-25 16:09:01.856 Fuel Eco[12079:70b]  int Y is 1
    2014-02-25 16:09:01.856 Fuel Eco[12079:70b] ScatterPlotField X is 0
    2014-02-25 16:09:01.856 Fuel Eco[12079:70b] ScatterPlotField Y is 1
    2014-02-25 16:09:01.857 Fuel Eco[12079:70b] Data to be Plotted: (
            {
            0 = "31-Jan-2014";
            1 = 10;
        }
    )
    2014-02-25 16:09:01.857 Fuel Eco[12079:70b] x is : 02-Feb-2014
    2014-02-25 16:09:01.857 Fuel Eco[12079:70b] y is : 10.07
    2014-02-25 16:09:01.858 Fuel Eco[12079:70b]  int X is 0
    2014-02-25 16:09:01.858 Fuel Eco[12079:70b]  int Y is 1
    2014-02-25 16:09:01.858 Fuel Eco[12079:70b] ScatterPlotField X is 0
    2014-02-25 16:09:01.859 Fuel Eco[12079:70b] ScatterPlotField Y is 1
    2014-02-25 16:09:01.859 Fuel Eco[12079:70b] Data to be Plotted: (
            {
            0 = "31-Jan-2014";
            1 = 10;
        },
            {
            0 = "02-Feb-2014";
            1 = "10.07";
        }
    )
    2014-02-25 16:09:01.859 Fuel Eco[12079:70b] x is : 24-Feb-2014
    2014-02-25 16:09:01.860 Fuel Eco[12079:70b] y is : 8.75
    2014-02-25 16:09:01.860 Fuel Eco[12079:70b]  int X is 0
    2014-02-25 16:09:01.860 Fuel Eco[12079:70b]  int Y is 1
    2014-02-25 16:09:01.861 Fuel Eco[12079:70b] ScatterPlotField X is 0
    2014-02-25 16:09:01.861 Fuel Eco[12079:70b] ScatterPlotField Y is 1
    2014-02-25 16:09:01.861 Fuel Eco[12079:70b] Data to be Plotted: (
            {
            0 = "31-Jan-2014";
            1 = 10;
        },
            {
            0 = "02-Feb-2014";
            1 = "10.07";
        },
            {
            0 = "24-Feb-2014";
            1 = "8.75";
        }
    )
    

    下面是nslog显示“ - (NSNumber *)numberForPlot:(CPTPlot *)”绘图字段结果的结果:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx“with data,idx,fieldEnum。< / p>

    2014-02-25 16:09:01.862 Fuel Eco[12079:70b] Number of Records for Plot = 3
    2014-02-25 16:09:01.862 Fuel Eco[12079:70b] Number of Records for Plot = 3
    2014-02-25 16:09:01.863 Fuel Eco[12079:70b] Number tobe  Plot with index = 31-Jan-2014 0, 0
    2014-02-25 16:09:01.863 Fuel Eco[12079:70b] Number tobe  Plot with index = 02-Feb-2014 1, 0
    2014-02-25 16:09:01.863 Fuel Eco[12079:70b] Number tobe  Plot with index = 24-Feb-2014 2, 0
    2014-02-25 16:09:01.864 Fuel Eco[12079:70b] Number of Records for Plot = 3
    2014-02-25 16:09:01.864 Fuel Eco[12079:70b] Number tobe  Plot with index = 10 0, 1
    2014-02-25 16:09:01.864 Fuel Eco[12079:70b] Number tobe  Plot with index = 10.07 1, 1
    2014-02-25 16:09:01.865 Fuel Eco[12079:70b] Number tobe  Plot with index = 8.75 2, 1
    2014-02-25 16:09:01.865 Fuel Eco[12079:70b] Number of Records for Plot = 3
    

    全部谢谢

    更新*** 我重写了代码变成这样,它的工作原理:

    -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
    {
        switch (fieldEnum)
        {
            case CPTScatterPlotFieldX:
            {
                NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
                return num;
            }
            case CPTScatterPlotFieldY:
            {
                if ([plot.identifier isEqual:@"Honda Plot"])
                {
                    NSDecimalNumber *num = [[plotData1 objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
                    return num;
                }
                else
                {
                    NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
                    return num;
                }
            }
        }
        return nil;
    }
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self fetchResult];
        [self filterarray];
        [self filterArray1];
    
        //CGRect frame1 = CGRectMake(0,200,300,600);
        [self generateData];
        //FESubView = [[CPTGraphHostingView alloc]initWithFrame:frame1];
        [self renderInLayer:FESubView withTheme:Nil animated:NO];
        }
    
    -(void)generateData
    {
    
        if ( !plotData ) {
            //const NSTimeInterval oneDay = 24 * 60 * 60 ;
            gregorian = [self setCalendar];
           NSDateComponents *dateComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit  | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit     ) fromDate:[[self.fullTransaction valueForKey:@"tDate" ] objectAtIndex:0]];
    NSDate *refDate = [gregorian dateFromComponents:dateComponents];
    
            NSDate *dataDate = [[NSDate alloc]init];
    
            NSDateComponents *comps;
    
            int days;
    
            // Add some data
            NSMutableArray *newData = [NSMutableArray array];
            NSMutableArray *newData1 = [NSMutableArray array];
            NSUInteger i;
    
    
    
            for ( i = 0; i < [[self.fullTransaction valueForKey:@"tDate"]count]; i++ ) {
    
                dataDate = [[self.fullTransaction valueForKey:@"tDate"]objectAtIndex:i];
               NSLog(@"RefDate is: %@",refDate);
                NSLog(@"DateDate is: %@",dataDate);
                comps = [gregorian components:NSDayCalendarUnit fromDate:refDate toDate:dataDate  options:0];
                days = [comps day];
                NSTimeInterval x = oneDay *days;
                //id y             = [[self.fullTransaction valueForKey:@"tFuelEconomy"]objectAtIndex:i];
                int counts = [self.filteredTransaction count];
                id y;
                id z;
                int count1 = [self.filteredTransaction1 count];
                if (i < counts)
                {
                y             = [[self.filteredTransaction valueForKey:@"tFuelEconomy"]objectAtIndex:i];
                }else y = NULL;
                if (i < count1)
                {
                    z            = [[self.filteredTransaction1 valueForKey:@"tFuelEconomy"]objectAtIndex:i];
                }else z = NULL;
    
    
               [newData addObject:
                 [NSDictionary dictionaryWithObjectsAndKeys:
                  [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX],
                  y, [NSNumber numberWithInt:CPTScatterPlotFieldY],
                  nil]];
                [newData1 addObject:
                 [NSDictionary dictionaryWithObjectsAndKeys:
                  [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX],
                  z, [NSNumber numberWithInt:CPTScatterPlotFieldY],
                  nil]];
            }
            plotData = newData;
            plotData1 = newData1;
            NSLog(@"Data are: %@",plotData);
            NSLog(@"Data are: %@",plotData1);
        }
    }
    
    -(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme animated:(BOOL)animated
    {
        // 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.
        CGRect frame1 = CGRectMake(10,150,320,250);
        FESubView = [[CPTGraphHostingView alloc]initWithFrame:frame1];
        FESubView.allowPinchScaling = YES;
        [self.view addSubview:FESubView];
    
        //NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    
        gregorian = [self setCalendar];
        NSDateComponents *dateComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit  | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit     ) fromDate:[[self.fullTransaction valueForKey:@"tDate" ] objectAtIndex:0]];
    
        NSDate *refDate = [gregorian dateFromComponents:dateComponents];
    
        NSLog(@"RefDate is :%@",refDate);
    
    #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
        CGRect bounds = layerHostingView.bounds;
    #else
        CGRect bounds = NSRectToCGRect(layerHostingView.bounds);
    #endif
    
        CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:bounds];
        [graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
        FESubView.hostedGraph = graph;
        graph.title = @"Fuel Economy Graph";
        CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
        titleStyle.color = [CPTColor whiteColor];
        titleStyle.fontName = @"Helvetica-Bold";
        titleStyle.fontSize = 14.0f;
        graph.titleTextStyle = titleStyle;
        graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
        graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
        // 4 - Set padding for plot area
        [graph.plotAreaFrame setPaddingLeft:40.0f];
        [graph.plotAreaFrame setPaddingBottom:65.0f];
        [graph.plotAreaFrame setPaddingTop:3.0f];
        [graph.plotAreaFrame setPaddingRight:3.0f];
    
        graph.legend = [CPTLegend legendWithGraph:graph];
        graph.legend.fill = [CPTFill fillWithColor:[CPTColor darkGrayColor]];
        graph.legend.cornerRadius = 5.0;
        graph.legend.swatchSize = CGSizeMake(25.0, 25.0);
        graph.legendAnchor = CPTRectAnchorCenter;
    
        graph.legendDisplacement = CGPointMake(2.40, 12.0);
    
        // Setup scatter plot space
        CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
        NSTimeInterval xLow       = 0.0f;
        plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow) length:CPTDecimalFromFloat(oneDay * 10)];
        plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(5.0) length:CPTDecimalFromFloat(10.0f)];
    
        //symbol
    
        CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
        [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1)];
    //   
        plotSpace.allowsUserInteraction = YES;
    
        // Create a plot that uses the data source method
        CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
        dataSourceLinePlot.identifier = @"Nisan Plot";
        CPTColor *feColor = [CPTColor redColor];
        CPTMutableLineStyle *feLineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
        feLineStyle.lineWidth = 2.5;
        feLineStyle.lineColor = feColor;
        dataSourceLinePlot.dataLineStyle = feLineStyle;
        CPTMutableLineStyle *feSymbolLineStyle = [CPTMutableLineStyle lineStyle];
        feSymbolLineStyle.lineColor = feColor;
        CPTPlotSymbol *feSymbol = [CPTPlotSymbol ellipsePlotSymbol];
        feSymbol.fill = [CPTFill fillWithColor:feColor];
        feSymbol.lineStyle = feSymbolLineStyle;
        feSymbol.size = CGSizeMake(6.0f, 6.0f);
        dataSourceLinePlot.plotSymbol = feSymbol;
        CPTMutableTextStyle *feLabelStyle = [CPTMutableTextStyle textStyle];
        feLabelStyle.fontSize = 10;
        feLabelStyle.color = [CPTColor whiteColor];
        dataSourceLinePlot.labelTextStyle = feLabelStyle;
        dataSourceLinePlot.title = @"Nissan Terano";
    
        dataSourceLinePlot.dataSource = self;
        [graph addPlot:dataSourceLinePlot];
    
        CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
        axisTextStyle.color = [CPTColor whiteColor];
        axisTextStyle.fontName = @"Helvetica-Bold";
        axisTextStyle.fontSize = 8.0f;
        CPTScatterPlot *dataSourceLinePlot1 = [[CPTScatterPlot alloc] init];
        dataSourceLinePlot1.identifier = @"Honda Plot";
        CPTColor *feColor1 = [CPTColor blueColor];
        CPTMutableLineStyle *feLineStyle1 = [dataSourceLinePlot1.dataLineStyle mutableCopy];
        feLineStyle1.lineWidth = 2.5;
        feLineStyle1.lineColor = feColor1;
        dataSourceLinePlot1.dataLineStyle = feLineStyle1;
        CPTMutableLineStyle *feSymbolLineStyle1 = [CPTMutableLineStyle lineStyle];
        feSymbolLineStyle1.lineColor = feColor1;
        CPTPlotSymbol *feSymbol1 = [CPTPlotSymbol ellipsePlotSymbol];
        feSymbol1.fill = [CPTFill fillWithColor:feColor1];
        feSymbol1.lineStyle = feSymbolLineStyle1;
        feSymbol1.size = CGSizeMake(6.0f, 6.0f);
        dataSourceLinePlot1.plotSymbol = feSymbol1;
        //CPTTextStyle *test = [CPTTextStyle textStyle];
        //test.fontSize = 12;
        CPTMutableTextStyle *feLabelStyle1 = [CPTMutableTextStyle textStyle];
        feLabelStyle1.fontSize = 10;
        feLabelStyle1.color = [CPTColor whiteColor];
        dataSourceLinePlot1.labelTextStyle = feLabelStyle1;
    
        dataSourceLinePlot1.dataSource = self;
        [graph addPlot:dataSourceLinePlot1];
    dataSourceLinePlot1.title = @"Honda Jazz";
    //    
        // Axes
        CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
        CPTXYAxis *x          = axisSet.xAxis;
        x.majorIntervalLength         = CPTDecimalFromFloat(oneDay);
        x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"5");
        x.minorTicksPerInterval       = 0;
        x.labelTextStyle = axisTextStyle;
        //x.axisLineStyle = axisLineStyle;
        //x.axisLineStyle = feLabelStyle;
        dateFormatter = [self setDateFormatterLabel];
        //dateFormatter.dateStyle = kCFDateFormatterShortStyle;
    
        CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
        timeFormatter.referenceDate = refDate;
        x.labelFormatter            = timeFormatter;
        x.labelRotation            = M_PI / 4;
        x.tickLabelDirection = CPTSignPositive;
        x.tickDirection = CPTSignPositive;
        //x.labelTextStyle = feLabelStyle;
        //x.visibleRange=plotSpace.xRange;
        x.labelOffset = -70;
    
        CPTXYAxis *y = axisSet.yAxis;
        y.majorIntervalLength         = CPTDecimalFromString(@"5.0");
        y.minorTicksPerInterval       = 10;
        y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"-1");
    
        }
    

1 个答案:

答案 0 :(得分:0)

  1. 我不确定你的意思。你能更清楚地解释一下这个问题吗?

  2. 您永远不会向xLocations集添加任何位置。如果customTickLocations数组包含您需要的所有位置,只需直接设置刻度位置:

    x.majorTickLocations = [NSSet setWithArray:customTickLocations];
    
  3. 您为什么使用-expandRangeByFactor:?如果您知道所需的绘图范围,只需直接设置:

    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0)
                                                length:CPTDecimalFromDouble(10.0)];