Hi,
We are writing an app where a stack column series will have an unknown number of series at design time.
The number of series will depend on the user input of a particular analysis type.
Therefore, we cannot create an object with predefined column names. What would be the right way to implement such chart?
Thank you
Hi Aleksandr,
Our data source helper for stacked charts requires hard coded value references, which means a static number of fragments based on the data model you provide. Attached is a project that contains a custom data source that fixes the need for hard coded value references. You'll find projects written in C# and Objective-C. The data source uses an NSMutableArray of NSNumber values to achieve a dynamic number of fragments. It's important to note that the number of fragments for each point have to be equal.
Your data model would be set up as shown below:
Objective-C
@interface DataModel : NSObject@property (nonatomic, retain) NSString *name;@property (nonatomic, retain) NSString *title;@property (nonatomic, retain) NSMutableArray *points;@end
@implementation DataModel
- (id)init{ self = [super init]; if (self) { _points = [[NSMutableArray alloc] init]; }
return self;}
@end
Xamarin.iOS C#
public class DataModel : NSObject{ [Export("Name")] public string Name { get; set; } [Export("Points")] public NSMutableArray Points { get; set; } public DataModel() { Points = new NSMutableArray(); }}
The data array is populated with our custom data model objects. You would need to customize this population with his data. The sample created in the attached projects is just generating random data.
- (void)createData{ NSInteger totalPoints = arc4random_uniform(5) + 2; NSInteger totalFragments = arc4random_uniform(5) + 2;
for (int i = 0; i < totalFragments; i++) { DataModel *dataModel = [[DataModel alloc] init]; dataModel.name = [NSString stringWithFormat:@"Fragment %d", i]; dataModel.title = [NSString stringWithFormat:@"Title %d", i];
for (int j = 0; j < totalPoints; j++) { [dataModel.points addObject:[NSNumber numberWithInt:arc4random_uniform(100)]]; }
[_data addObject:dataModel]; }}
void CreateData(){ int totalPoints = _random.Next(10) + 2; int totalFragments = _random.Next(5) + 2; for (int i = 0; i < totalFragments; i++) { DataModel dataModel = new DataModel (); dataModel.Name = String.Format ("Fragment {0}", i); for (int j = 0; j < totalPoints; j++) { dataModel.Points.Add (new NSNumber(_random.Next (100))); } _data.Add (dataModel); }}
Then to load this data model with our custom data source. The first parameter of the constructor passes in the NSArray (ObjC) / NSObject[] (C#) array, next is the fragment item label path of the data model, and finally the path to the NSMutableArray containing values in the data model.
_dataSource = [[DynamicStackedDataSource alloc] initWithData:_data labelPath:@"name" valuesPath:@"points"];
_dataSource = new DynamicStackedDataSource (_data.ToArray(), "Name", "Points");
Additionally, the custom data source for these projects was separated out; for C# the file is DynamicStackedDataSource.cs, and the files DynamicStackedDataSource.h & DynamicStackedDataSource.m for Objective-C. You should be able to easily integrate it into your existing project. If you need a further information, let me know.
How can set legend into this chart?