2014-01-14 2 views
0

iOS에서 CorePlot을 사용하여 가로 막 대형 차트를 만들려고했습니다.수평 막대 차트 막대를 사용할 때 특별히 고려해야 할 사항

문서를 찾으려고하지만이 영역에 대해 좋은 것을 찾지 못하는 것 같습니다. 바를 수평으로 만들려면 무엇을 바꾸어야합니까? 그것은 단순한 표시 일입니까, 아니면 데이터를 변경해야합니까?

이들은

#pragma mark - 
#pragma mark - Chart behavior 
-(void)initPlot { 
    [self configureGraph]; 
    [self configurePlots]; 
    [self configureAxes]; 
} 

-(void)configureGraph { 

    CPTGraph *countryGraph = [[CPTXYGraph alloc] initWithFrame:self.countryGraph.bounds]; 
    CPTGraph *timeZoneGraph = [[CPTXYGraph alloc] initWithFrame:self.timeZoneGraph.bounds]; 

    countryGraph.plotAreaFrame.masksToBorder = NO; 
    timeZoneGraph.plotAreaFrame.masksToBorder = NO; 


    self.countryGraph.hostedGraph = countryGraph; 
    self.timeZoneGraph.hostedGraph = timeZoneGraph; 


    CPTXYPlotSpace *countryPlotSpace = (CPTXYPlotSpace *) countryGraph.defaultPlotSpace; 
    countryPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([self.countryData[0] count])]; 
    countryPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([self.countryData count]*50)]; 

    CPTXYPlotSpace *timeZonePlotSpace = (CPTXYPlotSpace *) timeZoneGraph.defaultPlotSpace; 
    timeZonePlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([self.timeZoneData[0] count])]; 
    timeZonePlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([self.timeZoneData count]*50)]; 




} 

-(void)configurePlots { 




    CPTBarPlot *countryPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor colorWithComponentRed:19/255.0 green:221/255.0 blue:187/255.0 alpha:1] horizontalBars:YES]; 

    countryPlot.identifier = @"CountryPlot"; 

    CPTBarPlot *timeZonePlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor colorWithComponentRed:30/255.0 green:33/255.0 blue:36/255.0 alpha:1] horizontalBars:YES]; 
    timeZonePlot.identifier = @"TimeZonePlot"; 


    CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init]; 
    barLineStyle.lineColor = [CPTColor whiteColor]; 
    barLineStyle.lineWidth = 1.0; 

    CPTGraph *countryGraph = self.countryGraph.hostedGraph; 
    CPTGraph *timeZoneGraph = self.timeZoneGraph.hostedGraph; 

    countryPlot.dataSource = self; 
    countryPlot.delegate = self; 
    countryPlot.barWidth = CPTDecimalFromDouble(20); 
    countryPlot.baseValue = CPTDecimalFromString(@"0"); 
    countryPlot.lineStyle = barLineStyle; 
    countryPlot.barOffset = CPTDecimalFromFloat(0.25f); 
    [countryGraph addPlot:countryPlot toPlotSpace:countryGraph.defaultPlotSpace]; 


    timeZonePlot.dataSource = self; 
    timeZonePlot.delegate = self; 
    timeZonePlot.baseValue = CPTDecimalFromString(@"0"); 
    timeZonePlot.barWidth = CPTDecimalFromDouble(20); 
    timeZonePlot.lineStyle = barLineStyle; 
    timeZonePlot.barOffset = CPTDecimalFromFloat(0.25f); 

    [timeZoneGraph addPlot:timeZonePlot toPlotSpace:timeZoneGraph.defaultPlotSpace]; 

} 



-(void)configureAxes { 

    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] colorWithAlphaComponent:1]; 


    CPTXYAxisSet *countryAxisSet = (CPTXYAxisSet *) self.countryGraph.hostedGraph.axisSet; 

    CPTXYAxis *x = countryAxisSet.xAxis; 

    x.labelingPolicy = CPTAxisLabelingPolicyNone; 
    x.axisLineStyle = axisLineStyle; 
    x.majorIntervalLength = CPTDecimalFromString(@"20"); 

    CPTXYAxis *y = countryAxisSet.yAxis; 

    y.labelingPolicy = CPTAxisLabelingPolicyNone; 
    y.axisLineStyle = axisLineStyle; 
    y.majorIntervalLength = CPTDecimalFromString(@"50"); 


    CPTXYAxisSet *timeZoneAxisSet = (CPTXYAxisSet *) self.timeZoneGraph.hostedGraph.axisSet; 

    CPTXYAxis *x2 = timeZoneAxisSet.xAxis; 

    x2.labelingPolicy = CPTAxisLabelingPolicyNone; 
    x2.axisLineStyle = axisLineStyle; 
    x2.majorIntervalLength = CPTDecimalFromString(@"20"); 

    CPTXYAxis *y2 = timeZoneAxisSet.yAxis; 

    y2.labelingPolicy = CPTAxisLabelingPolicyNone; 
    y2.axisLineStyle = axisLineStyle; 
    y2.majorIntervalLength = CPTDecimalFromString(@"50"); 




} 

#pragma mark - CPTPlotDataSource methods 
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { 
    NSUInteger num = 0; 
    if ([plot.identifier isEqual:@"CountryPlot"]) { 
     num = self.countryData.count; 
    } else { 
     num = self.timeZoneData.count; 
    } 
    return num; 


} 

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { 
    NSNumber *num = @0; 

    switch (fieldEnum) { 
     case CPTBarPlotFieldBarTip: 
      if ([plot.identifier isEqual:@"CountryPlot"]) { 
       num = [NSNumber numberWithInteger:[self.countryData[index] count]]; 
      } else { 
       num = [NSNumber numberWithInteger:[self.timeZoneData[index] count]]; 

      } 
      break; 

     default: 
      num = [NSNumber numberWithInteger:index]; 
      break; 
    } 
    return num; 

} 

여기에 ...

여기

내가 지금까지 무엇을 구현

  • xRange, yRange

  • -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 것 그것이하는 것 enter image description here

    그리고 나는 막대기가 작은 분리로 서로 위에 겹쳐져있는 것을 좋아할 것이다 .... 그래서 나는 무엇을 놓쳤는가?

답변

1

수평 막대를 만들기 위해 막대 그림에 barsAreHorizontal ~ YES을 설정하십시오. 막대 위치가 y 축에 있고 막대 끝과 기준 값이 x 축에있는 것을 제외하고는 다른 모든 항목이 동일합니다.

+0

아 좋네요. 질문에 내 코드도 추가했습니다. –

+0

아아, 해결했습니다 ... 'num = [NSNumber numberWithInteger : (인덱스 * 13) +5];'내가 원하는 것을 얻었습니다. –

관련 문제