2014-05-19 178 views
0

我開始變得勇敢,並試圖將JBChartView(特別是折線圖)填充到我的統計跟蹤應用程序中。我從來沒有使用過圖形庫,但我盡了最大的努力去研究它們,這看起來非常強大。問題是我無法從測試數據中正確地獲取繪圖。實施JBChartView時遇到困難

當我運行代碼如下,我得到[__NSCFConstantString objectAtIndex:] unrecognized selector sent to instance。有問題的路線是verticalValueforHorizontalIndex,但我找不出產生任何數據的另一種方法。有沒有人有任何這方面的經驗,或任何人都可以幫我弄清楚爲什麼我在這裏遇到這麼多麻煩?

實現文件

#import "waterStatsViewController.h" 
#import "JBLineChartView.h" 
#import "JBChartView.h" 
#import "JBBarChartView.h" 
#import "JBChartHeaderView.h" 
#import "JBLineChartFooterView.h" 

typedef NS_ENUM(NSInteger, JBLineChartLine){ 
JBLineChartLineSolid, 
JBLineChartLineDashed, 
JBLineChartLineCount 
}; 

@interface waterStatsViewController() 

- (void)initData; 

@end 

@implementation waterStatsViewController 

- (id)init 
{ 
self = [super init]; 
if (self) 
{ 
    [self initData]; 
} 
return self; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
self = [super initWithCoder:aDecoder]; 
if (self) 
{ 
    [self initData]; 
} 
return self; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) 
{ 
    [self initData]; 
} 
return self; 
} 

- (void) initData 
{ 
// DEFINE ARRAYS 
testArray1 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", nil]; 
testArray2 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", nil]; 

// CREATE MUTABLE ARRAY FOR LINES IN CHART 
NSMutableArray *mutableLineCharts = [NSMutableArray array]; 

// AS LONG AS LINEINDEX IS LESS THEN THE LINE COUNT, INCREASE THE LINE COUNT AND EXECUTE CODE 
for (int lineIndex = 0; lineIndex<JBLineChartLineCount; lineIndex++) 
{ 
    // CREATE MUTABLE ARRAY FOR DATA IN CHART 
    NSMutableArray *mutableChartData = [NSMutableArray array]; 

    // AS LONG AS INT I IS LESS THEN THE COUNT OF OBJECTS IN TEST ARRAY 2; INCREASE COUNT AND EXECUTE CODE 
    for (int i = 0; i < testArray2.count; i++) 
    { 
     // ADD OBJECTS FROM TEST ARRAY 2 TO CHART DATA 
     [mutableChartData addObjectsFromArray:testArray2]; 
    } 
    // TAKE OBJECTS FROM MUTABLE CHART DATA AND ADD THEM TO OHHHHHH FOR EACH ITEM YOU ADD ANOTHER LINE 
    [mutableLineCharts addObjectsFromArray:mutableChartData]; 
} 
} 


- (void)viewDidLoad 
{ 
self.title = @"Water Quality"; 

_chartView = [[JBLineChartView alloc] init]; 
_chartView.delegate = self; 
_chartView.dataSource = self; 
_chartView.state = 0; 
_chartView.backgroundColor = [UIColor blackColor]; 
_chartView.showsLineSelection = YES; 
_chartView.showsVerticalSelection = YES; 

_headerView = [[JBChartHeaderView alloc] initWithFrame:CGRectMake(0, 64, 320, 30)]; 
_chartView.frame = CGRectMake(0, 94, 320, 300); 
_footerView = [[JBLineChartFooterView alloc] initWithFrame:CGRectMake(0, 404, 320, 30)]; 

_headerView.titleLabel.text = @"Alkalinity"; 
_headerView.titleLabel.textColor = [UIColor whiteColor]; 

_footerView.leftLabel.text = [testArray1 firstObject]; 
_footerView.rightLabel.text = [testArray1 lastObject]; 
_footerView.leftLabel.textColor = [UIColor whiteColor]; 
_footerView.rightLabel.textColor = [UIColor whiteColor]; 
_footerView.backgroundColor = [UIColor blackColor]; 
_footerView.sectionCount = [testArray1 count]; 

// THIS IS THE VIEW WHEN THE USER INTERACTS WITH THE CHART 
/* 
_informationView = [[JBChartInformationView alloc] initWithFrame:CGRectMake(0, 0, 40, 300)]; 
[_informationView setBackgroundColor:[UIColor grayColor]];*/ 


[_chartView setMinimumValue:1.0f]; 
[_chartView setMaximumValue:20.0f]; 

[self.view addSubview:_footerView]; 
[self.view addSubview:_headerView]; 
[self.view addSubview:_chartView]; 
// [self.view addSubview:_informationView]; 
[_chartView reloadData]; 

[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
} 

- (BOOL)lineChartView:(JBLineChartView *)lineChartView showsDotsForLineAtLineIndex:(NSUInteger)lineIndex; 
{ 
return YES; 
} 

- (NSUInteger)numberOfLinesInLineChartView:(JBLineChartView *)lineChartView; 
{ 
return 1; 
} 

- (NSUInteger)lineChartView:(JBLineChartView *)lineChartView numberOfVerticalValuesAtLineIndex:(NSUInteger)lineIndex; 
{ 
return 1; 
} 


- (CGFloat)lineChartView:(JBLineChartView *)lineChartView verticalValueForHorizontalIndex:(NSUInteger)horizontalIndex atLineIndex:(NSUInteger)lineIndex; 
{ 
return [[[testArray2 objectAtIndex:lineIndex] objectAtIndex:horizontalIndex] floatValue]; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

回答

3

的幾個問題:

  • 數值不要存儲在您的測試數組的字符串。
  • 刪除mutableLineCharts代碼;這是演示中剩餘的,不需要用於你的目的。
  • 我假設你想要兩條線?一個虛線和一個實體?如果是這樣,則返回JBLineChartLineCount,而不是1的numberOfLinesInLineChartView。
  • 返回verticalValueForHorizo​​ntalIndex中testArray1或testArray2的數值。

初始化數據

- (void) initData 
{ 
    testArray1 = @[@(1), @(2), @(3), @(4), @(5), @(6), @(7), @(8), @(9), @(10)]; 
    testArray2 = @[@(11), @(12), @(13), @(14), @(15), @(16), @(17), @(18), @(19), @(20)]; 
} 

行數

- (NSUInteger)numberOfLinesInLineChartView:(JBLineChartView *)lineChartView; 
{ 
    return JBLineChartLineCount; 
} 

數據計數

- (NSUInteger)lineChartView:(JBLineChartView *)lineChartView numberOfVerticalValuesAtLineIndex:(NSUInteger)lineIndex; 
{ 
    if (lineIndex == JBLineChartLineSolid) 
    { 
     return [self.testArray1 count]; 
    } 
    else 
    { 
     return [self.testArray2 count]; 
    } 
    return 0; 
} 

數據值

- (CGFloat)lineChartView:(JBLineChartView *)lineChartView verticalValueForHorizontalIndex:(NSUInteger)horizontalIndex atLineIndex:(NSUInteger)lineIndex; 
{ 
    if (lineIndex == JBLineChartLineSolid) 
    { 
     NSNumber *value = (NSNumber *)[self.testArray1 objectAtIndex:horizontalIndex]; 
     return [value floatValue]; 
    } 
    else 
    { 
     NSNumber *value = (NSNumber *)[self.testArray2 objectAtIndex:horizontalIndex]; 
     return [value floatValue]; 
    } 
    return 0; 
} 

希望這有助於。

+0

是的。那樣做了;除了我的數組的原始佈局被接受,但您列出的方式拋出了一個錯誤。這是巨大的。謝謝。 –