Good Morning,
I have already taken a look at your tutorials on how to create an IGChart. Unfortunately they examples provided only describe the process using randomly generated double values.
Presently I have an array of custom objects. Each object is a custom type called Graph Object.
@interface GraphObject : NSObject
@property (nonatomic,retain) NSString *Territory;
@property double Cost;
@end
I would like to have a datasource that can support an NSMutableArray of these objects. The chart should have
numeric values along the Y axis (Cost) with the Territory values along the X axis.
Can you please assist? I do enjoy the tools.
Best regards.
The data source helpers used with the IGChartView allow you to specify property paths should you want to use a custom object contained within an array. Below I've pasted code from the implementation file to help you get started. If you have any further questions don't hesitate to ask.
#import "igViewController.h"#import <IGChart/IGChart.h>
@interface GraphObject : NSObject @property (nonatomic,retain) NSString *territory; @property (nonatomic, assign) double cost;@end
@implementation GraphObject @synthesize cost, territory;@end
@interface igViewController () { IGChartView *_chart; NSMutableArray *_data; IGCategorySeriesDataSourceHelper *_source;}
@implementation igViewController
- (void)viewDidLoad{ [super viewDidLoad];
_data = [[NSMutableArray alloc] init]; [self generateData];
_source = [[IGCategorySeriesDataSourceHelper alloc] init]; _source.data = _data; _source.valuePath = @"cost"; _source.labelPath = @"territory";
_chart = [[IGChartView alloc] initWithFrame:CGRectInset(self.view.frame, 30.0, 30.0)]; [_chart setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; _chart.theme = [IGChartGradientThemes IGThemeDark]; _chart.delegate = self; [self.view addSubview:_chart];
[_chart addSeriesForType:[IGColumnSeries class] usingKey:@"series" withDataSource:_source firstAxisKey:@"xAxis" secondAxisKey:@"yAxis"];}
- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning];}
-(void)generateData{ NSArray *territories = [NSArray arrayWithObjects:@"Canada", @"Finland", @"Holland", @"Japan", @"USA", nil];
for (int i = 0; i < 5; i++) { GraphObject *graphObject = [[GraphObject alloc] init]; graphObject.territory = [territories objectAtIndex:i]; graphObject.cost = arc4random() % 100000; [_data addObject:graphObject]; }}
- (NSString *)chartView:(IGChartView *)chartView labelForAxis:(IGAxis *)axis withItem:(NSObject *)item{ if([axis.key isEqualToString:@"xAxis"]) //item for this axis is an IGCategoryPoint return ((IGCategoryPoint *)item).label; else if ([axis.key isEqualToString:@"yAxis"]) { //item for this axis is a NSNumber NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterCurrencyStyle]; return [formatter stringFromNumber:(NSNumber *)item]; }
return nil;
}
Hi Torey,
Thank you very much for your quick response. I do have one question. In this code:
Can I assume I can change it to this?
-(NSMutableArray *)generateData{ NSMutableArray *graphValues = [[NSMutableArray alloc]init];
GraphObject g1 = [[GraphObject alloc] init];
g1.Territory = @"West";
g1.Cost = 100;
[graphValues addObject:g1];
GraphObject g2 = [[GraphObject alloc] init];
g2.Territory = @"North";
g2.Cost = 90;
[graphValues addObject:g2];
GraphObject g3 = [[GraphObject alloc] init];
g3.Territory = @"East";
g3.Cost = 40;
[graphValues addObject:g3];
return graphValues;}
Would this work?
Again thank you for your help. I am thinking that this example would be great to add to the documentation as I would think alot of other people would benefit from this example.
Yes, it would work. If you need anymore help let us know.