title: Core plot mouseMoved
body: <p>I would like to show (annotate) values in a <a href="https://code.google.com/p/core-plot/source/browse/examples/CorePlotGallery/src/plots/SimpleScatterPlot.m?r=abfc0cb289b890ed0fbbf8ee856ec852d73796a3" rel="nofollow">scatterplot</a> as the mouse is moved/hovering over a symbol or line. I understand this question has been asked before but I can only find answers that require the user to click the plot to show this information. I have tried to implement the mouseMoved method in my plotView delegate:</p>
  
  <pre><code>- (void)mouseMoved:(NSEvent *)theEvent
  {
      NSPoint location = [hostingView convertPoint: [theEvent locationInWindow] fromView: nil];
      CGPoint mouseLocation = NSPointToCGPoint(location);
      CGPoint pointInHostedGraph = [hostingView.layer convertPoint: mouseLocation toLayer:  plotItem.graph.plotAreaFrame.plotArea];
  
      NSUInteger index = [self.plotItem.dataSourceLinePlot indexOfVisiblePointClosestToPlotAreaPoint: pointInHostedGraph];
      NSLog(@"test: %lu",(unsigned long)index);
  </code></pre>
  
  <p>}</p>
  
  <p>Here, plotItem is a subclass of NSObject and defined like PlotItem.h in the <a href="https://code.google.com/p/core-plot/source/browse/examples/CorePlotGallery/src/shared/PlotItem.h?r=5e338f062b6dae3d339e5d286f1011705b705510" rel="nofollow">example</a> and hosting view is an instance of CPTGraphHostingView.  I also add:</p>
  
  <pre><code>    CPTGraphHostingView *hostedlayer=[self.plotItem updateView:hostingView height:hostingView.frame.size.height width:hostingView.frame.size.width];
  
      [self.plotItem renderInView:hostedlayer withTheme:theme animated:YES withData:self.myFlattenedNodes];
  
      hostedlayer.window.acceptsMouseMovedEvents = YES;
      [hostedlayer.window makeFirstResponder:hostingView];
  </code></pre>
  
  <p>But my mouseMoved method does not get called. What am I doing wrong here as I am unable to get mouseMoved to respond to my hovering. do I need to add a NSTrackingArea to hostedLayer ? Suggestions are appreciated. T</p>
  
  <p><strong>Update and solution:</strong>
  I followed Erics' suggestion and subclassed my CPTGraphHostingView where I implemented the following:</p>
  
  <pre><code>- (id)initWithFrame:(NSRect)frame
  {
      self = [super initWithFrame:frame];
      if (self) {
          self.window.acceptsMouseMovedEvents = YES;
          [self.window makeFirstResponder:self];
  
          area = [[NSTrackingArea alloc] initWithRect:self.frame
                                              options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow| NSTrackingActiveAlways)
                                                owner:self userInfo:nil];
  
          [self addTrackingArea:area];
          [self becomeFirstResponder];
      }
      return self;
  }
  
  - (void)updateTrackingAreas {
      [self removeTrackingArea:area];
      area = [[NSTrackingArea alloc] initWithRect:self.frame
                                      options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow| NSTrackingActiveAlways)
                                        owner:self userInfo:nil];
      [self addTrackingArea:area];
  }
  
  - (void)mouseMoved:(NSEvent *)theEvent
  {
      NSPoint location = [self convertPoint: [theEvent locationInWindow] fromView: nil];
      CGPoint mouseLocation = NSPointToCGPoint(location);
      [self setLocationOfMouse:[self.layer convertPoint: mouseLocation toLayer:nil]];
  }
  
  -(void) dealloc{
      [self removeTrackingArea:area];
  }
  </code></pre>
  
  <p>I also defined to properties of the class:</p>
  
  <pre><code>CGPoint locationOfMouse;
  NSTrackingArea *area;
  </code></pre>
  
  <p>In my controller I added an observer for the locationOfMouse property:</p>
  
  <pre><code>    [self.plotItem.graphHostingView addObserver:self
                          forKeyPath:@"locationOfMouse"
                             options:0
                             context:NULL];
  </code></pre>
  
  <p>Which triggered my method that draws the annotation:</p>
  
  <pre><code>-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  {
       if ( [keyPath isEqualToString:@"locationOfMouse"] ) {
          CGPoint location = self.plotItem.graphHostingView.locationOfMouse;
          [self.plotItem mouseMovedOverGraph:location];    
      }
      else {
          [super observeValueForKeyPath:keyPath ofObject:object
                             change:change context:context];
      }
  } 
  </code></pre>