BSChartView

  1. A tool to support folding line chart, bar chart, pie chart
  2. Support double Y axis display, support left and right sliding, support custom styles.

The effect



Github.com/FreeBaiShun…

Pod integration

pod ‘BSChartView’

usage

It’s kind of like using tableView

// // viewController. m // // Created by BS on 2019/8/25. // Copyright © 2019 BS. All rights reserved#import "ViewController.h"
#import "BSLineChartView.h"
#import "BSPieChartView.h"

#define HEX_COLOR(hex) [UIColor colorWithRed:(float)((hex &0xFF0000)>>16))/255.0 green:((float)((hex)(hex) & 0 xff00) > > 255.0 blue: (8))/(float) (hex & 0 XFF))/alpha 255.0:1] / / / category has this macro@interface ViewController ()<LineChartViewDataSource, PieChartViewDataSource> @property (copy, nonatomic) NSMutableArray *arrMX; @property (copy, nonatomic) NSMutableArray *arrMData; @property (copy, nonatomic) NSMutableArray *arrMData1; @property (copy, nonatomic) NSMutableArray *arrMColumn; @property (copy, nonatomic) NSMutableArray *arrMPieData; @ Property (Copy, nonatomic) NSMutableArray *arrMPieTitles; @property (Copy, nonatomic) NSMutableArray *arrMPieDataChange; @property (Copy, nonatomic) NSMutableArray *arrMPieTitlesChange; @property (strong, nonatomic) UIButton *btnRefresh; @end @implementation ViewController{ BSLineChartView *lineChartView; int count; BSPieChartView *chartView; NSArray *arrPieData; NSArray *arrPieTitleData; } // lazy load data source - (NSMutableArray *)arrMX{if (_arrMX == nil) {
_arrMX = [NSMutableArray array];
for (int i = 1; i <= 30; i++) {
[_arrMX addObject:[NSString stringWithFormat:@"11-%d",i]]; }}return _arrMX;
}

- (NSMutableArray *)arrMData{
if (_arrMData == nil) {
_arrMData = [NSMutableArray array];
for (int i = 1; i <= 30; i++) {
if (i %2 == 0) {
[_arrMData addObject:@"200"];
}else{
[_arrMData addObject:@"100"]; }}}return _arrMData;
}

- (NSMutableArray *)arrMData1{
if (_arrMData1 == nil) {
_arrMData1 = [NSMutableArray array];
for (int i = 1; i <= 30; i++) {
if (i %2 == 0) {
[_arrMData1 addObject:@"100"];
}else{
[_arrMData1 addObject:@"200"]; }}}return _arrMData1;
}

- (NSMutableArray *)arrMColumn{
if (_arrMColumn == nil) {
_arrMColumn = [NSMutableArray array];
for (int i = 1; i <= 30; i++) {
if (i %2 == 0) {
[_arrMColumn addObject:@"150"];
}else{
[_arrMColumn addObject:@"250"]; }}}return _arrMColumn;
}

- (NSMutableArray *)arrMPieData{
if(_arrMPieData = = nil) {_arrMPieData = [NSMutableArray arrayWithArray: @ [@ (0.1), @ (0.25), @ (0.2), @ (0.1), @ (0.15), @ (0.2)]]; }return _arrMPieData;
}

- (NSMutableArray *)arrMPieTitles{
if (_arrMPieTitles == nil) {
_arrMPieTitles = [NSMutableArray arrayWithArray:@[@"10.00% \ n400 people"The @"25.00% \ n400 people"The @"20.00% \ n400 people"The @"10.00% \ n400 people"The @"15.00% \ n400 people"The @"20.00% \ n400 people"]];
}
return _arrMPieTitles;
}

- (NSMutableArray *)arrMPieDataChange{
if(_arrMPieDataChange == nil) {_arrMPieDataChange = [NSMutableArray arrayWithArray:@[@(0.2), @(0.15), @(0.1), @(0.2), @ (0.25), @ (0.1)]]; }return _arrMPieDataChange;
}

- (NSMutableArray *)arrMPieTitlesChange{
if (_arrMPieTitlesChange == nil) {
_arrMPieTitlesChange = [NSMutableArray arrayWithArray:@[@"20.00% \ n100 people"The @"15.00% \ n100 people"The @"10.00% \ n100 people"The @"20.00% \ n100 people"The @"25.00% \ n100 people"The @"10.00% \ n100 people"]];
}
return_arrMPieTitlesChange; } - (void)viewDidLoad { [super viewDidLoad]; arrPieData = self.arrMPieData; arrPieTitleData = self.arrMPieTitles; self.view.backgroundColor = [UIColor whiteColor]; // Line graph, bar graph (can support double Y axis, a line graph Y axis, A cylindrical Y) lineChartView = [[BSLineChartView alloc] initWithFrame: CGRectMake (0, 0, the self. The frame. The size, width, self.view.frame.size.height/2-64)]; / / optional configuration (before the dataSource configuration will only take effect) lineChartView. DisplayVerticalLine = NO; / / show the vertical bar lineChartView. YScaleAdapt = 50; Linechartview. YTextColor = [UIColor orangeColor]; linechartView. YTextColor = [UIColor orangeColor]; linechartView. YTextColor = [UIColor orangeColor]; Linechartview. fontSize = @13; linechartView. fontSize = @13; // linechartView.fontname = @"PingFangSC-Medium"; / / font lineChartView isTextVague = YES; // LinechartView.datasource = self; [self.view addSubview:lineChartView]; / / pie chart chartView = [[BSPieChartView alloc] initWithFrame: CGRectMake (0, the self. The view. Frame. The size. Height / 2-64. self.view.frame.size.width, self.view.frame.size.height/2-64)]; Chartview. isTextVague = YES; chartview. isTextVague = YES; Chartview.datasource = self; [self.view addSubview:chartView]; / / to refresh data self. BtnRefresh = [[UIButton alloc] initWithFrame: CGRectMake (50, the self. The view. The frame. The size, height - 100, 80, 50)]; [self.btnRefreshsetBackgroundColor:[UIColor blueColor]];
[self.btnRefresh setTitle:@"Refresh data" forState:UIControlStateNormal];
[self.btnRefresh addTarget:self action:@selector(btnRefreshClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btnRefresh];
}

#pragma mark - LineChartViewDataSource
- (NSArray *)titlesArrayOfLineChartView:(BSLineChartView *)lineChartView
{
return self.arrMX;
}
- (NSArray *)lineValuesArrayOfLineChartView:(BSLineChartView *)lineChartView
{
return @[self.arrMData];
}
- (NSArray *)barValuesArrayOfLineChartView:(BSLineChartView *)lineChartView
{
return self.arrMColumn;
}
- (NSArray *)colorsArrayOfLineChartView:(BSPieChartView *)pieChartView
{
return @[@[[UIColor redColor], [UIColor greenColor]],
[UIColor lightGrayColor]];
}


#pragma mark - PieChartViewDataSource
- (CGFloat)radiusOfPieChartView:(BSPieChartView *)pieChartView
{
return self.view.bounds.size.width/5;
}
- (NSArray *)valuseArrayOfPieChartView:(BSPieChartView *)pieChartView
{
return arrPieData;
}
- (NSArray *)colorsArrayOfPieChartView:(BSPieChartView *)pieChartView
{
return@[HEX_COLOR(0xd32f35), HEX_COLOR(0xe653f1), HEX_COLOR(0xa46b3e), HEX_COLOR(0x2f4567), HEX_COLOR(0xaa4111), HEX_COLOR(0xd11111)]; } // customize title (optional protocol) - (NSArray *)valuesTitlesChartView:(BSPieChartView *)pieChartView{return arrPieTitleData;
}

#pragma Mark - Refresh data button is clicked
- (void)btnRefreshClick{
count ++;
if (count %2 == 0) {
self.arrMData = self.arrMData1;
arrPieData = self.arrMPieData;
arrPieTitleData = self.arrMPieTitles;
}else{
self.arrMData = self.arrMColumn;
arrPieData = self.arrMPieDataChange;
arrPieTitleData = self.arrMPieTitlesChange;
}

[lineChartView refreshAllData];
[chartView refreshAllData];
}
@end
Copy the code