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 ;
}
運行效果如下:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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