#import//散點圖的數據點數:20#definenum20@interfaceBarChartViewController:UIViewController{@privateCPXYG" />

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

如何使用Core Plot繪制直方圖和折線圖

系統 1930 0

Core Plot提供了散點圖(CPScatterPlot)繪制,包括折線圖和直方圖,如下圖所示:

如何使用Core Plot繪制直方圖和折線圖

下面的代碼包括了折線圖和直方圖的實現:

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

如何使用Core Plot繪制直方圖和折線圖


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产精品视频在线观看 | 玖玖草在线观看 | 天天操网站 | 亚洲啪啪免费视频 | 天天做天天爱夜夜爽女人爽宅 | 成 人 黄 色 免费网 | 曰本黄色录像 | 在线看福利视频120秒 | 久久99精品这里精品动漫6 | 免费的一级毛片 | 99国产精品免费视频 | 91欧美在线 | 免费看欧美一级特黄a大片 免费看欧美一级特黄a大片一 | 久久精品视频18 | 国产区精品一区二区不卡中文 | 国产免费一区二区三区免费视频 | 久久99久久精品久久久久久 | 免费人成黄页网站在线观看 | 国产伦乱 | 手机看片日韩高清国产欧美 | 精品国产一区二区三区19 | 国产在线精品一区二区 | 午夜国产福利 | 偷偷操不一样的久久 | 色婷婷影视 | 四虎永久网址在线观看 | 中文字幕不卡 | 大片刺激免费播放视频 | 久久久久久91 | 麻豆成人久久精品二区三区小说 | 精品热久久 | 日韩中文在线视频 | 亚洲全黄 | 国产国拍亚洲精品永久不卡 | 91亚洲国产成人久久精品网站 | 国产精品成人一区二区 | 狠狠色噜噜狠狠狠狠色综合久 | 97在线碰碰观看免费高清 | 91这里只有精品 | 91久久亚洲国产成人精品性色 | 欧美人成毛片在线播放 |