2017-04-26 54 views
0

如何將可變數組中的值插入條形圖?使用可變數組的條形圖

#import "thirdViewController.h" 


@interface thirdViewController() 
{ 

    PNBarChart * barChart; 
} 
@end 

@implementation thirdViewController 

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    self.lblpass.text = [NSString stringWithFormat:@"%d",self.pass ]; 
    self.lblfailed.text = [NSString stringWithFormat:@"%d",self.failed ]; 

    [self getdata]; 
} 
-(void)getdata 
{ 
    barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+10, self.view.frame.origin.y+300, SCREEN_WIDTH-50, 200.0)]; 
    [barChart setXLabels:@[@"pass",@"Failed"]]; 
    [barChart setYValues:@[@1, @2, @3, @4, @5]]; 
    [barChart strokeChart]; 
    [self.view addSubview:barChart]; 
} 

我已經試過上面的代碼。

+0

我會建議使用https://github.com/danielgindi/Charts它已經不僅僅是柱狀圖等各種圖表和支持是真棒。我已經在我的兩個項目中使用了它,並且似乎完美地工作。 – satheeshwaran

回答

0

關於PNBarChart的Github文檔很好地展示瞭如何使用這個類。

https://github.com/kevinzhow/PNChart

//For Line Chart 
PNLineChart * lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)]; 
[lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5"]]; 

// Line Chart No.1 
NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2]; 
PNLineChartData *data01 = [PNLineChartData new]; 
data01.color = PNFreshGreen; 
data01.itemCount = lineChart.xLabels.count; 
data01.getData = ^(NSUInteger index) { 
    CGFloat yValue = [data01Array[index] floatValue]; 
    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 = PNTwitterColor; 
data02.itemCount = lineChart.xLabels.count; 
data02.getData = ^(NSUInteger index) { 
    CGFloat yValue = [data02Array[index] floatValue]; 
    return [PNLineChartDataItem dataItemWithY:yValue]; 
}; 

lineChart.chartData = @[data01, data02]; 
[lineChart strokeChart];