Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
245
Creating IGDataSource,IGChartView with custom objects.
posted

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.

Parents
  • 4940
    Suggested Answer
    Offline posted

    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;
    }

    @end

    @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;

    }

    @end

Reply Children