PNChart:一個簡潔高效的iOS圖表庫

IzettaHafne 7年前發布 | 22K 次閱讀 LineChart iOS開發 移動開發

1.要求

PNChart 依賴于下列框架,在使用前請導入這些框架(ps:至于怎么導入,這里就不說了):

  • Foundation.framework
  • UIKit.framework
  • CoreGraphics.framework
  • QuartzCore.framework
    切記 :這個框架依賴于第三方框架:UICountingLabel 請自行去 https://github.com 下載,并添加到這個框架中。

2.使用

2.1 折線圖使用

折線圖.png

PNLineChart * lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
    // 設置x軸上坐標內容
    [lineChart setXLabels:@[@"1",@"2",@"3",@"4",@"5"]];
    // 設置好像沒什么用
    lineChart.xLabelColor = [UIColor orangeColor];

lineChart.showLabel = YES;
// 是否顯示Y軸的數值
lineChart.showGenYLabels = YES;
// 是否顯示橫向虛線
lineChart.showYGridLines = YES;
// 是否平滑的曲線
lineChart.showSmoothLines = NO;
// 是否顯示xy 坐標軸
lineChart.showCoordinateAxis = YES;
// 軸的顏色
lineChart.axisColor = [UIColor orangeColor];
// 軸的寬度
lineChart.axisWidth = 2.0f;

NSLog(@"%f",lineChart.chartMarginLeft);

//    lineChart.thousandsSeparator = YES;
// 設置y軸坐標的顏色
lineChart.yLabelColor = [UIColor redColor];

// Line Chart No.1
NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2];
PNLineChartData *data01 = [PNLineChartData new];
data01.color = PNFreshGreen;
data01.dataTitle = @"Hello World";
// 設置點的格式
data01.inflexionPointStyle = PNLineChartPointStyleCircle;
data01.inflexionPointColor = [UIColor purpleColor];
// 是否點label
data01.showPointLabel = YES;
data01.pointLabelColor = [UIColor redColor];
data01.pointLabelFont = [UIFont systemFontOfSize:12];
data01.pointLabelFormat = @"%1.1f";
// 設置折線有幾個值
data01.itemCount = lineChart.xLabels.count;
data01.getData = ^(NSUInteger index) {
    CGFloat yValue = [data01Array[index] floatValue];

    // 設置x軸坐標對應的y軸的值
    return [PNLineChartDataItem dataItemWithY:yValue];
};
// Line Chart No.2
NSArray * data02Array = @[@20.1, @180.1, @26.4, @202.2, @126.2];
PNLineChartData *data02 = [PNLineChartData new];
data02.color = PN推terColor;
data02.itemCount = lineChart.xLabels.count;
data02.getData = ^(NSUInteger index) {
    CGFloat yValue = [data02Array[index] floatValue];
    return [PNLineChartDataItem dataItemWithY:yValue];
};
// 設置line的數據數組
lineChart.chartData = @[data01, data02];
// 繪制出來
[lineChart strokeChart];

[self.view addSubview:lineChart];</code></pre> 

2.2 柱狀圖使用

柱狀圖.png

PNBarChart *barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
    // 是否顯示xy 軸的數字
    barChart.showLabel = YES;
    // 是否顯示水平線 但把柱子壓低上移了
    //    barChart.showLevelLine = YES;
    //是否顯示xy 軸
    barChart.showChartBorder = YES;
    // 是否顯示柿子的數值
    barChart.isShowNumbers = YES;
    // 立體顯示
    barChart.isGradientShow = YES;
    // 設置柱子的圓角
    barChart.barRadius = 5;
    // 設置bar color
    barChart.strokeColor = [UIColor redColor];

barChart.xLabels = @[@"1",@"2",@"3",@"4",@"5"];

barChart.yValues = @[@"2",@"4",@"1",@"10",@"9"];

barChart.yLabelFormatter = ^ (CGFloat yLabelValue) {

    return [NSString stringWithFormat:@"%f",yLabelValue];
};

[barChart strokeChart];

[self.view addSubview:barChart];</code></pre> 

2.3 circle charet 使用

circle Chart.png

// 設置圓狀圖
    PNCircleChart *circleChart = [[PNCircleChart alloc]initWithFrame:CGRectMake(100, 50, 200, 200) total:@100 current:@10 clockwise:NO shadow:YES shadowColor:[UIColor grayColor] displayCountingLabel:YES overrideLineWidth:@15];

circleChart.chartType = PNChartFormatTypePercent;
circleChart.strokeColor = [UIColor greenColor];

[circleChart strokeChart];

[self.view addSubview:circleChart];</code></pre> 

2.4 餅狀圖使用

餅狀圖.png

NSArray *items = @[[PNPieChartDataItem dataItemWithValue:30 color:PNBrown description:@"cat"],[PNPieChartDataItem dataItemWithValue:20 color:PNDarkBlue description:@"pig"], [PNPieChartDataItem dataItemWithValue:50 color:PNGrey description:@"dog"]];

PNPieChart *pieChart = [[PNPieChart alloc] initWithFrame:CGRectMake(100, 100, 200, 200) items:items];

pieChart.delegate = self;
[pieChart strokeChart];
// 加到父視圖上
[self.view addSubview:pieChart];

// 顯示圖例
pieChart.hasLegend = YES;
// 橫向顯示
pieChart.legendStyle = PNLegendItemStyleSerial;
// 顯示位置
pieChart.legendPosition = PNLegendPositionTop;
// 獲得圖例 當橫向排布不下另起一行
UIView *legend = [pieChart getLegendWithMaxWidth:100];
legend.frame = CGRectMake(100, 300, legend.bounds.size.width, legend.bounds.size.height);
[self.view addSubview:legend];</code></pre> 

 

來自:http://www.jianshu.com/p/1dc4af401be5

 

 本文由用戶 IzettaHafne 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!