亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

如何用Core Plot繪制柱狀圖

系統 1861 0

Core Plot提供了柱狀圖的繪制,不足的是,只有垂直柱狀圖,沒有提供水平柱狀圖。期待下一版本的實現。

1、新建Windows-base Application。加入對Core Plot框架的引用。這里我們假設使用了Core Plot SDK ,項目設置參考前一博文《Core Plot SDK的用法》。

2、新建ViewController,首先修改ViewController的頭文件,import CorePlot.h,同時實現CPPlotDataSource協議,增加一個CPGraph變量:

#import <UIKit/UIKit.h>

#import <CorePlot/CorePlot.h>

@interface BarChartViewController : UIViewController <CPPlotDataSource>

{

@private

CPXYGraph * barChart ;

}

@property ( readwrite , retain , nonatomic ) NSTimer *timer;

@end

3、具體實現如下:

-( void )viewDidAppear:( BOOL )animated

{

// CPGraph 指定主題

barChart = [[ CPXYGraph alloc ] initWithFrame : CGRectZero ];

CPTheme *theme = [ CPTheme themeNamed : kCPDarkGradientTheme ];

[ barChart applyTheme :theme];

// self.view UIView 轉變為 CPGraphHostingView ,因為 UIView 無法加載 CPGraph

self . view =[[ CPGraphHostingView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];

CPGraphHostingView *hostingView = ( CPGraphHostingView *) self . view ;

[hostingView setHostedGraph : barChart ];

// CPGraph 邊框:無

barChart . plotAreaFrame . borderLineStyle = nil ;

barChart . plotAreaFrame . cornerRadius = 0.0f ;

// CPGraph 四邊不留白

barChart . paddingLeft = 0.0f ;

barChart . paddingRight = 0.0f ;

barChart . paddingTop = 0.0f ;

barChart . paddingBottom = 0.0f ;

// 繪圖區 4 邊留白

barChart . plotAreaFrame . paddingLeft = 70.0 ;

barChart . plotAreaFrame . paddingTop = 20.0 ;

barChart . plotAreaFrame . paddingRight = 20.0 ;

barChart . plotAreaFrame . paddingBottom = 80.0 ;

//CPGraph 標題

barChart . title = @"Graph Title" ;

// SDK CPMutableTextStyle 不可用,用 CPTextStyle 替代

CPTextStyle * textStyle=[ CPTextStyle textStyle ];

// CPMutableTextStyle *textStyle = [CPTextStyle textStyle];

textStyle. color = [ CPColor grayColor ];

textStyle. fontSize = 16.0f ;

barChart . titleTextStyle = textStyle;

barChart . titleDisplacement = CGPointMake ( 0.0f , - 20.0f );

barChart . titlePlotAreaFrameAnchor = CPRectAnchorTop ;

// 繪圖空間 plot space

CPXYPlotSpace *plotSpace = ( CPXYPlotSpace *) barChart . defaultPlotSpace ;

// 繪圖空間大小: Y 0-300 x 0-16

plotSpace. yRange = [ CPPlotRange plotRangeWithLocation : CPDecimalFromFloat ( 0.0f ) length : CPDecimalFromFloat ( 300.0f )];

plotSpace. xRange = [ CPPlotRange plotRangeWithLocation : CPDecimalFromFloat ( 0.0f ) length : CPDecimalFromFloat ( 16.0f )];

// 坐標系

CPXYAxisSet *axisSet = ( CPXYAxisSet *) barChart . axisSet ;

//x 軸:為坐標系的 x

CPXYAxis *x = axisSet. xAxis ;

CPLineStyle * lineStyle=[[ CPLineStyle alloc ] init ];

lineStyle. lineColor =[ CPColor greenColor ];

lineStyle. lineWidth = 1.0f ;

//x 軸:線型設置

x. axisLineStyle = lineStyle;

// 大刻度線:線型設置

x. majorTickLineStyle = lineStyle;

// 大刻度線:長度

x. majorTickLength = 10 ;

// 小刻度線:無

x. minorTickLineStyle =lineStyle;

// 小刻度線:長度

x. minorTickLength = 5 ;

// 大刻度線間隔單位: 5 個單位

x. majorIntervalLength = CPDecimalFromString ( @"5" );

// 直角坐標: 0

x. orthogonalCoordinateDecimal = CPDecimalFromString ( @"0" );

// 標題

x. title = @"X Axis" ;

// 標題位置: 7.5 單位

x. titleLocation = CPDecimalFromFloat ( 7.5f );

// 向下偏移: 55.0

x. titleOffset = 55.0f ;

//y

CPXYAxis *y = axisSet. yAxis ;

//y 軸:線型設置

y. axisLineStyle = lineStyle;

//y 軸:線型設置

y. majorTickLineStyle = lineStyle;

//y 軸:不顯示小刻度線

y. minorTickLineStyle = nil ;

// 大刻度線間距: 50 單位

y. majorIntervalLength = CPDecimalFromString ( @"50" );

// 坐標原點: 0

y. orthogonalCoordinateDecimal = CPDecimalFromString ( @"0" );

// 軸標題

y. title = @"Y Axis" ;

y. titleOffset = 45.0f ;

y. titleLocation = CPDecimalFromFloat ( 150.0f );

// 1 個柱狀圖:黑色

CPBarPlot *barPlot = [ CPBarPlot tubularBarPlotWithColor :[ CPColor darkGrayColor ] horizontalBars : NO ];

barPlot. baseValue = CPDecimalFromString ( @"1" );

// 數據源,必須實現 CPPlotDataSource 協議

barPlot. dataSource = self ;

// 圖形向左偏移: 0.25

barPlot. barOffset = - 0.25f ;

//id ,根據此 id 來區分不同的 plot ,或者為不同 plot 提供不同數據源

barPlot. identifier = @"Bar Plot 1" ;

// 添加圖形到繪圖空間

[ barChart addPlot :barPlot toPlotSpace :plotSpace];

// 2 個柱狀圖:藍色

barPlot = [ CPBarPlot tubularBarPlotWithColor :[ CPColor blueColor ] horizontalBars : NO ];

// 數據源,必須實現 CPPlotDataSource 協議

barPlot. dataSource = self ;

// 柱子的起始基線:即最下沿的 y 坐標

barPlot. baseValue = CPDecimalFromString ( @"1" );

// 圖形向右偏移: 0.25

barPlot. barOffset = 0.25f ;

// SDK 中, barCornerRadius cornerRadius 替代

barPlot. cornerRadius = 2.0f ;

//barPlot.barCornerRadius = 2.0f;

//id ,根據此 id 來區分不同的 plot ,或者為不同 plot 提供不同數據源

barPlot. identifier = @"Bar Plot 2" ;

// 添加圖形到繪圖空間

[ barChart addPlot :barPlot toPlotSpace :plotSpace];

}

- ( void )didReceiveMemoryWarning {

[ super didReceiveMemoryWarning ]; // Releases the view if it doesn't have a superview

// Release anything that's not essential, such as cached data

}

#pragma mark -

#pragma mark Plot Data Source Methods

// 返回數據源的紀錄數

-( NSUInteger )numberOfRecordsForPlot:( CPPlot *)plot {

return 16 ;

}

// 返回數據源的數據

-( NSNumber *)numberForPlot:( CPPlot *)plot field:( NSUInteger )fieldEnum recordIndex:( NSUInteger )index

{

// 返回類型是一個 NSNumber

NSDecimalNumber *num = nil ;

// 如果圖形類型是 柱狀圖

if ( [plot isKindOfClass :[ CPBarPlot class ]] ) {

// 根據情況,柱狀圖的每一點都需要返回兩種數據:位置( x 軸),長度( y 軸)

switch ( fieldEnum ) {

//x 軸坐標(柱子位置):

case CPBarPlotFieldBarLocation :

num = ( NSDecimalNumber *)[ NSDecimalNumber numberWithUnsignedInteger :index];

break ;

//y 軸坐標(柱子長度):

//SDK 中,枚舉 CPBarPlotField 只有兩個值 CPBarPlotFieldBarLocation = 2, 或者 CPBarPlotFieldBarLength = 3

case CPBarPlotFieldBarLength :

//case CPBarPlotFieldBarTip:

num = ( NSDecimalNumber *)[ NSDecimalNumber numberWithUnsignedInteger :(index+ 1 )*(index+ 1 )];

// 對于第 2 個圖形的點的 y 值,在第一個圖形的基礎上減去 10

if ( [plot. identifier isEqual : @"Bar Plot 2" ] )

num = [num decimalNumberBySubtracting :[ NSDecimalNumber decimalNumberWithString : @"10" ]];

break ;

}

}

return num;

}

-( CPFill *) barFillForBarPlot:( CPBarPlot *)barPlot recordIndex:( NSNumber *)index;

{

return nil ;

}

運行效果如下:

如何用Core Plot繪制柱狀圖

如何用Core Plot繪制柱狀圖


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!?。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久久国产一区二区三区 | 黄色片免费在线观看视频 | 日本久久久久亚洲中字幕 | 久久综合视频网 | 青青久久国产成人免费网站 | 国产综合社区 | 91视频免费播放 | 干欧美女人 | 亚洲日韩欧美一区二区在线 | 亚洲国产日韩欧美一区二区三区 | 深夜在线观看网站 | 中文字幕亚洲综合久久菠萝蜜 | 欧美日本在线 | 91精品免费国产高清在线 | 99热这里只有精品18 | 免费看国产一级特黄aa大片 | 羞羞色院91蜜桃在线观看 | 久久精品亚洲牛牛影视 | ww亚洲ww在线观看国产 | 亚洲人成一区二区不卡 | 韩国一级理黄论片 | japanese护士奶水 | 亚洲一片| 国产亚洲精品久久麻豆 | 深夜视频在线免费 | 久久综合免费 | 九九热在线视频观看这里只有精品 | 色涩亚洲 | 欧美一级第一免费高清 | 久久奇米| 五月激情久久 | 色婷婷视频在线 | 337p欧洲日本大胆艺术 | 色综合天天射 | 99久久99久久精品国产 | 亚洲精品久久久久影院 | 亚洲a视频在线观看 | 一区 在线播放 | 久久天天躁狠狠躁夜夜不卡 | 国产一区精品视频 | 在线观看免费亚洲 |