2017-08-20 4 views
0

私のx軸目盛りのラベルは、単独で消えているようです。時々、彼らは彼らが眺めから消えた他の時間をよく見せます。私はそれが私のデータスケーリングを行う何かを持っていると仮定しますが、私は多くのことを試して、私はそれを修正することができます。私のコードは次のとおりです(私はかなり直接それを の例から直接コピーしました)。私の価値観はすべて肯定的です。 提案がありましたら教えてください。おかげさまで coreplotのx軸ティックタイトルを失う

-(void)configureHostFour { 
    self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds]; 
    self.hostView.allowPinchScaling = YES; 
    [self.myViewFour addSubview:self.hostView]; 
    CPTGraphHostingView *BarGraphView = [[CPTGraphHostingView alloc] init]; 
    self.hostView.frame = CGRectMake(self.myViewFour.bounds.origin.x, self.myViewFour.bounds.origin.y, self.myViewFour.bounds.size.width, self.myViewFour.bounds.size.height); 
    [self.hostView addSubview:BarGraphView]; 
} 

-(void)configureGraphFour { 
    // 1 - Create the graph 
    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds]; 
    //  [graph applyTheme:[CPTTheme themeNamed:kCPTSlateTheme]]; 
    [graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]]; 

    self.hostView.hostedGraph = graph; 
    // 2 - Set graph title 
    NSString *title = @"Heart Rate"; 
    graph.title = title; 
    // 3 - Create and set text style 
    CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle]; 
    titleStyle.color = [CPTColor blackColor]; 
    titleStyle.fontName = @"Helvetica-Bold"; 
    titleStyle.fontSize = 12.0f; 
    graph.titleTextStyle = titleStyle; 
    graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop; 
    graph.titleDisplacement = CGPointMake(0.0f, 17.0f); 
    // 4 - Set padding for plot area 
    [graph.plotAreaFrame setPaddingLeft:1.0f]; 
    [graph.plotAreaFrame setPaddingBottom:1.0f]; 
    // 5 - Enable user interactions for plot space 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 
    plotSpace.allowsUserInteraction = YES; 
} 

-(void)configurePlotsFour { 
    // 1 - Get graph and plot space 
    CPTGraph *graph = self.hostView.hostedGraph; 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 

    // 2 - Create the plots 
    CPTScatterPlot *hrPlot = [[CPTScatterPlot alloc] init]; 
    hrPlot.dataSource = self; 
    hrPlot.identifier = hrSCORE; 
    CPTColor *hrColor = [CPTColor blueColor]; 
    [graph addPlot:hrPlot toPlotSpace:plotSpace]; 

    CPTScatterPlot *avghrPlot = [[CPTScatterPlot alloc] init]; 
    avghrPlot.dataSource = self; 
    avghrPlot.identifier = hrSCOREAVG; 
    CPTColor *avghrColor = [CPTColor redColor]; 
    [graph addPlot:avghrPlot toPlotSpace:plotSpace]; 


    // 3 - Set up plot space 
    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:hrPlot, avghrPlot, nil]]; 
    CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy]; 
    [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)]; 
    plotSpace.xRange = xRange; 
    CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy]; 
    [yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)]; 
    plotSpace.yRange = yRange; 

    // 4 - Create styles and symbols 
    CPTMutableLineStyle *hrLineStyle = [hrPlot.dataLineStyle mutableCopy]; 
    hrLineStyle.lineWidth = 2.0; 
    hrLineStyle.lineColor = hrColor; 
    hrPlot.dataLineStyle = hrLineStyle; 
    CPTMutableLineStyle *hrSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
    hrSymbolLineStyle.lineColor = hrColor; 

    CPTMutableLineStyle *avghrLineStyle = [hrPlot.dataLineStyle mutableCopy]; 
    avghrLineStyle.lineWidth = 2.0; 
    avghrLineStyle.lineColor = avghrColor; 
    avghrPlot.dataLineStyle = avghrLineStyle; 
    CPTMutableLineStyle *avghrSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
    avghrSymbolLineStyle.lineColor = avghrColor; 

} 

-(void)configureAxesFour{ 
    // 1 - Create styles 
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle]; 
    axisTitleStyle.color = [CPTColor blackColor]; 
    axisTitleStyle.fontName = @"Helvetica-Bold"; 
    axisTitleStyle.fontSize = 10.0f; 
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle]; 
    axisLineStyle.lineWidth = .01f; 
    axisLineStyle.lineColor = [CPTColor blackColor]; 

    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init]; 
    axisTextStyle.color = [CPTColor blackColor]; 
    axisTextStyle.fontName = @"Helvetica-Bold"; 
    axisTextStyle.fontSize = 9.0f; 
    CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle]; 
    tickLineStyle.lineColor = [CPTColor blackColor]; 
    tickLineStyle.lineWidth = 1.0f; 
    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle]; 
    tickLineStyle.lineColor = [CPTColor blackColor]; 
    tickLineStyle.lineWidth = 1.0f; 
    // 2 - Get axis set 
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet; 
    // 3 - Configure x-axis 
    CPTAxis *x = axisSet.xAxis; 
    x.title = @"Day of Month"; 
    x.titleTextStyle = axisTitleStyle; 
    x.titleOffset = 20.0f; 
    x.axisLineStyle = axisLineStyle; 
    x.labelingPolicy = CPTAxisLabelingPolicyNone; 
    x.labelTextStyle = axisTextStyle; 
    x.majorTickLineStyle = axisLineStyle; 
    x.majorTickLength = 4.0f; 
    x.tickDirection = CPTSignNegative; 

    CGFloat dateCount = [self.dateArray count]; 
    NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount]; 
    NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount]; 
    NSInteger i = 0; 
    for (NSString *date in self.dateArray) { 
     CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date textStyle:x.labelTextStyle]; 
     CGFloat location = i++; 
     label.tickLocation = CPTDecimalFromCGFloat(location); 
     label.offset = x.majorTickLength; 
     if (label) { 
      [xLabels addObject:label]; 
      [xLocations addObject:[NSNumber numberWithFloat:location]]; 
     } 
    } 
    x.axisLabels = xLabels; 
    x.majorTickLocations = xLocations; 

    // 4 - Configure y-axis 
    CPTAxis *y = axisSet.yAxis; 
    y.title = @""; 
    y.titleTextStyle = axisTitleStyle; 
    y.titleOffset = -40.0f; 
    y.axisLineStyle = axisLineStyle; 
    y.majorGridLineStyle = gridLineStyle; 
    y.labelingPolicy = CPTAxisLabelingPolicyNone; 
    y.labelTextStyle = axisTextStyle; 
    y.labelOffset = 16.0f; 
    y.majorTickLineStyle = axisLineStyle; 
    y.majorTickLength = 4.0f; 
    y.minorTickLength = 2.0f; 
    y.tickDirection = CPTSignPositive; 
    NSInteger majorIncrement = self.hrTick; 
    NSInteger minorIncrement = self.hrHalfTick; 
    CGFloat yMax = 700.0f; // should determine dynamically based on max price 
    NSMutableSet *yLabels = [NSMutableSet set]; 
    NSMutableSet *yMajorLocations = [NSMutableSet set]; 
    NSMutableSet *yMinorLocations = [NSMutableSet set]; 
    for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) { 
     NSUInteger mod = j % majorIncrement; 
     if (mod == 0) { 
      CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%li", (long)j] textStyle:y.labelTextStyle]; 
      NSDecimal location = CPTDecimalFromInteger(j); 
      label.tickLocation = location; 
      label.offset = -y.majorTickLength - y.labelOffset; 
      if (label) { 
       [yLabels addObject:label]; 
      } 
      [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]]; 
     } else { 
      [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]]; 
     } 
    } 
    y.axisLabels = yLabels; 
    y.majorTickLocations = yMajorLocations; 
    y.minorTickLocations = yMinorLocations; 
} 

#pragma mark - CPTPlotDataSource methods 
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { 
    return [self.dateArray count]; 
} 

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { 
    NSInteger valueCount = [self.dateArray count]; 
    switch (fieldEnum) { 
     case CPTScatterPlotFieldX: 
      if (index < valueCount) { 
       return [NSNumber numberWithUnsignedInteger:index]; 
      } 
      break; 

     case CPTScatterPlotFieldY: 

      if ([plot.identifier isEqual:hrSCORE] == YES) { 
       NSLog(@"hrscore"); 
       return [self.hrArray objectAtIndex:index]; 
      } else if ([plot.identifier isEqual:hrSCOREAVG] == YES) { 
       NSLog(@"hravg"); 
       return [self.avgHrArray objectAtIndex:index]; 
      } else if ([plot.identifier isEqual:fgSCORE] == YES) { 
       NSLog(@"gfscore"); 

       return [self.fgArray objectAtIndex:index]; 
      } else if ([plot.identifier isEqual:fgSCOREAVG] == YES) { 
       NSLog(@"fgavg"); 

       return [self.avgFgArray objectAtIndex:index]; 
      } else if ([plot.identifier isEqual:weightSCORE] == YES) { 
       NSLog(@"weight"); 

       return [self.weightArray objectAtIndex:index]; 
      } else if ([plot.identifier isEqual:weightSCOREAVG] == YES) { 
       NSLog(@"weight av"); 

       return [self.avgWeightArray objectAtIndex:index]; 
      } else if ([plot.identifier isEqual:mapSCORE] == YES) { 
       NSLog(@"map"); 

       return [self.mapArray objectAtIndex:index]; 
      } else if ([plot.identifier isEqual:mapSCOREAVG] == YES) { 
       NSLog(@"map avg"); 

       return [self.avgMapArray objectAtIndex:index]; 
      } 

      break; 
    } 
    return [NSDecimalNumber zero]; 
} 

答えて

0

ティックの位置とラベルはゼロ(0)から始まる整数値に配置されています。 xRangeが何であるかは言わないが、そのlengthが1未満であり、ティックの場所の1つをカバーしていない場合、ダニやラベルは表示されません。

+0

ありがとうございます。 x目盛りのラベルはうまく表示されていますが、枠外に表示されています。彼らはYゼロのすぐ下に表示され、私はそれらにスクロールすることができます。 Y軸を圧縮する方法はありますか?たとえば、yの値が50〜100の場合、y軸(グラフ全体)は50〜100しか表示されません。x軸は表示されません。ご協力いただきありがとうございます。 – SlimPickens

+0

軸の拘束を使用して、x軸を所定の位置にロックすることができます。 –

+0

これにはコードスニペットを含めてください。私はこれを行う方法を理解することはできません。どうもありがとう! – SlimPickens