2017-12-27 45 views
0

私は非常に混雑したxyグラフを持っていますが、x軸ラベルをデフォルトの水平軸の代わりに垂直に表示しようとしています。私はこれを行う方法に関する文書を見つけることができません。誰でもこれを行う方法を教えてもらえますか、適切なドキュメントを教えてください。 x.labelrotationを調整しましたが、x軸のラベルを回転します ありがとうございます。コアプロットx軸ラベル垂直

#pragma mark - Chart behavior 
-(void)initPlotThree { 
    [self configureHostThree]; 
    [self configureGraphThree]; 
    [self configurePlotsThree]; 
    [self configureAxesThree]; 
} 

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

-(void)configureGraphThree { 
    // 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 = @"Tremor"; 
    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)configurePlotsThree { 
    // 1 - Get graph and plot space 
    CPTGraph *graph = self.hostView.hostedGraph; 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 

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

    // 3 - Set up plot space 
    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:tremorPlot, 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 *tremorLineStyle = [tremorPlot.dataLineStyle mutableCopy]; 
    tremorLineStyle.lineWidth = 2.0; 
    tremorLineStyle.lineColor = tremorColor; 
    tremorPlot.dataLineStyle = tremorLineStyle; 
    CPTMutableLineStyle *tremorSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
    tremorSymbolLineStyle.lineColor = tremorColor;} 

-(void)configureAxesThree { 
    // 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 = @""; 
    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; 
    x.labelRotation = .6; 
    x.labelOffset = 15; 

    CGFloat dateCount = [self.dates count]; 
    NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount]; 
    NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount]; 
    NSInteger i = 0; 
    for (NSString *date in self.dates) { 
     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; 

    // CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 
    axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:15.0]; // 8-22 

    // 4 - Configure y-axis 
    CPTAxis *y = axisSet.yAxis; 
    y.title = @""; 
    y.titleTextStyle = axisTitleStyle; 
    y.titleOffset = -40.0f; 
    y.axisLineStyle = axisLineStyle; 
    y.majorGridLineStyle = nil; 
    y.labelingPolicy = CPTAxisLabelingPolicyNone; 
    y.labelTextStyle = nil; 
    y.labelOffset = 16.0f; 
    y.majorTickLineStyle = nil; 
    y.majorTickLength = 4.0f; 
    y.minorTickLength = 1.0f; 
    y.tickDirection = CPTSignPositive; 
    NSInteger majorIncrement = 2; 
    NSInteger minorIncrement = 1; 
    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]]; 
      [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]]; 
     } else { 
      [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]]; 
      [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]]; 
     } 
    } 
    y.axisLabels = yLabels; 
    y.majorTickLocations = yMajorLocations; 
    y.minorTickLocations = yMinorLocations; 

} 

答えて

0

自動ラベリングポリシーのいずれかを使用する際にラベルを回転させるために、x軸上labelRotationを設定します。カスタムラベルを作成するときは、各ラベルにrotationを直接設定する必要があります。

+0

ありがとうございます、私はこのx.labelRotation = .25; x軸のラベルはまだ水平に表示されています。助言がありますか? – SlimPickens

+0

詳細を見ることができるように、コードを質問に追加してください。角度の測定値はラジアンであることに注意してください。 –

+0

上記の私のコードを私の元の質問で見てください。あなたの助けをありがとう – SlimPickens

関連する問題