OC 整合Charts 及使用

b10l07發表於2018-09-08
  1. OC 使用 pod整合 Charts庫時 要在podefie中 加上 use_frameworks! ! 如圖


    7423586-3a8d959794ea1a9c.png
    a.png

2.在OC專案建立 swift的檔案並生成橋接檔案


7423586-e2acafbda0f9b00e.png
b.png
7423586-55d45d5203a3bb94.png
E3D1F41FBF5F1010FB169F61C8FF49FD.png
7423586-2b82ced75cc88cd2.png
E1C4F1204DAD4C10EB3366F6CEE2B94B.png
7423586-5ef915ed72e78b73.png
00AC03009493D6EBC61E52E4DE463113.png
7423586-95048e6b852ab198.png
8D2139D4E3C6EB8DC11D7E678F7BF6D1.png

3.如果整合報錯做如下操作


7423586-69bbbf777bd6db07.png
F00E1B3FBDB1D870326DC41770064267.png

到這裡就將Charts 整合到及的專案中啦

例項1.折線的建立
-(void)zxinit
{

self.lineView=[[LineChartView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 300)];
[self.view addSubview:self.lineView];
self.lineView.delegate=self;
//設定折線圖描述
self.lineView.noDataText=@"戰無資料"; //沒資料時顯示
//self.lineView.drawBordersEnabled=YES; //
// self.lineView.drawGridBackgroundEnabled=NO;
self.lineView.scaleYEnabled=NO; //啟動Y軸縮放
self.lineView.scaleXEnabled=NO; //啟動X軸縮放
self.lineView.doubleTapToZoomEnabled=NO; //取消雙擊縮放
self.lineView.dragEnabled=YES; //啟動拖拽圖示
self.lineView.dragDecelerationEnabled=YES; //拖拽後是否有慣性效果
self.lineView.dragDecelerationFrictionCoef = 0.9;//拖拽後慣性效果的摩擦係數(0~1),數值越小,慣性越不明顯
//設定X軸樣式
ChartXAxis *xAxis = self.lineView.xAxis;
xAxis.axisLineWidth = 1.0/[UIScreen mainScreen].scale;//設定X軸線寬
xAxis.labelCount=11;
xAxis.labelPosition = XAxisLabelPositionBottom;//X軸的顯示位置,預設是顯示在上面的
xAxis.drawGridLinesEnabled = NO;//不繪製網格線
// xAxis.spaceBetweenLabels = 4;//設定label間隔
xAxis.labelTextColor = [UIColor orangeColor];//label文字顏色

//設定Y軸樣式
self.lineView.rightAxis.enabled = NO;//不繪製右邊軸
ChartYAxis *leftAxis = self.lineView.leftAxis;//獲取左邊Y軸
leftAxis.labelCount = 11;//Y軸label數量,數值不一定,如果forceLabelsEnabled等於YES, 則強制繪製制定數量的label, 但是可能不平均
leftAxis.forceLabelsEnabled = NO;//不強制繪製指定數量的label
// leftAxis.showOnlyMinMaxEnabled = NO;//是否只顯示最大值和最小值
leftAxis.axisMinimum = 0;//設定Y軸的最小值
leftAxis.drawZeroLineEnabled = YES;//從0開始繪製
leftAxis.axisMaximum = 105;//設定Y軸的最大值
leftAxis.inverted = NO;//是否將Y軸進行上下翻轉
leftAxis.axisLineWidth = 1.0/[UIScreen mainScreen].scale;//Y軸線寬
leftAxis.axisLineColor = [UIColor blackColor];//Y軸顏色
// leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];//自定義格式
// leftAxis.valueFormatter.positiveSuffix = @" $";//數字字尾單位
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
leftAxis.labelTextColor =[UIColor orangeColor];//文字顏色
leftAxis.labelFont = [UIFont systemFontOfSize:10.0f];//文字字型

//設定網格樣式
leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//設定虛線樣式的網格線
leftAxis.gridColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//網格線顏色
leftAxis.gridAntialiasEnabled = YES;//開啟抗鋸齒

//設定限制線
ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:100 label:@"限制線"];
limitLine.lineWidth = 2;
limitLine.lineColor = [UIColor greenColor];
limitLine.lineDashLengths = @[@5.0f, @5.0f];//虛線樣式
limitLine.labelPosition = ChartLimitLabelPositionLeftBottom;//位置
limitLine.valueTextColor =[UIColor blueColor];//label文字顏色
limitLine.valueFont = [UIFont systemFontOfSize:12];//label字型
[leftAxis addLimitLine:limitLine];//新增到Y軸上
leftAxis.drawLimitLinesBehindDataEnabled = YES;//設定限制線繪製在折線圖的後面

//設定折線圖描述及圖例樣式
//[self.lineView setDescriptionText:@"折線圖"];//折線圖描述
// [self.lineView setDescriptionTextColor:[UIColor darkGrayColor]];
//   self.lineView.legend.form = ChartLegendFormLine;//圖例的樣式
//  self.lineView.legend.formSize = 30;//圖例中線條的長度
//  self.lineView.legend.textColor = [UIColor darkGrayColor];//圖例文字顏色

//汽包
BalloonMarker *markerA=[[BalloonMarker alloc]initWithColor:[UIColor orangeColor] font:[UIFont systemFontOfSize:12] textColor:[UIColor whiteColor] insets:UIEdgeInsetsMake(9.0, 8.0, 20.0, 8.0)];
markerA.chartView=self.lineView;
markerA.minimumSize=CGSizeMake(50.f, 25.f);
self.lineView.marker=markerA;

self.lineView.data=[self zxData];

}

-(LineChartData *)zxData
{
int xVals_count = 12;//X軸上要顯示多少條資料
double maxYVal = 100;//Y軸的最大值
//X軸上面需要顯示的資料
NSMutableArray *xVals = [[NSMutableArray alloc] init];
for (int i = 0; i < xVals_count; i++) {
[xVals addObject:[NSString stringWithFormat:@"%d", i+1]];
}

//對應Y軸上面需要顯示的資料
NSMutableArray *yVals = [[NSMutableArray alloc] init];
for (int i = 0; i < xVals_count; i++) {
    double mult = maxYVal + 1;
    double val = (double)(arc4random_uniform(mult));
    ChartDataEntry *entry =[[ChartDataEntry alloc]initWithX:i y:val]; //[[ChartDataEntry alloc] initWithValue:val xIndex:i];
    [yVals addObject:entry];
}

LineChartDataSet *set1 = nil;
if (self.lineView.data.dataSetCount > 0) {
    LineChartData *data = (LineChartData *)self.lineView.data;
    set1 = (LineChartDataSet *)data.dataSets[0];
    //set1.=yVals;
    return data;
}else{
    //建立LineChartDataSet物件
    set1=[[LineChartDataSet alloc]initWithValues:yVals label:@"A"];
    //設定折線的樣式
    set1.lineWidth = 1.0/[UIScreen mainScreen].scale;//折線寬度
    set1.drawValuesEnabled = YES;//是否在拐點處顯示資料
    set1.valueColors = @[[UIColor brownColor]];//折線拐點處顯示資料的顏色
    [set1 setColor:[UIColor greenColor]];//折線顏色
    //set1.drawSteppedEnabled = NO;//是否開啟繪製階梯樣式的折線圖
    
    //折線拐點樣式
    set1.drawCirclesEnabled = NO;//是否繪製拐點
    set1.circleRadius = 4.0f;//拐點半徑
    set1.circleColors = @[[UIColor redColor], [UIColor greenColor]];//拐點顏色
    //拐點中間的空心樣式
    set1.drawCircleHoleEnabled = YES;//是否繪製中間的空心
    set1.circleHoleRadius = 2.0f;//空心的半徑
    set1.circleHoleColor = [UIColor blackColor];//空心的顏色
    //折線的顏色填充樣式
    //第一種填充樣式:單色填充
    //        set1.drawFilledEnabled = YES;//是否填充顏色
    //        set1.fillColor = [UIColor redColor];//填充顏色
    //        set1.fillAlpha = 0.3;//填充顏色的透明度
    //第二種填充樣式:漸變填充
    set1.drawFilledEnabled = YES;//是否填充顏色
    NSArray *gradientColors = @[(id)[ChartColorTemplates colorFromString:@"#FFFFFFFF"].CGColor,
                                (id)[ChartColorTemplates colorFromString:@"#FF007FFF"].CGColor];
    CGGradientRef gradientRef = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
    set1.fillAlpha = 0.3f;//透明度
    set1.fill = [ChartFill fillWithLinearGradient:gradientRef angle:90.0f];//賦值填充顏色物件
    CGGradientRelease(gradientRef);//釋放gradientRef
    
    //點選選中拐點的互動樣式
    set1.highlightEnabled = YES;//選中拐點,是否開啟高亮效果(顯示十字線)
    set1.highlightColor = [UIColor darkGrayColor];//點選選中拐點的十字線的顏色
    set1.highlightLineWidth = 1.0/[UIScreen mainScreen].scale;//十字線寬度
    set1.highlightLineDashLengths = @[@5, @5];//十字線的虛線樣式
    
    //將 LineChartDataSet 物件放入陣列中
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    
    //新增第二個LineChartDataSet物件
    LineChartDataSet *set2 = [set1 copy];
    NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
    for (int i = 0; i < xVals_count; i++) {
        double mult = maxYVal + 1;
        double val = (double)(arc4random_uniform(mult));
        ChartDataEntry *entry =[[ChartDataEntry alloc]initWithX:i y:val];   //[[ChartDataEntry alloc] initWithValue:val xIndex:i];
        [yVals2 addObject:entry];
    }
    // set2.yVals = yVals2;
    set2.values=yVals2;
    set2.label=@"B";
    [set2 setColor:[UIColor redColor]];
    set2.drawFilledEnabled = YES;//是否填充顏色
    set2.fillColor = [UIColor redColor];//填充顏色
    set2.fillAlpha = 0.1;//填充顏色的透明度
    [dataSets addObject:set2];
    
    //建立 LineChartData 物件, 此物件就是lineChartView需要最終資料物件
    LineChartData *data=[[LineChartData alloc]initWithDataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:8.f]];//文字字型
    [data setValueTextColor:[UIColor grayColor]];//文字顏色
    // NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    //自定義資料顯示格式
    // [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    // [formatter setPositiveFormat:@"#0.0"];
    //  [data setValueFormatter:formatter];
    
    return data;
    
}

//效果如圖


7423586-4905efe73340628e.png
EA845473FC3484058848FC2BEE8F834D.png

例項2.條形圖
-(void)txinit
{

self.chartView=[[BarChartView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 300)];
self.chartView.delegate=self;
[self.view addSubview:self.chartView];
self.chartView.noDataText=@"戰無資料"; //沒資料時顯示
self.chartView.drawValueAboveBarEnabled=YES; //數值顯示在柱形的上面還是下面
self.chartView.drawBarShadowEnabled=NO; //是否繪製柱形的陰影背景
self.chartView.scaleYEnabled=NO; //啟動Y軸縮放
self.chartView.scaleXEnabled=NO; //啟動X軸縮放
self.chartView.doubleTapToZoomEnabled=NO; //取消雙擊縮放
self.chartView.dragEnabled=YES; //啟動拖拽圖示
self.chartView.dragDecelerationEnabled=YES; //拖拽後是否有慣性效果
self.chartView.dragDecelerationFrictionCoef = 0.9;//拖拽後慣性效果的摩擦係數(0~1),數值越小,慣性越不明顯

//設定X軸的樣式
ChartXAxis *xAxis=self.chartView.xAxis;
xAxis.axisLineWidth=1; //X軸的線寬
xAxis.labelPosition = XAxisLabelPositionBottom;//X軸的顯示位置,預設是顯示在上面的
xAxis.drawGridLinesEnabled = NO;//不繪製網格線
xAxis.labelCount=12; //label的個數
//xAxis. = 4;//設定label間隔,若設定為1,則如果能全部顯示,則每個柱形下面都會顯示label
//xAxis.spaceMin=0;
// xAxis.spaceMax=1;
xAxis.labelTextColor = [UIColor brownColor];//label文字顏色

//設定Y軸的樣式
self.chartView.rightAxis.enabled=NO; //不繪製右邊軸
ChartYAxis *leftAxis=self.chartView.leftAxis;
leftAxis.forceLabelsEnabled = NO;//不強制繪製制定數量的label
leftAxis.axisMinimum = 0;//設定Y軸的最小值
leftAxis.drawZeroLineEnabled = YES;//從0開始繪製
leftAxis.axisMaximum = 105;//設定Y軸的最大值
leftAxis.inverted = NO;//是否將Y軸進行上下翻轉
leftAxis.axisLineWidth = 0.5;//Y軸線寬
// leftAxis.axisMinLabels=1; //軸上標籤的最小數目
//leftAxis.axisMaxLabels=100;//軸上標籤的最大數目
leftAxis.axisLineColor = [UIColor blackColor];//Y軸顏色
leftAxis.labelCount = 5;  //通過labelCount屬性設定Y軸要均分的數量.設定的labelCount的值不一定就是Y軸要均分的數量,這還要取決於forceLabelsEnabled屬性,
leftAxis.forceLabelsEnabled = NO; //如果forceLabelsEnabled等於YES, 則強制繪製指定數量的label, 但是可能不是均分的
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
leftAxis.labelTextColor = [UIColor brownColor];//文字顏色
leftAxis.labelFont = [UIFont systemFontOfSize:10.0f];//文字字型
// 設定Y軸上標籤顯示數字的格式,程式碼如下:
//leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];//自定義格式
//leftAxis.valueFormatter.pos = @" $";//數字字尾單位
// 設定Y軸上網格線的樣式,程式碼如下:
leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//設定虛線樣式的網格線
leftAxis.gridColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//網格線顏色
leftAxis.gridAntialiasEnabled = YES;//開啟抗鋸齒
//設定限制線
ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:80 label:@"限制線"];
limitLine.lineWidth = 2;
limitLine.lineColor = [UIColor greenColor];
limitLine.lineDashLengths = @[@5.0f, @5.0f];//虛線樣式
limitLine.labelPosition = ChartLimitLabelPositionRightTop;//位置
[leftAxis addLimitLine:limitLine];//新增到Y軸上
leftAxis.drawLimitLinesBehindDataEnabled = YES;//設定限制線繪製在柱形圖的後面
self.chartView.legend.enabled=NO; //不顯示圖利說明
//    self.chartView.=@"";


//汽包
BalloonMarker *markerA=[[BalloonMarker alloc]initWithColor:[UIColor orangeColor] font:[UIFont systemFontOfSize:12] textColor:[UIColor whiteColor] insets:UIEdgeInsetsMake(9.0, 8.0, 20.0, 8.0)];
markerA.chartView=self.chartView;
markerA.minimumSize=CGSizeMake(50.f, 25.f);
self.chartView.marker=markerA;
self.chartView.data=[self txData]; //資料設定
[self.chartView animateWithYAxisDuration:1]; //設定動畫效果

}

//資料設定
-(BarChartData *)txData
{
int xValues=12; //X軸上要顯示多少條資料
double maxY=100;// Y軸的最大值

//X軸上面需要顯示的資料
NSMutableArray *xV=[[NSMutableArray alloc]init];
for (int i=0; i<xValues; i++) {
    [xV addObject:[NSString stringWithFormat:@"%d",I]];
}
//對應Y軸上面需要顯示的資料
NSMutableArray *yV=[[NSMutableArray alloc]init];
for (int i=0; i<xValues; i++) {
    
    double mult=maxY+1;
    double val=(double)(arc4random_uniform(mult));
    BarChartDataEntry *entry=[[BarChartDataEntry alloc]initWithX:i y:val];
    [yV addObject:entry];
}

//建立BarChartDataSet物件,其中包含有Y軸資料資訊,以及可以設定柱形樣式
BarChartDataSet *set1=[[BarChartDataSet alloc]initWithValues:yV label:nil];
// set1.barSpace = 0.2;//柱形之間的間隙佔整個柱形(柱形+間隙)的比例
set1.drawValuesEnabled = YES;//是否在柱形圖上面顯示數值
set1.highlightEnabled = YES;//點選選中柱形圖是否有高亮效果,(雙擊空白處取消選中)
[set1 setColors:ChartColorTemplates.material];//設定柱形圖顏色
//將BarChartDataSet物件放入陣列中
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
//建立BarChartData物件, 此物件就是barChartView需要最終資料物件
BarChartData *data=[[BarChartData alloc]initWithDataSets:dataSets];
[data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];//文字字型
[data setValueTextColor:[UIColor orangeColor]];//文字顏色
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
//自定義資料顯示格式
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setPositiveFormat:@"#0.0"];
//[data setValueFormatter:formatter];
return data;

}

效果如圖


7423586-e37be71e3ac98c22.png
D9F7359C81D1CAF7D7E4AE6120EBF0B0.png

例項3 餅狀圖
-(void)pieInit{

self.pieChartView=[[PieChartView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 300)];
self.pieChartView.delegate=self;
[self.view addSubview:self.pieChartView];
[self.pieChartView setExtraOffsetsWithLeft:5 top:10 right:70 bottom:5];//餅狀圖距離邊緣的間隙
self.pieChartView.usePercentValuesEnabled = YES;//是否根據所提供的資料, 將顯示資料轉換為百分比格式
self.pieChartView.dragDecelerationEnabled = YES;//拖拽餅狀圖後是否有慣性效果
self.pieChartView.drawSlicesUnderHoleEnabled= NO;//如果是的話,這個洞會穿透到薄片的內部尖端
self.pieChartView.chartDescription.enabled = NO;//餅狀圖描述
self.pieChartView.drawHoleEnabled = YES;//餅狀圖是否是空心
self.pieChartView.holeRadiusPercent = 0.5;//空心半徑佔比
self.pieChartView.holeColor = [UIColor clearColor];//空心顏色
self.pieChartView.transparentCircleRadiusPercent = 0.55;//半透明空心半徑佔比
self.pieChartView.transparentCircleColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.3];//半透明空心的顏色
if (self.pieChartView.isDrawHoleEnabled == YES) {
    self.pieChartView.drawCenterTextEnabled = YES;//是否顯示中間文字
    //普通文字
    // self.pieChartView.centerText = @"餅狀圖";//中間文字
    //富文字
    NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"餅狀圖bbb\n餅狀圖aaa"];
    [centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
                                NSForegroundColorAttributeName: [UIColor orangeColor]}
                        range:NSMakeRange(0, centerText.length)];
    self.pieChartView.centerAttributedText = centerText;
}

self.pieChartView.legend.maxSizePercent = 1;//圖例在餅狀圖中的大小佔比, 這會影響圖例的寬高
self.pieChartView.legend.formToTextSpace = 5;//文字間隔
self.pieChartView.legend.font = [UIFont systemFontOfSize:10];//字型大小
self.pieChartView.legend.textColor = [UIColor grayColor];//字型顏色
// self.pieChartView.legend.position = ChartLegendPositionBelowChartCenter;//圖例在餅狀圖中的位置
self.pieChartView.legend.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
self.pieChartView.legend.verticalAlignment = ChartLegendVerticalAlignmentTop;
self.pieChartView.legend.orientation = ChartLegendOrientationVertical;
self.pieChartView.legend.form = ChartLegendFormCircle;//圖示樣式: 方形、線條、圓形
self.pieChartView.legend.formSize = 8;//圖示大小
[self.pieChartView setRotationEnabled:false];//禁止拖拽
[self pieData];

}

-(PieChartData *)pieData
{
//每個區塊的資料
//每個區塊的名稱或描述
NSMutableArray *arr = [[NSMutableArray alloc]init];
NSMutableArray *nameArr = [[NSMutableArray alloc]init];
[arr addObject:@"6"];
[arr addObject:@"7"];
[arr addObject:@"2"];
[arr addObject:@"4"];
[nameArr addObject:@"A"];
[nameArr addObject:@"B"];
[nameArr addObject:@"C"];
[nameArr addObject:@"D"];
NSMutableArray *values = [[NSMutableArray alloc] init];
// IMPORTANT: In a PieChart, no values (Entry) should have the same xIndex (even if from different DataSets), since no values can be drawn above each other.
for (int i = 0; i < arr.count; I++)
{
NSString * aaa = arr[i];
double bb = aaa.doubleValue;
[values addObject:[[PieChartDataEntry alloc] initWithValue: bb label: nameArr[i]]];
}
//dataSet
PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:values label:@"aaaf"];
dataSet.drawValuesEnabled = YES;//是否繪製顯示資料
NSMutableArray *colors = [[NSMutableArray alloc] init];
[colors addObjectsFromArray:ChartColorTemplates.vordiplom];
[colors addObjectsFromArray:ChartColorTemplates.joyful];
[colors addObjectsFromArray:ChartColorTemplates.colorful];
[colors addObjectsFromArray:ChartColorTemplates.liberty];
[colors addObjectsFromArray:ChartColorTemplates.pastel];
[colors addObject:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
dataSet.colors = colors;//區塊顏色
dataSet.sliceSpace = 5;//相鄰區塊之間的間距
dataSet.selectionShift = 8;//選中區塊時, 放大的半徑
dataSet.xValuePosition = PieChartValuePositionInsideSlice;//名稱位置
dataSet.yValuePosition = PieChartValuePositionOutsideSlice;//資料位置
//資料與區塊之間的用於指示的折線樣式
dataSet.valueLinePart1OffsetPercentage = 0.85;//折線中第一段起始位置相對於區塊的偏移量, 數值越大, 折線距離區塊越遠
dataSet.valueLinePart1Length = 0.5;//折線中第一段長度佔比
dataSet.valueLinePart2Length = 0.4;//折線中第二段長度最大佔比
dataSet.valueLineWidth = 1;//折線的粗細
dataSet.valueLineColor = [UIColor brownColor];//折線顏色
//data
PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterPercentStyle;
formatter.maximumFractionDigits = 0;//小數位數
formatter.multiplier = @1.f;
[data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:formatter]];//設定顯示資料格式
[data setValueTextColor:[UIColor brownColor]];
[data setValueFont:[UIFont systemFontOfSize:10]];
self.pieChartView.data = data;
[ self.pieChartView setNeedsDisplay];
return data;

}

//效果如何


7423586-8106e90ccdb35d37.png
877D35DA0468A7F6289DC229E048033D.png

例項4 複合圖
/*複合圖/
-(void)comInit{

self.comView=[[CombinedChartView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 300)];
self.comView.delegate=self;
[self.view addSubview:self.comView];
self.comView.doubleTapToZoomEnabled=NO;//取消雙擊放大
self.comView.scaleXEnabled=YES; //設定X軸縮放
self.comView.scaleYEnabled=YES;//設定Y軸縮放
self.comView.dragDecelerationEnabled = YES;//拖拽後是否有慣性效果
self.comView.dragDecelerationFrictionCoef = 0.9;//拖拽後慣性效果的摩擦係數(0~1),數值越小,慣性越不明顯
self.comView.highlightPerTapEnabled = NO;//取消單擊高亮顯示
self.comView.highlightPerDragEnabled = NO;//取消拖拽高亮
//設定X軸的樣式
ChartXAxis *xAxis=self.comView.xAxis;
xAxis.axisLineWidth=1; //X軸的線寬
xAxis.labelPosition = XAxisLabelPositionBottom;//X軸的顯示位置,預設是顯示在上面的
xAxis.drawGridLinesEnabled = NO;//不繪製網格線
xAxis.labelCount=12; //label的個數
//xAxis. = 4;//設定label間隔,若設定為1,則如果能全部顯示,則每個柱形下面都會顯示label
//xAxis.spaceMin=0;
// xAxis.spaceMax=1;
xAxis.labelTextColor = [UIColor brownColor];//label文字顏色

//設定Y軸的樣式
self.comView.rightAxis.enabled=NO; //不繪製右邊軸
ChartYAxis *leftAxis=self.comView.leftAxis;
leftAxis.forceLabelsEnabled = NO;//不強制繪製制定數量的label
leftAxis.axisMinimum = 0;//設定Y軸的最小值
leftAxis.drawZeroLineEnabled = YES;//從0開始繪製
leftAxis.axisMaximum = 105;//設定Y軸的最大值
leftAxis.inverted = NO;//是否將Y軸進行上下翻轉
leftAxis.axisLineWidth = 0.5;//Y軸線寬
// leftAxis.axisMinLabels=1; //軸上標籤的最小數目
//leftAxis.axisMaxLabels=100;//軸上標籤的最大數目
leftAxis.axisLineColor = [UIColor blackColor];//Y軸顏色
leftAxis.labelCount = 5;  //通過labelCount屬性設定Y軸要均分的數量.設定的labelCount的值不一定就是Y軸要均分的數量,這還要取決於forceLabelsEnabled屬性,
leftAxis.forceLabelsEnabled = NO; //如果forceLabelsEnabled等於YES, 則強制繪製指定數量的label, 但是可能不是均分的
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
leftAxis.labelTextColor = [UIColor brownColor];//文字顏色
leftAxis.labelFont = [UIFont systemFontOfSize:10.0f];//文字字型
// 設定Y軸上標籤顯示數字的格式,程式碼如下:
//leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];//自定義格式
//leftAxis.valueFormatter.pos = @" $";//數字字尾單位
// 設定Y軸上網格線的樣式,程式碼如下:
leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//設定虛線樣式的網格線
leftAxis.gridColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//網格線顏色
leftAxis.gridAntialiasEnabled = YES;//開啟抗鋸齒
//設定限制線
ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:80 label:@"限制線"];
limitLine.lineWidth = 2;
limitLine.lineColor = [UIColor greenColor];
limitLine.lineDashLengths = @[@5.0f, @5.0f];//虛線樣式
limitLine.labelPosition = ChartLimitLabelPositionRightTop;//位置
[leftAxis addLimitLine:limitLine];//新增到Y軸上
leftAxis.drawLimitLinesBehindDataEnabled = YES;//設定限制線繪製在柱形圖的後面
self.comView.legend.enabled=NO; //不顯示圖利說明
[self.comView animateWithYAxisDuration:1];
//設定資料
CombinedChartData *comData=[[CombinedChartData alloc]init];
comData.barData=[self txData];
comData.lineData=[self zxData];
self.comView.data=comData;

}

效果圖


7423586-428c1af851822d20.png
51ABBD3604729282190DD2BD013F6C0B.png

相關文章