Core Plot學習筆記:繪製餅圖

轉自 http://blog.csdn.net/u012890196/article/details/20909877


我們用該類庫繪製一個餅圖出來:

         建一個單視圖工程

         .h文件內容如下:

        

        

#import <UIKit/UIKit.h>

#import "CorePlot-CocoaTouch.h"

@interface QQViewController :UIViewController<CPTPlotDataSource,CPTPieChartDelegate>

@property(retain,nonatomic)NSMutableArray *arr;

@property(retain,nonatomic)CPTXYGraph *graph;

@property(retain,nonatomic)CPTPieChart *piePlot;


@end

   
        在.m文件如下:
    
     

- (void)viewDidLoad

{

    [superviewDidLoad];

    //初始化數組

    self.arr = [[NSMutableArrayalloc]initWithObjects:@"0.2",@"0.3",@"0.1",@"0.2",@"0.2",nil];

    

    //創建畫布

   self.graph = [[CPTXYGraphalloc]initWithFrame:self.view.bounds];

    //設置畫布主題

    CPTTheme *theme = [CPTThemethemeNamed:kCPTPlainWhiteTheme];

    [self.graphapplyTheme:theme];

    //畫布與周圍的距離

    self.graph.paddingBottom =10;

    self.graph.paddingLeft =5;

    self.graph.paddingRight =5;

    self.graph.paddingTop =10;

    //將畫布的座標軸設爲空

    self.graph.axisSet =nil;

    

    //創建畫板

    CPTGraphHostingView *hostView = [[CPTGraphHostingViewalloc]initWithFrame:self.view.bounds];

    //設置畫板的畫布

    hostView.hostedGraph =self.graph;

    //設置畫布標題的風格

    CPTMutableTextStyle *whiteText = [CPTMutableTextStyletextStyle];

    whiteText.color = [CPTColorblackColor];

    whiteText.fontSize =18;

    whiteText.fontName =@"Helvetica-Bold";

   self.graph .titleTextStyle = whiteText;

   self.graph.title =@"餅狀圖";


    //創建餅圖對象

   self.piePlot = [[CPTPieChartalloc]initWithFrame:CGRectMake(10,10,200,200)];

    //設置數據源

    self.piePlot.dataSource =self;

    //設置餅圖半徑

    self.piePlot.pieRadius =100.0;

    //設置餅圖表示符

    self.piePlot.identifier =@"pie chart";

    //餅圖開始繪製的位置

    self.piePlot.startAngle =M_PI_4;

    //餅圖繪製的方向(順時針/逆時針)

    self.piePlot.sliceDirection = CPTPieDirectionCounterClockwise;

    //餅圖的重心

    self.piePlot.centerAnchor =CGPointMake(0.5,0.38);

    //餅圖的線條風格

    self.piePlot.borderLineStyle = [CPTLineStylelineStyle];

    //設置代理

   self.piePlot.delegate =self;

    //將餅圖加到畫布上

    [self.graphaddPlot:self.piePlot];

    

    //將畫板加到視圖上

    [self.viewaddSubview:hostView];

    

    //創建圖例

   CPTLegend *theLegeng = [CPTLegendlegendWithGraph:self.graph];

    theLegeng.numberOfColumns =1;

    theLegeng.fill = [CPTFillfillWithColor:[CPTColorwhiteColor]];

    theLegeng.borderLineStyle = [CPTLineStylelineStyle];

    theLegeng.cornerRadius =5.0;

    theLegeng.delegate =self;

    

   self.graph.legend = theLegeng;

    self.graph.legendAnchor = CPTRectAnchorRight;

    self.graph.legendDisplacement =CGPointMake(-10,100);


    

}


#pragma ===========================CPTPlotDelegate========================

//返回扇形數目

- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot

{

    return self.arr.count;

}

//返回每個扇形的比例

- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx

{

   return [self.arrobjectAtIndex:idx];

}

//凡返回每個扇形的標題

- (CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)idx

{

   CPTTextLayer *label = [[CPTTextLayeralloc]initWithText:[NSStringstringWithFormat:@"hello,%@",[self.arrobjectAtIndex:idx]]];

    CPTMutableTextStyle *text = [ label.textStylemutableCopy];

    text.color = [CPTColorwhiteColor];

   return label;

}


#pragma ===========CPTPieChart   Delegate========================

//選中某個扇形時的操作

- (void)pieChart:(CPTPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)idx

{

    self.graph.title = [NSStringstringWithFormat:@"比例:%@",[self.arrobjectAtIndex:idx]];

}


//返回圖例

- (NSAttributedString *)attributedLegendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)idx

{

    NSAttributedString *title = [[NSAttributedString alloc]initWithString:[NSStringstringWithFormat:@"hi:%i",idx]];

    

    return title;

}






運行結果:


    

點擊某個扇形,“餅狀圖”標題內容回改變。



其他一些方法:

  (1) -(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)idx;繪製某一扇形是的顏色。

    

   

     (2)對於餅圖,我們可以把某塊扇形“切除”下來,以此突出該扇形區域。這需要實現數據源方法radialOffsetForPieChart:recordIndex: 方法。以下代碼將餅圖中第2塊扇形“剝離”10個像素點:

-(CGFloat)radialOffsetForPieChart:(CPTPieChart*)piePlot recordIndex:(NSUInteger)index{

return (indexix==1?10:0);

}

(3)圖例由 CPTLengend 對象表示。上述代碼中,我們已經創建了圖例。


    CPTLengend 的一些屬性:

numberOfColumns屬性 - 圖例的列數。有時圖例太多,單列顯示太長,可分爲多列顯示。

fill屬性 - 圖例的填充屬性,CPTFill 類型。

borderLineStyle屬性 - 圖例外框的線條樣式。

cornerRadiuss屬性 - 圖例外框的圓角半徑。

     把一個圖例對象賦值給圖形的 legend 屬性,即可在繪製圖形時加上圖例。此外還圖形對象還有兩個和圖例相關的重要屬性:

legendAnchor屬性 - 圖例對齊於圖框的位置,可以用 CPTRectAnchor 枚舉類型,指定圖例向圖框的4角、4邊(中點)對齊。

legendDisplacement屬性 - 圖例對齊時的偏移距離。注意,對齊時是使用圖例的相同錨點和圖框的相同錨點對齊。比如,圖形的legendAnchor屬性是右上角(CPTRectAnchorTopRight),則對齊時用圖例的右上角和圖框的右上角進行對齊。這個偏移座標(x,y)就是這兩個錨點之間的距離(用圖例的錨點座標減圖框的錨點座標)。默認legendDisplacement爲(0,0)。注意這個值的正負關係,比如(-30,-30)和(30,30)是截然相反的。

       圖例上的描述文字,從數據本身是看不到的,默認情況下 CorePlot 會以“Pie Chart 1”、“Pie Char 2”來命名。

我們需要實現數據源方法legendTitleForPieChart:recordIndex:來覆蓋默認的圖例名稱:

-(NSString*)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index;

- (NSAttributedString *)attributedLegendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)idx方法例也可以返回圖例的內容,在本例中。我們就用的該方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章