2014-12-04 1 views
0

line graph.i를 그리기 위해 코어 플롯을 사용하는 경우 간격이 다른 두 개의 y 축이 필요합니다.ios 8에서 coreplot을 사용하여 y1 및 y2 축을 그리는 코드 작성 방법은 무엇입니까?

(x 축) i 다음 코드를 사용했습니다.

#pragma mark - Chart behavior 
    -(void)initPlotForDancis { 
     [self configureHostDancis]; 
     [self configureGraphDancis]; 
     [self configurePlotsDancis]; 
     [self configureAxesDancis]; 
    } 

    -(void)configureHostDancis { 
     self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:CGRectMake(0 ,0, dancisView.frame.size.width, dancisView.frame.size.height-150)]; 
     self.hostView.allowPinchScaling = YES; 
     [dancisView addSubview:self.hostView]; 
    } 

    -(void)configureGraphDancis { 
     // 1 - Create the graph 
     CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds]; 
     [graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]]; 
     self.hostView.hostedGraph = graph; 
     // 2 - Set graph title 
     NSString *title = @"Dancis Graph"; 
     graph.title = title; 
     // 3 - Create and set text style 
     CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle]; 
     titleStyle.color = [CPTColor whiteColor]; 
     titleStyle.fontName = @"Helvetica-Bold"; 
     titleStyle.fontSize = 16.0f; 
     graph.titleTextStyle = titleStyle; 
     graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop; 
     graph.titleDisplacement = CGPointMake(0.0f, 10.0f); 
     // 4 - Set padding for plot area 
     [graph.plotAreaFrame setPaddingLeft:30.0f]; 
     [graph.plotAreaFrame setPaddingBottom:30.0f]; 
     // 5 - Enable user interactions for plot space 
     CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 
     plotSpace.allowsUserInteraction = YES; 
    } 

    -(void)configurePlotsDancis { 
     // 1 - Get graph and plot space 
     CPTGraph *graph = self.hostView.hostedGraph; 
     CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 
     // 2 - Create the three plots 
     CPTScatterPlot *aaplPlot = [[CPTScatterPlot alloc] init]; 
     aaplPlot.dataSource = self; 
     aaplPlot.identifier = CPDTickerSymbolAAPL; 
     CPTColor *aaplColor = [CPTColor redColor]; 
     [graph addPlot:aaplPlot toPlotSpace:plotSpace]; 
     CPTScatterPlot *googPlot = [[CPTScatterPlot alloc] init]; 
     googPlot.dataSource = self; 
     googPlot.identifier = CPDTickerSymbolGOOG; 
     CPTColor *googColor = [CPTColor greenColor]; 
     [graph addPlot:googPlot toPlotSpace:plotSpace]; 
     CPTScatterPlot *msftPlot = [[CPTScatterPlot alloc] init]; 
     msftPlot.dataSource = self; 
     msftPlot.identifier = CPDTickerSymbolMSFT; 
     CPTColor *msftColor = [CPTColor blueColor]; 
     [graph addPlot:msftPlot toPlotSpace:plotSpace]; 
     // 3 - Set up plot space 
     [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:aaplPlot, googPlot, msftPlot, 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 *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy]; 
     aaplLineStyle.lineWidth = 2.5; 
     aaplLineStyle.lineColor = aaplColor; 
     aaplPlot.dataLineStyle = aaplLineStyle; 
     CPTMutableLineStyle *aaplSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
     aaplSymbolLineStyle.lineColor = aaplColor; 
     CPTPlotSymbol *aaplSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
     aaplSymbol.fill = [CPTFill fillWithColor:aaplColor]; 
     aaplSymbol.lineStyle = aaplSymbolLineStyle; 
     aaplSymbol.size = CGSizeMake(6.0f, 6.0f); 
     aaplPlot.plotSymbol = aaplSymbol; 
     CPTMutableLineStyle *googLineStyle = [googPlot.dataLineStyle mutableCopy]; 
     googLineStyle.lineWidth = 1.0; 
     googLineStyle.lineColor = googColor; 
     googPlot.dataLineStyle = googLineStyle; 
     CPTMutableLineStyle *googSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
     googSymbolLineStyle.lineColor = googColor; 
     CPTPlotSymbol *googSymbol = [CPTPlotSymbol starPlotSymbol]; 
     googSymbol.fill = [CPTFill fillWithColor:googColor]; 
     googSymbol.lineStyle = googSymbolLineStyle; 
     googSymbol.size = CGSizeMake(6.0f, 6.0f); 
     googPlot.plotSymbol = googSymbol; 
     CPTMutableLineStyle *msftLineStyle = [msftPlot.dataLineStyle mutableCopy]; 
     msftLineStyle.lineWidth = 2.0; 
     msftLineStyle.lineColor = msftColor; 
     msftPlot.dataLineStyle = msftLineStyle; 
     CPTMutableLineStyle *msftSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
     msftSymbolLineStyle.lineColor = msftColor; 
     CPTPlotSymbol *msftSymbol = [CPTPlotSymbol diamondPlotSymbol]; 
     msftSymbol.fill = [CPTFill fillWithColor:msftColor]; 
     msftSymbol.lineStyle = msftSymbolLineStyle; 
     msftSymbol.size = CGSizeMake(6.0f, 6.0f); 
     msftPlot.plotSymbol = msftSymbol; 

    // // plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) 
    //            length:CPTDecimalFromInteger(10)]; 
    // plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) 
    //             length:CPTDecimalFromInteger(2000)]; 
    // 
    } 

    -(void)configureAxesDancis { 
     // 1 - Create styles 
     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; 
     // 2 - Get axis set 
     CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet; 
     // 3 - Configure x-axis 
     CPTAxis *x = axisSet.xAxis; 
     x.title = @"Age In Days"; 
     x.titleTextStyle = axisTitleStyle; 
     x.titleOffset = 15.0f; 
     x.axisLineStyle = axisLineStyle; 
     x.labelingPolicy = CPTAxisLabelingPolicyNone; 
     x.labelTextStyle = axisTextStyle; 
     x.majorTickLineStyle = axisLineStyle; 
     x.majorTickLength = 4.0f; 
     x.tickDirection = CPTSignNegative; 







     CGFloat dateCount = [[[CPDStockPriceStore sharedInstance] datesInMonth] count]; 
     NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount]; 
     NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount]; 
     NSInteger i = 0; 
     for (NSString *date in [[CPDStockPriceStore sharedInstance] datesInMonth]) { 
      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 = @"Width"; 
     y.titleTextStyle = axisTitleStyle; 
     y.titleOffset = 10.0f; 
     y.axisLineStyle = axisLineStyle; 
     y.majorGridLineStyle = gridLineStyle; 
     y.labelingPolicy = CPTAxisLabelingPolicyNone; 
     y.labelTextStyle = axisTextStyle; 
     y.labelOffset = -10.0f; 
     y.majorTickLineStyle = axisLineStyle; 
     y.majorTickLength = 4.0f; 
     y.minorTickLength = 2.0f; 
     y.tickDirection = CPTSignNegative; 
     NSInteger majorIncrement = 100; 
     NSInteger minorIncrement = 50; 
     CGFloat yMax = 50000.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:@"%i", 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; 

    } 

y2 axis.using 코어 플롯 라이브러리를 그리는 방법. y1과 y2 axis.y1 시작 500.1000,1500, ..... 및 y2 시작 10,20,30,40, ..... 및 x 축 시작 20,25,30, 35,40, ......

+0

안녕하세요. 난 단지 ios8에서 corepolt를 사용하여 그래프를 만들고 싶다. 헤더를 추가하는 동안 오류가 발생하면 링크 나 자습서를 제안 해주십시오. raywenderlich 튜토리얼을 확인했지만 ios8이 아닙니다. 저를 도와주세요?? –

답변

0

두 번째 y 축에 다른 플롯 공간을 추가해야합니다. xRange을 첫 번째 플롯 공간과 동일하게 만듭니다. 플롯 갤러리 예제 앱에는 여러 가지 데모가 있습니다. "Plot Space Demo"는 설정이 다른 여러 플롯 공간을 만들고 각각에 축을 지정합니다. "축 데모"는 두 번째 y 축을 설정하는 방법을 보여줍니다.

+0

두 번째 플롯 공간 CPTXYPlotSpace *를 추가했습니다. plotSpace2 = [[CPTXYPlotSpace alloc] init]; [그래프 addPlotSpace : plotSpace2]; 이지만 y2 축 레이블 및 기울임 꼴은 표시되지 않습니다. – user2914924

+0

은 solve.am에 라벨 지정이 어려움을 나타냅니다 .i1 시작을 500.1000,1500으로, y2 시작을 10,20,30,40, ..... 및 x 축 시작을 20,25,30,35로 지정합니다. , 40, ... 최소 최대 및 간격을 정의하는 코드 작성 방법 – user2914924

+0

플롯 범위는 시작 위치와 길이 (최대 - 최소)를가집니다. 자신의 틱 위치 및 레이블을 작성 중입니다. 당신은 당신이 원하는 곳 어디에서나 그들을 넣을 수 있습니다. CPTAxisLabelingPolicyFixedInterval 레이블링 정책 (기본값)은 틱과 레이블을 축을 따라 고정 된 간격으로 'majorIntervalLength'로 구분하여 자동으로 배치합니다. –

관련 문제