我开始勇敢地尝试将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
答案 0 :(得分:3)
一些问题:
初始化数据
- (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;
}
希望这会有所帮助。