2

Ok, I can create a plot. But if I want the plot to be given data points one at a time at every 1 second interval, I don't know how to get it to work other than redrawing the plot each time. What methodology do I need to apply to add more data points to the plot subclassing UIView?

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code.
}

- (void)addSample: ??{
    ???
}

thx

1 Answer 1

1

Assuming your plot uses a data set that’s in a mutable collection, like an NSMutableArray, and that your -drawRect: pulls from that collection, then your -addSample: method just needs to add its data points to that collection and then call the view’s -setNeedsDisplay. Your -drawRect: will then get called at the next iteration through the run loop, thereby redrawing the graph.

3
  • I just want to add another point to the plot, not redraw the entire plot. ??
    – Woof
    Jan 9, 2012 at 23:51
  • If you’re only doing it every second or so, redrawing the whole thing will have negligible performance impact. Usually a graph like that needs to be redrawn repeatedly as its data set grows, whether to adjust the scale or to remove older data points—is there some reason that’s not a concern here? Jan 10, 2012 at 0:06
  • I will be setting plot limits right at the beginning. I do not want the plot resizing. There eventually will be up to 10,000 points on the plot.
    – Woof
    Jan 10, 2012 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.