如何在 CPT 散点图的 x 轴上显示自定义对象

how to display Custom objects on x-axis in CPTScatterPlot graph

我已经在 y 轴上实现了自定义对象(来自网络服务),从 y 正交 40 开始,y 轴上的间隔为 20,并且它工作正常。 目前我们在 x 轴上显示 1 天的时间间隔。但是现在我想以 20 的间隔显示自定义对象(即日期,但有可能在同一日期出现 2 个不同的时间)(来自 web 服务),如在 y 轴上所做的那样。 我尝试了类似于 y 轴的方法,但没有用。所有值都显示在单个位置,但不是以 20 为间隔显示。 示例服务器响应如下所示,仅针对 x 轴。 例如:-

{
      date : “2015-12-25 6:30:00”,
      date : “2015-12-30 14:00:00”,
      date : “2016-01-25 5:30:00”,
      date : “2016-01-25 7:00:00”,
      date : “2016-01-10 10:00:00”,
      date : “2016-01-10 12:30:00”,
      date : “2016-01-11 11:00:00”,
      date : “2016-01-12 16:30:00”
}

这是我的代码:

-(void)reloadData
{
    if ( !self.graph ) {
        CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
        self.graph = newGraph;

        newGraph.paddingTop    = 0.0;
        newGraph.paddingBottom = 0.0;
        newGraph.paddingLeft   = 0.0;
        newGraph.paddingRight  = 0.0;


        self.graph.plotAreaFrame.paddingTop = 2.0f;
        self.graph.plotAreaFrame.paddingRight = 8.0f;
        self.graph.plotAreaFrame.paddingBottom = 30.0f;
        self.graph.plotAreaFrame.paddingLeft = 42.0f;


        //self.graph.backgroundColor = [[UIColor greenColor] CGColor];

        self.graph.paddingTop = 2.0f;
        self.graph.paddingRight = 2.0f;
        self.graph.paddingBottom = 2.0f;
        self.graph.paddingLeft = 2.0f;



        CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] initWithFrame:newGraph.bounds];
        dataSourceLinePlot.identifier = @"Systolic";

        dataSourceLinePlot.dataSource = self;
        dataSourceLinePlot.delegate   = self;

        CPTColor *areaColor = [CPTColor colorWithComponentRed:249.0/255.0 green:183.0/255.0 blue:183.0/255.0 alpha:.7];
        CPTFill *areaGradientFill = [CPTFill fillWithColor:areaColor];
        [dataSourceLinePlot setAreaFill:areaGradientFill];
        [dataSourceLinePlot setAreaBaseValue:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInt(60)]];//[NSNumber numberWithInt:CPTDecimalFromInt(0)]];

        CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
        CPTColor *aaplColor = [CPTColor redColor];
        lineStyle.lineColor = aaplColor;
        CPTXYAxisSet axisSet = (CPTXYAxisSet ) newGraph.axisSet;
        /*

         // 3 - Configure x-axis
         CPTAxis *x = axisSet.xAxis;
         */

        // set the majorGridLinestyleProperty by this line as.
        CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
        gridLineStyle.lineColor = [CPTColor grayColor];
        gridLineStyle.lineWidth = 1.0f;

        gridLineStyle.dashPattern = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:2], nil];
        gridLineStyle.patternPhase=0.0f;



        dataSourceLinePlot.dataLineStyle = lineStyle;

        axisSet.yAxis.majorGridLineStyle = gridLineStyle;
        //        axisSet.yAxis.axisLineStyle = axisLineStyle;

        axisSet.xAxis.majorGridLineStyle = gridLineStyle;
        // axisSet.xAxis.axisLineStyle = axisLineStyle;

        dataSourceLinePlot.dataSource = self;
        [newGraph addPlot:dataSourceLinePlot];



        //-------------------------------------------------------------------------------------

        CPTScatterPlot *dataSourceLinePlot2 = [[CPTScatterPlot alloc] initWithFrame:newGraph.bounds];
        dataSourceLinePlot2.identifier = @"Diastolic";

        dataSourceLinePlot2.dataSource = self;
        dataSourceLinePlot2.delegate   = self;

        CPTColor *areaColor1 = [CPTColor colorWithComponentRed:229.0/255.0 green:87.0/255.0 blue:87.0/255.0 alpha:.7];

        CPTFill *areaGradientFill1 = [CPTFill fillWithColor:areaColor1];
        [dataSourceLinePlot2 setAreaFill:areaGradientFill1];
        [dataSourceLinePlot2 setAreaBaseValue:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInt(60)]];

        CPTMutableLineStyle *lineStyle1 = [dataSourceLinePlot.dataLineStyle mutableCopy];
        CPTColor *aaplColor1 = [CPTColor redColor];
        lineStyle1.lineColor = aaplColor1;
        dataSourceLinePlot2.dataLineStyle = lineStyle1;

        dataSourceLinePlot2.dataSource = self;
        [newGraph addPlot:dataSourceLinePlot2];
    }

    CPTXYGraph *theGraph = self.graph;
    self.graphHost.hostedGraph = theGraph;

    CPTXYPlotSpace plotSpace = (CPTXYPlotSpace )theGraph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.delegate = self;


    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setGeneratesDecimalNumbers:NO];

    NSTimeInterval oneDay = 24  60  60;
    NSTimeInterval xLow = 0.0f;

    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromFloat(xLow)]
                                                    length:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromFloat(oneDay*5.0f)]];

    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:@50.0 length:@(120)];

    // Axes
    CPTXYAxisSet axisSet = (CPTXYAxisSet )theGraph.axisSet;


    NSDate refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:31556926  10];
    // added for date
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM"];
    // dateFormatter.dateStyle = kCFDateFormatterShortStyle;
    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
    NSLog(@"Ref date: %@", refDate);
    timeFormatter.referenceDate = refDate;
    axisSet.xAxis.labelFormatter = timeFormatter;


    CPTXYAxis *x = axisSet.xAxis;
    x.majorIntervalLength   = [NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromFloat(oneDay)];
    x.orthogonalPosition    = @60.0;
    x.minorTicksPerInterval = 1;

    CPTXYAxis *y  = axisSet.yAxis;
    y.majorIntervalLength   = @20;
    y.majorTickLineStyle    = nil;
    y.minorTicksPerInterval = 4;
    y.minorTickLineStyle    = nil;
    y.orthogonalPosition    = @60.0;
    y.tickDirection = CPTSignPositive;
    [theGraph reloadData];
}

请让我知道如何实现这一目标。 提前致谢。

您可以使用 -[NSDateFormatter dateFromString:] 将从服务器接收到的日期字符串转换为 NSDate 对象。看起来图表已经设置为显示日期。