Core Plot, Core Data and bindings

396 views
Skip to first unread message

Hans Salomonsson

unread,
Jan 14, 2011, 7:24:31 PM1/14/11
to coreplot-discuss
Hi,

I am using core data to store my model data that i want to plot with
core plot. I am aiming at having the graph updating itself
automatically (using bindings) when a model value is changed or added.
Currently I implement -numberForPlot by:
1. Executing a fetch of the data (this is done every time -
numberForPlot is called).
2. Setting x and y data:
if (fieldEnum == CPScatterPlotFieldX) {
return [[data objectAtIndex:index] valueForKey:@"xData"];
}
else if (fieldEnum == CPScatterPlotFieldY) {
return [[data objectAtIndex:index] valueForKey:@"yData"];
}
I can then update the graph using reloadData. Is this the way to go or
is there a better solution? I studied the bindings in the CPTestApp,
but that was for a dictionary and not managedObjects. I tried doing it
analogously for core data, but got SIGABRT error. Here is what I
wrote:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Data"
inManagedObjectContext:moc]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"xData" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray
arrayWithObject:sortDescriptor]];
NSError *error;
NSArray *data = [NSArray arrayWithArray:[moc
executeFetchRequest:fetchRequest error:&error]];

[plot bind:CPScatterPlotBindingXValues toObject:data
withKeyPath:@"xData" options:nil];
[plot bind:CPScatterPlotBindingYValues toObject:data
withKeyPath:@"yData" options:nil];

This might very well not be the proper way of doing this with bindings
and core data. I would really like to know, if possible, how that is
done.

Thanks,
Hans

Drew McCormack

unread,
Jan 15, 2011, 4:19:28 AM1/15/11
to coreplot...@googlegroups.com
I recommend not using any fetch requests. Instead, make an NSArrayController, set the entity of it to the CoreData entity that has your data, bind the managed object context of the array controller, and then bind the graph to the data via the array controller. Set the sort descriptor of the array controller to ensure the data is correctly sorted.

This assumes you have a data entity with a value for x, and a value for y. Eg.

MyDataEntity
number x
number y

This is all demonstrated in CPTestApp for Mac.

Drew

> --
> You received this message because you are subscribed to the Google Groups "coreplot-discuss" group.
> To post to this group, send email to coreplot...@googlegroups.com.
> To unsubscribe from this group, send email to coreplot-discu...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/coreplot-discuss?hl=en.
>

Hans Salomonsson

unread,
Jan 17, 2011, 9:53:58 AM1/17/11
to coreplot-discuss
Thanks Drew! Now it works.

If anyone is having the same problem I will exemplify Drew's answer.

1. Create a Core Data model with the entity Data and attributes xData
and yData.
2. Create a NSArrayController in IB. Set its mode to Entity and Entity
name to "Data" (or whatever your entity is called). Bind its
managedObjectContext to the same as your model's (where your data
lives, if you created a default data model application, it is in the
app delegate).
3. In whatever class you are setting up your graph add an IBOutlet
your NSArrayController. You need to reference to it when you do your
binding and setting your sortDescriptor. I did this:
//Setting up a ScatterPlot
CPScatterPlot *plot = [[[CPScatterPlot alloc] init]
autorelease];
plot.dataLineStyle.lineWidth = 1.0f;
plot.dataLineStyle.lineColor = [CPColor blueColor];
[graph addPlot:plot];
//Binding x and y values
[plot bind:CPScatterPlotBindingXValues toObject:[self
arrayController] withKeyPath:@"arrangedObjects.xData" options:nil]; //
Binding
[plot bind:CPScatterPlotBindingYValues toObject:[self
arrayController] withKeyPath:@"arrangedObjects.yData" options:nil]; //
Binding
//Set the sorting
[arrayController setSortDescriptors:[NSArray arrayWithObject:
[NSSortDescriptor sortDescriptorWithKey:@"xData" ascending:YES]]];

Hans
Reply all
Reply to author
Forward
0 new messages