2011-08-14 22 views
3

我有文件的所有者設置爲這種觀點筆尖文件,它只有一個觀點,即有一個自定義類CPTGraphHostingView集。它通過一個插座連接到這個視圖。這個視圖加載,但它顯示的是一個非常小的條形圖。我真的不確定爲什麼。而且,任何添加到IB視圖中的東西都會顛倒過來。如何使用Core Plot設置圖表寬度?

在這一點是圖表獲得的寬度設置爲它?我假設使用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];的初始設置,並能正常工作。我認爲在實現中有一些代碼將大小設置爲包含視圖的大小。

joe

+0

-1,因爲積空間確定是可見的在該圖中,在視圖中而不是它的物理尺寸的數據範圍。 –

+0

http://i.imgur.com/SLj7H.png我影響了這些行和其他一些,並改爲CGRectZero,現在我終於得到了傳說,但酒吧仍然很小。 – Tatsh

1

託管視圖將使圖形自動填充界限。只要託管視圖的容器大小正確,初始化它就無關緊要。有幾件事情發生在這裏:

  1. 您在白色背景上使用白色網格線。他們很難看到這種方式。 :-)更改線條顏色或在圖形上設置不同顏色的填充。

  2. 複製/粘貼錯誤在y軸:

    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使軸標籤和標題可見。您還可以調整上的填充以設置繪圖區域框外的整體邊距。

埃裏克