Core-Plot: Plotting only one point in a Scatterplot?
I have a graph with two plots. One plot shows 10 data points and is static. The second plot should only show one data point that is a function of a slider selection.
However, whenever I move the slider to compute the coordinates of the single data point, the plot generates a whole series of points until I stop sliding. I would like to remove this trail of dots and only show the one represented by the stopping position of the slider. Hope this makes sense.
Here is what the graph looks like (erroneously):
Oops, I'm too new to post an image, but I'm sure you get the picture.
Here is a portion of the code in the slider IBAction:
CPTScatterPlot *dotPlot = [[[CPTScatterPlot alloc] init] autorelease];
dotPlot.identifier = @"Blue Plot";
dotPlot.dataSource = self;
dotPlot.dataLineStyle = nil;
[graph addPlot:dotPlot];
NSMutableArray *dotArray = [NSMutableArray arrayWithCapacity:1];
NSNumber *xx = [NSNumber numberWithFloat:[estMonthNumber.text floatValue]];
NSNumber *yy = [NSNumber numberWithFloat:[estMonthYield.text floatValue]];
[dotArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:xx,@"x",yy,@"y", nil]];
CPTMutableLineStyle *dotLineStyle = [CPTMutableLineStyle lineStyle];
dotLineStyle.lineColor = [CPTColor blueColor];
CPTPlotSymbol *yieldSymbol = [CPTPlotSymbol ellipsePlotSymbol];
yieldSymbol.fill = [CPTFill fillWithColor:[CPTColor blueColor]];
yieldSymbol.size = CGSizeMake(10.0, 10.0);
dotPlot.plotSymbol = yieldSymbol;
self.dataForPlot = dotArray;
I've tried to reload the plot with [dotPlot reloadData] and even tried to remove and add back the dotPlot but neither s开发者_StackOverfloweems to work or, perhaps, I am putting the instructions in the wrong place or wrong sequence.
Any advice would be greatly appreciated.
Why are you recreating the scatterplot in the slider action? The only thing you should need to do in that method is update the array that provides the data for the second plot, and call reloadData.
In any case, the reason you're getting the trail is that you keep creating new plots and adding them to the graph. The only code that should be in the slider method is:
NSMutableArray *dotArray = [NSMutableArray arrayWithCapacity:1];
NSNumber *xx = [NSNumber numberWithFloat:[estMonthNumber.text floatValue]];
NSNumber *yy = [NSNumber numberWithFloat:[estMonthYield.text floatValue]];
[dotArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:xx,@"x",yy,@"y", nil]];
self.dataForPlot = dotArray;
[graph reloadData];
I figured I was about a line of code away from a solution. As typically happens, I dreamt the solution. First I removed all the NSMutableArray *dotArray, etc, etc code from the slider method. Second I retained the [graph reloadData] in the slider method as Flyingdiver advised. Third, I modified the datasource methods as follows:
#pragma mark - Plot datasource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [dataForPlot count];
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex: (NSUInteger)index {
NSNumber *num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y")];
// Blue dot gets placed above the red actual yields
if ([(NSString *)plot.identifier isEqualToString:@"Blue Plot"]) {
if (fieldEnum == CPTScatterPlotFieldX) {
num = [NSNumber numberWithFloat:[estMonthNumber.text floatValue]]; }
if (fieldEnum == CPTScatterPlotFieldY) {
num = [NSNumber numberWithFloat:[estMonthYield.text floatValue]]; }
}
return num;
}
Once again, thanks a million to Flyingdiver for the clues that solved my mystery. I learned a lot.
精彩评论