2011-08-14 7 views
3

파일 소유자가이 뷰로 설정된 nib 파일이 있고 사용자 정의 클래스 CPTGraphHostingView가 설정된보기가 하나뿐입니다. 그것은 콘센트를 통해이 뷰에 연결됩니다. 이 뷰는로드되지만 표시되는 것은 매우 작은 막대 그래프입니다. 그리고 나는 정말로 확신 할 수 없다. 또한 IB의보기에 추가 된 내용이 모두 거꾸로 표시됩니다.코어 플롯에서 차트 너비를 설정하는 방법은 무엇입니까?

차트가 너비로 설정되는 시점은 언제입니까? self.view.bounds를 사용하면 차트가 사용 가능한 모든 화면 공간을 사용하게된다고 가정합니다.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:@"PollResultView" bundle:nil]; 
    self.title = @"Results"; 
    return self; 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds]; 

    CPTXYPlotSpace *barPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease]; 
    barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) 
                length:CPTDecimalFromFloat(100)]; 
    barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) 
                length:CPTDecimalFromFloat(30)]; 

    [graph addPlotSpace:barPlotSpace]; 

    CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle]; 
    majorGridLineStyle.lineWidth = 1.0f; 
    majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.75f]; 

    CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle]; 
    minorGridLineStyle.lineWidth = 1.0f; 
    minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.25f]; 

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 

    CPTXYAxis *x = axisSet.xAxis; 
    x.majorIntervalLength = CPTDecimalFromInteger(10); 
    x.minorTicksPerInterval = 9; 
    x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0); 
    x.majorGridLineStyle = majorGridLineStyle; 
    x.minorGridLineStyle = minorGridLineStyle; 
    x.axisLineStyle = nil; 
    x.majorTickLineStyle = nil; 
    x.minorTickLineStyle = nil; 
    x.labelOffset = 10.0f; 
    x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f) 
               length:CPTDecimalFromFloat(10.0f)]; 
    x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) 
                length:CPTDecimalFromFloat(100.0f)]; 
    x.title = @"X Axis"; 
    x.titleOffset = 30.0f; 
    x.titleLocation = CPTDecimalFromFloat(5.0f); 
    x.plotSpace = barPlotSpace; 

    CPTXYAxis *y = axisSet.yAxis; 
    y.majorIntervalLength = CPTDecimalFromInteger(10); 
    y.minorTicksPerInterval = 9; 
    y.orthogonalCoordinateDecimal = CPTDecimalFromInteger(-0.5); 
    y.preferredNumberOfMajorTicks = 8; 
    y.majorGridLineStyle = majorGridLineStyle; 
    y.minorGridLineStyle = minorGridLineStyle; 
    x.axisLineStyle = nil; 
    x.majorTickLineStyle = nil; 
    x.minorTickLineStyle = nil; 
    x.labelOffset = 10.0f; 
    y.labelRotation = M_PI/2; 
    y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) 
               length:CPTDecimalFromFloat(100.0f)]; 
    y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.5f) 
                length:CPTDecimalFromFloat(10.0f)]; 
    y.title = @"Y Axis"; 
    y.titleOffset = 30.0f; 
    y.titleLocation = CPTDecimalFromInteger(55); 
    y.plotSpace = barPlotSpace; 

    graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil]; 

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

    CPTBarPlot *barPlot = [[[CPTBarPlot alloc] init] autorelease]; 
    barPlot.lineStyle = barLineStyle; 
    barPlot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.0f 
                    green:0.0f 
                    blue:0.5f 
                    alpha:0.5f]]; 
    barPlot.barBasesVary = YES; 
    barPlot.barWidth = CPTDecimalFromFloat(0.5f); // bar is 50% of the available space 
    barPlot.barCornerRadius = 10.0f; 
    barPlot.barsAreHorizontal = NO; 

    CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle]; 
    whiteTextStyle.color = [CPTColor whiteColor]; 
    barPlot.barLabelTextStyle = whiteTextStyle; 
    // barPlot.delegate = self; 
    barPlot.dataSource = self; 
    barPlot.identifier = @"Bar Plot 1"; 

    [graph addPlot:barPlot toPlotSpace:barPlotSpace]; 

    CPTBarPlot *barPlot2 = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] 
               horizontalBars:NO]; 
    barPlot2.lineStyle = barLineStyle; 
    barPlot2.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.0f 
                    green:1.0f 
                    blue:0.5f 
                    alpha:0.5f]]; 
    barPlot2.barBasesVary = YES; 
    barPlot2.barWidth = CPTDecimalFromFloat(1.0f); 
    barPlot2.barCornerRadius = 2.0f; 
    barPlot2.barsAreHorizontal = NO; 
    //barPlot2.delegate = self; 
    barPlot2.dataSource = self; 
    barPlot2.identifier = @"Bar Plot 2"; 

    [graph addPlot:barPlot2 toPlotSpace:barPlotSpace]; 

    CPTLegend *theLegend = [CPTLegend legendWithGraph:graph]; 
    theLegend.numberOfRows = 2; 
    theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]]; 
    theLegend.borderLineStyle = barLineStyle; 
    theLegend.cornerRadius = 10.0; 
    theLegend.swatchSize = CGSizeMake(20.0, 20.0); 
    whiteTextStyle.fontSize = 16.0; 
    theLegend.textStyle = whiteTextStyle; 
    theLegend.rowMargin = 10.0; 
    theLegend.paddingLeft = 12.0; 
    theLegend.paddingTop = 12.0; 
    theLegend.paddingRight = 12.0; 
    theLegend.paddingBottom = 12.0; 

    NSArray *plotPoint = [NSArray arrayWithObjects:[NSNumber numberWithInteger:0], [NSNumber numberWithInteger:95], nil]; 
    CPTPlotSpaceAnnotation *legendAnnotation = [[[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:barPlotSpace anchorPlotPoint:plotPoint] autorelease]; 
    legendAnnotation.contentLayer = theLegend; 
    legendAnnotation.contentAnchorPoint = CGPointMake(0.0f, 1.0f); 
    [graph.plotAreaFrame.plotArea addAnnotation:legendAnnotation]; 

    graphHostingView.hostedGraph = graph; 
} 

답변

-1

당신은 이들 명령에 아주 작은 그래프로 설정하고 있습니다 :

barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) 
               length:CPTDecimalFromFloat(100)]; 
barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) 
               length:CPTDecimalFromFloat(30)]; 

100 픽셀 폭 30 픽셀 높이가 실제 그림 영역에 대해 매우 작습니다.

내 코드는 초기 설정에 graph = [CPTXYGraph alloc] initWithFrame:CGRectZero];을 사용하며 정상적으로 작동합니다. 구현시 구현시 크기가 포함 된 뷰를 설정하는 코드가 있다고 생각합니다.

+0

-1 플롯 공간이 그래프에서보기없는 물리적 크기 표시되는 데이터의 범위를 결정하기 때문이다. –

+0

http://i.imgur.com/SLj7H.png 나는 그 라인들과 몇몇 다른 것들에 영향을 주었고 CGRectZero로 바뀌었고 이제 나는 전설을 드러냈다. 그러나 막대는 여전히 작다. – Tatsh

1

호스팅보기를 사용하면 그래프가 자동으로 경계를 채 웁니다. 호스팅보기의 컨테이너 크기가 올바르면 초기화 할 때 사용하는 항목은 중요하지 않습니다. 여기에 몇 가지가 있습니다 :

  1. 흰 바탕에 흰색 눈금 선을 사용하고 있습니다. 그렇게보기가 어렵습니다. :-) 선 색상을 변경하거나 다른 색상의 그래프 채우기를 설정하십시오. y 축에

  2. 복사/붙여 넣기 오류 :

    x.axisLineStyle = nil;x.majorTickLineStyle = nil;x.minorTickLineStyle = nil;

    는의 가시 범위를 설정할 필요가 없습니다

    y.axisLineStyle = nil;y.majorTickLineStyle = nil;y.minorTickLineStyle = nil;

  3. 해야 당신의 축 - 당신은 어쨌든 그들을 그리지 않습니다.

  4. gridLinesRange를 설정할 필요가 없습니다. 기본값은 사용중인 플롯 범위입니다.

  5. barBasesVary를 사용하면 데이터 소스가 CPTBarPlotFieldBarBase 필드에 대한 데이터도 제공하는지 확인하십시오.

  6. 플롯 공간 주석을 사용하는 경우 범례의 고정 점은 플롯 공간 범위 (현재 x : [0, 100] 및 y : [0, 30]) 내에 있어야하며 그렇지 않은 경우 명백한.

  7. graph.plotAreaFrame에 패딩을 설정해야 축 레이블과 제목을 볼 수 있습니다. 에서 패딩을 조정하여 플롯 영역 프레임 외부의 전체 여백을 설정할 수도 있습니다.

에릭

관련 문제