Stymied trying to draw a simple box on a chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jt_swanson
Posts: 18
Joined: Sat Jun 02, 2012 6:12 pm
antibot: No, of course not.

Stymied trying to draw a simple box on a chart

Post by jt_swanson » Tue Jun 05, 2012 11:40 pm

It seemed from the various posts I read that it should not be hard to draw a box around a data point, but I have been unable to get this to work at all.

Step 1. I can determine when I click on the datapoint

Code: Select all

    class DiagnosticsMouseListener implements ChartMouseListener
    {
       public void chartMouseClicked(ChartMouseEvent event)
       {
          ChartEntity entity = event.getEntity();
          if (entity == null)
             return;
          if (! (entity instanceof XYItemEntity) )
             return;
   
// Get entity details
          IntervalXYDataset dataset = (IntervalXYDataset)(((XYItemEntity)entity).getDataset());
          int seriesIndex           = ((XYItemEntity)entity).getSeriesIndex();
          int item                  = ((XYItemEntity)entity).getItem();

          XYIntervalSeries series   = ((XYIntervalSeriesCollection)dataset).getSeries(seriesIndex);
          XYIntervalDataItem xyItem =  (XYIntervalDataItem)(series.getDataItem(item));

// I can get two different coordinate sets-
          System.out.println( "X: " + xyItem.getX() );
          System.out.println( "Y: " + xyItem.().getYValue() );
          System.out.println( "X: " + event.getTrigger().getX() );
          System.out.println( "Y: " + event.getTrigger().getY() );
       }

Using this listener I can happily select points and get either type of coordinates. I then tried adding the following to my listener, assuming one or the other of those coordinate sets should be appropriate:

Code: Select all

//  None of this works
          XYShapeAnnotation anno = new XYShapeAnnotation(
                                          new Rectangle2D.Float(                                           
                                              (float)event.getTrigger().getX(),
                                              (float)event.getTrigger().getY(),
                                               10.0f, 10.0f ) );
          myplot.addAnnotation(anno);
          double myx = xyItem.getX();
          XYShapeAnnotation anna = new XYShapeAnnotation(
                                          new Rectangle2D.Float(                                           
                                              (float)myx,
                                              (float)xyItem.getYValue(),
                                              10.0f, 10.0f ) );
          myplot.addAnnotation(anna);
while there are lots of snippets of code in the demos and a few examples here. None have added to my understanding of how things are supposed to work and I have become frustrated trying to adapt examples to my situation in the hope they might work without my understanding them.

Could someone do me a favor and explain to me how one could actually put an outline box around a data point?

jt_swanson
Posts: 18
Joined: Sat Jun 02, 2012 6:12 pm
antibot: No, of course not.

Re: Stymied trying to draw a simple box on a chart

Post by jt_swanson » Wed Jun 06, 2012 1:27 am

I seem to have made some progress through trial and error.

1. xyItem.getX() and xyItem.getYValue() are the coordinates I want to use (oddly xyItem.getY() and xyitem.getXValue() fail)

2. I found another method that uses Point2D p = mypanel.translateScreenToJava2D(new Point(mouseX, mouseY) ); ... This command gives a null pointer exception when I try to run it. So no point in going that route. It works in one of the demos, but not in my chart type.

3. myplot.addAnnotation(anno); compiles but does nothing.
myrenderer.addAnnotation(anno); works, but myrenderer.clearAnnotations(); fails to compile and myplot.clearAnnotations() does nothing.

4. Once I place an annotation over a point, clicking on the point no longer generates mouse events. Which is really problematic, since I want a second click to remove the annotation.

If anyone could help me understand what I am seeing by trial and error, I would appreciate the enlightenment.

jt_swanson
Posts: 18
Joined: Sat Jun 02, 2012 6:12 pm
antibot: No, of course not.

Re: Stymied trying to draw a simple box on a chart

Post by jt_swanson » Wed Jun 06, 2012 1:36 am

4a. myrenderer.addAnnotation(myanna, Layer.BACKGROUND);

allows me to click on the datapoint, so I can turn the annotation on and off.

Locked