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

如何用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條評論
主站蜘蛛池模板: 亚洲字幕在线观看 | 日本久久高清视频 | 中文字幕久久久久 | 日韩一级欧美一级一级国产 | 最新国产麻豆精品 | 青青热在线观看视频精品 | 精品在线99 | 久久伊人免费视频 | 奇米91| 一级毛片私人影院 | 奇米伊人| 一级免费大片 | 久久精品国产99精品最新 | 中文字幕 一区 婷婷 在线 | 天天爱天天操 | 国产综合久久久久影院 | 天天操天天干天天射 | 女人国产香蕉久久精品 | 日本欧美精品 | 久久综合九色综合欧洲 | 欧美成人一区亚洲一区 | 青青草国产97免久久费观看 | 国产成人综合亚洲欧美在 | 国产欧美一区二区三区免费 | 奇米网第四色 | 韩国爱情片免费大全 | 天天夜夜狠狠 | 毛片推荐| 香蕉毛片 | 亚洲视频国产视频 | 一区二区三区四区视频在线 | 中文字幕日韩一区二区不卡 | 国产精品免费看久久久香蕉 | 亚洲精品一区二区伦理 | 欧美国产亚洲精品高清不卡 | 在线观看久久 | 国产乱人免费视频 | 99久久综合狠狠综合久久 | 国产欧美成人xxx视频 | 国产伦码精品一区二区 | 欧美久久久久久 |