title: Core Plot: How to present popover from a bar selected by the user
body: <h3>What I would like to accomplish</h3>
  
  <p>I am using Core Plot (1.1) to draw a bar chart and I would like to present a popover with further details below the bar which has been selected (tapped) by the user.</p>
  
  <h3>Code</h3>
  
  <p>My code looks like this:</p>
  
  <pre><code>- (void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)idx {
  
      NSNumber *yValue = self.values[idx];
      NSLog(@"barWasSelectedAtRecordIndex x: %i, y: %@",idx,yValue);
  
      NSDecimal plotPoint[2];
      NSNumber *plotXvalue = [self numberForPlot:plot
                                           field:CPTScatterPlotFieldX
                                     recordIndex:idx];
      plotPoint[CPTCoordinateX] =
          CPTDecimalFromFloat(plotXvalue.floatValue);
  
      NSNumber *plotYvalue = [self numberForPlot:plot
                                           field:CPTScatterPlotFieldY
                                     recordIndex:idx];
  
      plotPoint[CPTCoordinateY] =
          CPTDecimalFromFloat(plotYvalue.floatValue); 
  
      CPTXYPlotSpace *plotSpace =
          (CPTXYPlotSpace *)graph.defaultPlotSpace;
      CGPoint dataPoint =
          [plotSpace plotAreaViewPointForPlotPoint:plotPoint];
      NSLog(@"datapoint (CGPoint) coordinates tapped: %@",
            NSStringFromCGPoint(dataPoint));
  
      GrowthChartInfoTableViewController *infoViewController =
          [self.storyboard instantiateViewControllerWithIdentifier:
           @"GrowthChartInfo"];
  
      self.popover = [[UIPopoverController alloc]
                      initWithContentViewController:infoViewController];
      self.popover.popoverContentSize = CGSizeMake(320, 300);
      self.popover.delegate = self;
      CGRect popoverAnchor =
              CGRectMake(dataPoint.x + graph.paddingLeft,
                         dataPoint.y - graph.paddingTop + graph.paddingBottom,
                         (CGFloat)1.0f, (CGFloat)1.0f);
  
      [self.popover presentPopoverFromRect:popoverAnchor
                                    inView:self.view
                  permittedArrowDirections:UIPopoverArrowDirectionAny
                                  animated:YES];
  }
  </code></pre>
  
  <h3>My problem</h3>
  
  <p>The y-position of the popover view controller is incorrect, it is different for each bar and for the first bar, which has a negative y-value, it is presented at the upper limit of the bar instead the lower limit and hides the bar. The x-position seems to be correct.</p>
  
  <p><img src="https://i.stack.imgur.com/l83HG.png" alt="enter image description here"></p>
  
  <h3>Please help</h3>
  
  <p>Any ideas on why my code does not work as expected?</p>
  
  <p>Thank you!</p>
  
  <h1>Edit</h1>
  
  <p>Based on Eric's answer I have updated my code as follows:</p>
  
  <pre><code>- (void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)idx {
  
      NSNumber *plotXvalue = [self numberForPlot:plot
                                           field:CPTScatterPlotFieldX
                                     recordIndex:idx];
      NSNumber *plotYvalue = [self numberForPlot:plot
                                           field:CPTScatterPlotFieldY
                                     recordIndex:idx];
  
      CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
  
      CGPoint cgPlotPoint =
          CGPointMake(plotXvalue.floatValue,
                      plotYvalue.floatValue);
      CGPoint cgPlotAreaPoint =
          [graph convertPoint:cgPlotPoint toLayer:graph.plotAreaFrame.plotArea];
  
      NSDecimal plotAreaPoint[2];
      plotAreaPoint[CPTCoordinateX] =
          CPTDecimalFromFloat(cgPlotAreaPoint.x);
      plotAreaPoint[CPTCoordinateY] =
          CPTDecimalFromFloat(cgPlotAreaPoint.y);
  
      CGPoint dataPoint = [plotSpace
  
  
             plotAreaViewPointForPlotPoint:plotAreaPoint];
          NSLog(@"datapoint (CGPoint) coordinates tapped: %@",NSStringFromCGPoint(dataPoint));
  
  GrowthChartInfoTableViewController *infoViewController = [self.storyboard 
     instantiateViewControllerWithIdentifier:@"GrowthChartInfo"];
  
  
  
      self.popover = nil;
          self.popover = [[UIPopoverController alloc]
  
  
        initWithContentViewController:infoViewController];
      self.popover.popoverContentSize = CGSizeMake(250, 200);
      self.popover.delegate = self;
      CGRect popoverAnchor =
          CGRectMake(dataPoint.x + graph.paddingLeft,
                     dataPoint.y - graph.paddingTop + graph.paddingBottom,
                     (CGFloat)1.0f, (CGFloat)1.0f);
  
      [self.popover presentPopoverFromRect:popoverAnchor
          inView:self.view
          permittedArrowDirections:UIPopoverArrowDirectionUp
          animated:YES];
  }
  </code></pre>
  
  <p>However, the results I am getting are more erroneous than before. I believe I must have misunderstand something. Here are some of the results of the NSLog commands:</p>
  
  <pre><code>barWasSelectedAtRecordIndex x: 0, y: -0.03920931548773198848
  datapoint (CGPoint) coordinates tapped: {-6388.88, -67024.8}
  
  barWasSelectedAtRecordIndex x: 2, y: 0.174494288286651392
  datapoint (CGPoint) coordinates tapped: {-6073.38, -66790}
  
  barWasSelectedAtRecordIndex x: 2, y: 0.174494288286651392
  datapoint (CGPoint) coordinates tapped: {-6073.38, -66790}
  
  barWasSelectedAtRecordIndex x: 0, y: -0.03920931548773198848
  datapoint (CGPoint) coordinates tapped: {-6388.88, -67024.8}
  </code></pre>
  
  <p>It seems that the conversion from the bar coordinates to the view coordinates is completely wrong in my code.</p>
  
  <h2>Edit 2</h2>
  
  <p>Thank you again for your code sample, Eric!</p>
  
  <p>I have updated my code and tested it with the following options (y-value = 0: <code>plotPoint[CPTCoordinateY] = CPTDecimalFromFloat(0.0f)</code>, which results in an anchor point for the popover whose y-value seems to be incorrect (please refer to the following screenshot). The anchor point's y-value should be equivalent to the position of <code>0</code> on the y-axis.</p>
  
  <p><img src="https://i.stack.imgur.com/AMFD9.png" alt="enter image description here"></p>
  
  <p>Then I tried to set the y-value of the anchor point to the current y-value of the data. (<code>plotPoint[CPTCoordinateY] = plotYvalue.decimalValue</code>). This results in an anchor point which is close to the y-axis's 0 value instead of being close to the top of the bar. However, the y-value changes a little bit for each company (x-value) indicating that there might be a problem with the shift of the y-values:</p>
  
  <p><img src="https://i.stack.imgur.com/7Pfhb.png" alt="enter image description here"></p>
  
  <p>I also tried the additional conversion which Eric suggested <code>dataPoint = [self.view convertPoint:dataPoint fromView:graph.hostingView]</code>, but this did not change the results. In addition, I tried to add <code>- graph.paddingTop + graph.paddingBottom</code> to the final y-values, without success.</p>
  
  <p>I would appreciate any ideas on what my error might be.</p>
  
  <p>Thank you!</p>