#import//散點(diǎn)圖的數(shù)據(jù)點(diǎn)數(shù):20#definenum20@interfaceBarChartViewController:UIViewController{@privateCPXYG" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

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

系統(tǒng) 2005 0

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

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

下面的代碼包括了折線圖和直方圖的實(shí)現(xiàn):

1、.h文件:

#import <UIKit/UIKit.h>

#import <CorePlot/CorePlot.h>

// 散點(diǎn)圖的數(shù)據(jù)點(diǎn)數(shù): 20

#define num 20

@interface BarChartViewController : UIViewController <CPPlotDataSource>

{

@private

CPXYGraph * graph ;

double x [ num ] ; // 散點(diǎn)的 x 坐標(biāo)

double y1 [ num ] ; // 1 個(gè)散點(diǎn)圖的 y 坐標(biāo)

double y2 [ num ]; // 2 個(gè)散點(diǎn)圖的 y 坐標(biāo)

}

@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 轉(zhuǎn)變?yōu)? CPGraphHostingView ,因?yàn)? UIView 無(wú)法加載 CPGraph

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

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

[hostingView setHostedGraph : graph ];

// CPGraph 邊框:無(wú)

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 ;

// 繪圖區(qū) 4 邊留白

graph . plotAreaFrame . paddingLeft = 45.0 ;

graph . plotAreaFrame . paddingTop = 40.0 ;

graph . plotAreaFrame . paddingRight = 5.0 ;

graph . plotAreaFrame . paddingBottom = 80.0 ;

// 坐標(biāo)系

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

//x 軸:為坐標(biāo)系的 x

CPXYAxis *X = axisSet. xAxis ;

// 清除默認(rèn)的軸標(biāo)簽 , 使用自定義的軸標(biāo)簽

X. labelingPolicy = CPAxisLabelingPolicyNone ;

// 構(gòu)造 MutableArray ,用于存放自定義的軸標(biāo)簽

NSMutableArray *customLabels = [ NSMutableArray arrayWithCapacity : num ];

// 構(gòu)造一個(gè) TextStyle

static CPTextStyle * labelTextStyle= nil ;

labelTextStyle=[[ CPTextStyle alloc ] init ];

labelTextStyle. color =[ CPColor whiteColor ];

labelTextStyle. fontSize = 10.0f ;

// 每個(gè)數(shù)據(jù)點(diǎn)一個(gè)軸標(biāo)簽

for ( int i= 0 ;i< num ;i++) {

CPAxisLabel *newLabel = [[ CPAxisLabel alloc ] initWithText : [ NSString stringWithFormat : @" %d 個(gè)數(shù)據(jù)點(diǎn) " ,(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" );

// 坐標(biāo)原點(diǎn): 0

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

y. titleOffset = 45.0f ;

y. titleLocation = CPDecimalFromFloat ( 150.0f );

// 1 個(gè)散點(diǎn)圖:藍(lán)色

CPScatterPlot *boundLinePlot = [[[ CPScatterPlot alloc ] init ] autorelease ];

//id ,用于識(shí)別該散點(diǎn)圖

boundLinePlot. identifier = @"Blue Plot" ;

// 線型設(shè)置

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

lineStyle. lineWidth = 1.0f ;

lineStyle. lineColor = [ CPColor blueColor ];

boundLinePlot. dataLineStyle = lineStyle;

// 設(shè)置數(shù)據(jù)源 , 必須實(shí)現(xiàn) CPPlotDataSource 協(xié)議

boundLinePlot. dataSource = self ;

[ graph addPlot :boundLinePlot];

// 在圖形上添加一些小圓點(diǎn)符號(hào)(節(jié)點(diǎn))

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

// 描邊:黑色

symbolLineStyle. lineColor = [ CPColor blackColor ];

// 符號(hào)類型:橢圓

CPPlotSymbol *plotSymbol = [ CPPlotSymbol ellipsePlotSymbol ];

// 填充色:藍(lán)色

plotSymbol. fill = [ CPFill fillWithColor :[ CPColor blueColor ]];

// 描邊

plotSymbol. lineStyle = symbolLineStyle;

// 符號(hào)大小: 10*10

plotSymbol. size = CGSizeMake ( 6.0 , 6.0 );

// 向圖形上加入符號(hào)

boundLinePlot. plotSymbol = plotSymbol;

// 創(chuàng)建漸變區(qū)

// 漸變色 1

CPColor *areaColor = [ CPColor colorWithComponentRed : 0.0 green : 0.0 blue : 1.0 alpha : 1.0 ];

// 創(chuàng)建一個(gè)顏色漸變:從 建變色 1 漸變到 無(wú)色

CPGradient *areaGradient = [ CPGradient gradientWithBeginningColor :areaColor endingColor :[ CPColor clearColor ]];

// 漸變角度: -90 度(順時(shí)針旋轉(zhuǎn))

areaGradient. angle = - 90.0f ;

// 創(chuàng)建一個(gè)顏色填充:以顏色漸變進(jìn)行填充

CPFill *areaGradientFill = [ CPFill fillWithGradient :areaGradient];

// 為圖形 1 設(shè)置漸變區(qū)

boundLinePlot. areaFill = areaGradientFill;

// 漸變區(qū)起始值,小于這個(gè)值的圖形區(qū)域不再填充漸變色

boundLinePlot. areaBaseValue = CPDecimalFromString ( @"0.0" );

//interpolation 值為 CPScatterPlotInterpolation 枚舉類型,該枚舉有 3 個(gè)值:

//CPScatterPlotInterpolationLinear, 線性插補(bǔ) —— 折線圖 .

//CPScatterPlotInterpolationStepped, 在后方進(jìn)行插補(bǔ) —— 直方圖

//CPScatterPlotInterpolationHistogram, 以散點(diǎn)為中心進(jìn)行插補(bǔ) —— 直方圖

boundLinePlot. interpolation = CPScatterPlotInterpolationHistogram ;

// 2 個(gè)散點(diǎn)圖:綠色

CPScatterPlot *dataSourceLinePlot = [[[ CPScatterPlot alloc ] init ] autorelease ];

dataSourceLinePlot. identifier = @"Green Plot" ;

// 線型設(shè)置

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

lineStyle. lineWidth = 1.0f ;

lineStyle. lineColor = [ CPColor greenColor ];

dataSourceLinePlot. dataLineStyle = lineStyle;

// 設(shè)置數(shù)據(jù)源 , 必須實(shí)現(xiàn) CPPlotDataSource 協(xié)議

dataSourceLinePlot. dataSource = self ;

[ graph addPlot :dataSourceLinePlot] ;

// 隨機(jī)產(chǎn)生散點(diǎn)數(shù)據(jù)

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

// 返回散點(diǎn)數(shù)

-( NSUInteger )numberOfRecordsForPlot:( CPPlot *)plot

{

return num ;

}

// 根據(jù)參數(shù)返回?cái)?shù)據(jù)(一個(gè) C 數(shù)組)

- ( double *)doublesForPlot:( CPPlot *)plot field:( NSUInteger )fieldEnum recordIndexRange:( NSRange )indexRange

{

// 返回類型:一個(gè) double 指針(數(shù)組)

double *values;

NSString * identifier=( NSString *)[plot identifier];

switch (fieldEnum) {

// 如果請(qǐng)求的數(shù)據(jù)是散點(diǎn) x 坐標(biāo) , 直接返回 x 坐標(biāo)(兩個(gè)圖形是一樣的),否則還要進(jìn)一步判斷是那個(gè)圖形

case CPScatterPlotFieldX :

values= x ;

break ;

case CPScatterPlotFieldY :

// 如果請(qǐng)求的數(shù)據(jù)是散點(diǎn) y 坐標(biāo),則對(duì)于圖形 1 ,使用 y1 數(shù)組,對(duì)于圖形 2 ,使用 y2 數(shù)組

if ([identifier isEqualToString : @"Blue Plot" ]) {

values= y1 ;

} else

values= y2 ;

break ;

}

// 數(shù)組指針右移個(gè) indexRage.location 單位,則數(shù)組截去 indexRage.location 個(gè)元素

return values + indexRange. location ;

}

// 添加數(shù)據(jù)標(biāo)簽

-( CPLayer *)dataLabelForPlot:( CPPlot *)plot recordIndex:( NSUInteger )index

{

// 定義一個(gè)白色的 TextStyle

static CPTextStyle *whiteText = nil ;

if ( !whiteText ) {

whiteText = [[ CPTextStyle alloc ] init ];

whiteText. color = [ CPColor whiteColor ];

}

// 定義一個(gè) 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繪制直方圖和折線圖


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 欧美在线网站 | 欧美色伊人 | 日本无码免费久久久精品 | 五月综合久久 | 色综合图| www.com黄| 亚洲一区二区中文字幕 | 日本女人毛茸茸 | 成人国产精品免费视频不卡 | 久久aⅴ乱码一区二区三区 日韩精品一区二区在线观看 | 欧美一区二区三 | 天天干天天干天天 | 日本黄 色 成 年 人免费观看 | 亚洲日韩中文字幕一区 | 欧美久久久网站 | 欧美综合激情网 | 黄色7777 | 色婷婷色综合激情国产日韩 | 九九在线视频 | www97影院| 久久久久亚洲精品 | 亚洲一区中文字幕在线观看 | 大看蕉a在线观看 | 98色花堂国产第一页 | 欧美一级毛片高清免费观看 | 国产乱码精品一区二区三区中文 | 欧美一区二区在线观看视频 | 亚洲精品久久久久综合中文字幕 | 色综合天天综合网国产成人网 | 欧美 日韩 国产 一区 | 国产精品视频免费 | 欧美一区二区免费电影 | 精品在线91 | 美女国产网站 | 国产一区二 | 国内精品一区二区在线观看 | 久久国产欧美日韩精品 | 日本在线视 | 一级黄片毛片 | 亚洲欧美色国产综合 | 日韩a电影|