Core Plot提供了散點圖(CPScatterPlot)繪制,包括折線圖和直方圖,如下圖所示:
下面的代碼包括了折線圖和直方圖的實現:
1、.h文件:
#import <UIKit/UIKit.h>
#import <CorePlot/CorePlot.h>
// 散點圖的數據點數: 20
#define num 20
@interface BarChartViewController : UIViewController <CPPlotDataSource>
{
@private
CPXYGraph * graph ;
double x [ num ] ; // 散點的 x 坐標
double y1 [ num ] ; // 第 1 個散點圖的 y 坐標
double y2 [ num ]; // 第 2 個散點圖的 y 坐標
}
@end
2、.m文件:
#import "BarChartViewController.h"
@implementation BarChartViewController
-( BOOL )shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation )toInterfaceOrientation
{
return YES ;
}
#pragma mark -
#pragma mark Initialization and teardown
-( void )viewDidAppear:( BOOL )animated
{
// 為 CPGraph 指定主題
graph = [[ CPXYGraph alloc ] initWithFrame : CGRectZero ];
CPTheme *theme = [ CPTheme themeNamed : kCPDarkGradientTheme ];
[ graph applyTheme :theme];
// 把 self.view 由 UIView 轉變為 CPGraphHostingView ,因為 UIView 無法加載 CPGraph
self . view =[[ CPGraphHostingView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
CPGraphHostingView *hostingView = ( CPGraphHostingView *) self . view ;
[hostingView setHostedGraph : graph ];
// CPGraph 邊框:無
graph . plotAreaFrame . borderLineStyle = nil ;
graph . plotAreaFrame . cornerRadius = 0.0f ;
// 繪圖空間 plot space
CPXYPlotSpace *plotSpace = ( CPXYPlotSpace *) graph . defaultPlotSpace ;
// 繪圖空間大小: Y : 0-300 , x : 0-16
plotSpace. yRange = [ CPPlotRange plotRangeWithLocation : CPDecimalFromFloat ( 0.0f ) length : CPDecimalFromFloat ( 200.0f )];
plotSpace. xRange = [ CPPlotRange plotRangeWithLocation : CPDecimalFromFloat ( 0.0f ) length : CPDecimalFromInt ( num )];
// CPGraph 四邊不留白
graph . paddingLeft = 0.0f ;
graph . paddingRight = 0.0f ;
graph . paddingTop = 0.0f ;
graph . paddingBottom = 0.0f ;
// 繪圖區 4 邊留白
graph . plotAreaFrame . paddingLeft = 45.0 ;
graph . plotAreaFrame . paddingTop = 40.0 ;
graph . plotAreaFrame . paddingRight = 5.0 ;
graph . plotAreaFrame . paddingBottom = 80.0 ;
// 坐標系
CPXYAxisSet *axisSet = ( CPXYAxisSet *) graph . axisSet ;
//x 軸:為坐標系的 x 軸
CPXYAxis *X = axisSet. xAxis ;
// 清除默認的軸標簽 , 使用自定義的軸標簽
X. labelingPolicy = CPAxisLabelingPolicyNone ;
// 構造 MutableArray ,用于存放自定義的軸標簽
NSMutableArray *customLabels = [ NSMutableArray arrayWithCapacity : num ];
// 構造一個 TextStyle
static CPTextStyle * labelTextStyle= nil ;
labelTextStyle=[[ CPTextStyle alloc ] init ];
labelTextStyle. color =[ CPColor whiteColor ];
labelTextStyle. fontSize = 10.0f ;
// 每個數據點一個軸標簽
for ( int i= 0 ;i< num ;i++) {
CPAxisLabel *newLabel = [[ CPAxisLabel alloc ] initWithText : [ NSString stringWithFormat : @" 第 %d 個數據點 " ,(i+ 1 )] textStyle :labelTextStyle];
newLabel. tickLocation = CPDecimalFromInt (i);
newLabel. offset = X. labelOffset + X. majorTickLength ;
newLabel. rotation = M_PI / 2 ;
[customLabels addObject :newLabel];
[newLabel release ];
}
X. axisLabels = [ NSSet setWithArray :customLabels];
//y 軸
CPXYAxis *y = axisSet. yAxis ;
//y 軸:不顯示小刻度線
y. minorTickLineStyle = nil ;
// 大刻度線間距: 50 單位
y. majorIntervalLength = CPDecimalFromString ( @"50" );
// 坐標原點: 0
y. orthogonalCoordinateDecimal = CPDecimalFromString ( @"0" );
y. titleOffset = 45.0f ;
y. titleLocation = CPDecimalFromFloat ( 150.0f );
// 第 1 個散點圖:藍色
CPScatterPlot *boundLinePlot = [[[ CPScatterPlot alloc ] init ] autorelease ];
//id ,用于識別該散點圖
boundLinePlot. identifier = @"Blue Plot" ;
// 線型設置
CPLineStyle * lineStyle = [[[ CPLineStyle alloc ] init ] autorelease ];
lineStyle. lineWidth = 1.0f ;
lineStyle. lineColor = [ CPColor blueColor ];
boundLinePlot. dataLineStyle = lineStyle;
// 設置數據源 , 必須實現 CPPlotDataSource 協議
boundLinePlot. dataSource = self ;
[ graph addPlot :boundLinePlot];
// 在圖形上添加一些小圓點符號(節點)
CPLineStyle *symbolLineStyle = [[ CPLineStyle alloc ] init ];
// 描邊:黑色
symbolLineStyle. lineColor = [ CPColor blackColor ];
// 符號類型:橢圓
CPPlotSymbol *plotSymbol = [ CPPlotSymbol ellipsePlotSymbol ];
// 填充色:藍色
plotSymbol. fill = [ CPFill fillWithColor :[ CPColor blueColor ]];
// 描邊
plotSymbol. lineStyle = symbolLineStyle;
// 符號大小: 10*10
plotSymbol. size = CGSizeMake ( 6.0 , 6.0 );
// 向圖形上加入符號
boundLinePlot. plotSymbol = plotSymbol;
// 創建漸變區
// 漸變色 1
CPColor *areaColor = [ CPColor colorWithComponentRed : 0.0 green : 0.0 blue : 1.0 alpha : 1.0 ];
// 創建一個顏色漸變:從 建變色 1 漸變到 無色
CPGradient *areaGradient = [ CPGradient gradientWithBeginningColor :areaColor endingColor :[ CPColor clearColor ]];
// 漸變角度: -90 度(順時針旋轉)
areaGradient. angle = - 90.0f ;
// 創建一個顏色填充:以顏色漸變進行填充
CPFill *areaGradientFill = [ CPFill fillWithGradient :areaGradient];
// 為圖形 1 設置漸變區
boundLinePlot. areaFill = areaGradientFill;
// 漸變區起始值,小于這個值的圖形區域不再填充漸變色
boundLinePlot. areaBaseValue = CPDecimalFromString ( @"0.0" );
//interpolation 值為 CPScatterPlotInterpolation 枚舉類型,該枚舉有 3 個值:
//CPScatterPlotInterpolationLinear, 線性插補 —— 折線圖 .
//CPScatterPlotInterpolationStepped, 在后方進行插補 —— 直方圖
//CPScatterPlotInterpolationHistogram, 以散點為中心進行插補 —— 直方圖
boundLinePlot. interpolation = CPScatterPlotInterpolationHistogram ;
// 第 2 個散點圖:綠色
CPScatterPlot *dataSourceLinePlot = [[[ CPScatterPlot alloc ] init ] autorelease ];
dataSourceLinePlot. identifier = @"Green Plot" ;
// 線型設置
lineStyle = [[[ CPLineStyle alloc ] init ] autorelease ];
lineStyle. lineWidth = 1.0f ;
lineStyle. lineColor = [ CPColor greenColor ];
dataSourceLinePlot. dataLineStyle = lineStyle;
// 設置數據源 , 必須實現 CPPlotDataSource 協議
dataSourceLinePlot. dataSource = self ;
[ graph addPlot :dataSourceLinePlot] ;
// 隨機產生散點數據
NSUInteger i;
for ( i = 0 ; i < num ; i++ ) {
x [i] = i ;
y1 [i] = ( num * 10 )*( rand ()/( float ) RAND_MAX );
y2 [i] = ( num * 10 )*( rand ()/( float ) RAND_MAX );
}
}
#pragma mark -
#pragma mark Plot Data Source Methods
// 返回散點數
-( NSUInteger )numberOfRecordsForPlot:( CPPlot *)plot
{
return num ;
}
// 根據參數返回數據(一個 C 數組)
- ( double *)doublesForPlot:( CPPlot *)plot field:( NSUInteger )fieldEnum recordIndexRange:( NSRange )indexRange
{
// 返回類型:一個 double 指針(數組)
double *values;
NSString * identifier=( NSString *)[plot identifier];
switch (fieldEnum) {
// 如果請求的數據是散點 x 坐標 , 直接返回 x 坐標(兩個圖形是一樣的),否則還要進一步判斷是那個圖形
case CPScatterPlotFieldX :
values= x ;
break ;
case CPScatterPlotFieldY :
// 如果請求的數據是散點 y 坐標,則對于圖形 1 ,使用 y1 數組,對于圖形 2 ,使用 y2 數組
if ([identifier isEqualToString : @"Blue Plot" ]) {
values= y1 ;
} else
values= y2 ;
break ;
}
// 數組指針右移個 indexRage.location 單位,則數組截去 indexRage.location 個元素
return values + indexRange. location ;
}
// 添加數據標簽
-( CPLayer *)dataLabelForPlot:( CPPlot *)plot recordIndex:( NSUInteger )index
{
// 定義一個白色的 TextStyle
static CPTextStyle *whiteText = nil ;
if ( !whiteText ) {
whiteText = [[ CPTextStyle alloc ] init ];
whiteText. color = [ CPColor whiteColor ];
}
// 定義一個 TextLayer
CPTextLayer *newLayer = nil ;
NSString * identifier=( NSString *)[plot identifier];
if ([identifier isEqualToString : @"Blue Plot" ]) {
newLayer = [[[ CPTextLayer alloc ] initWithText :[ NSString stringWithFormat : @"%.0f" , y1 [index]] style :whiteText] autorelease ];
}
return newLayer;
}
@end
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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