change icons in the MinMaxCategory Renderer?

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.

change icons in the MinMaxCategory Renderer?

Post by jt_swanson » Sun Sep 10, 2017 10:03 pm

I am trying to create a category plot that has data points that are above or below y = 0 and there is a vertical line connecting the point to y=0. I tried using the MinMaxCategoryPlot with some success. I use two rows, one for the data values and one that is all zero. Then 'all' I need to do is turn off the MinIcon when the value is positive and turn off the MaxIcon when the value is negative.

I did this by changing MinMaxCategoryRenderer as follows:

Code: Select all

    class DFBetaCategoryRenderer extends MinMaxCategoryRenderer
   {
      @Override
      public void drawItem( Graphics2D g2, CategoryItemRendererState state,
         Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
         ValueAxis rangeAxis, CategoryDataset dataset, int row, int column,
         int pass )
      {
//
// See if the value is positive or negative for the row that contains the
// values
         Double value = (Double) dataset.getValue(  row, column );
         String name  = (String) dataset.getRowKey( row );
         if ( value != null && "First".equals( name ) )
         {
            System.out.println( "VALUE: " + value );
            System.out.println( "NAME:  " + name  );
            if ( value < 0.0 )
            {
               setMinIcon( defaultMinIcon );
               setMaxIcon( emptyIcon      );
            }
            else if ( value > 0.0 )
            {
               setMinIcon( emptyIcon      );
               setMaxIcon( defaultMaxIcon );
            }
         }
         super.drawItem( g2, state, dataArea, plot, domainAxis, rangeAxis,
                         dataset, row, column, pass );
      }
   }
On the positive side, I get the plot that I want. However, I end up with a constant storm of events because setMinIcon and setMaxIcon do a fireChangeEvent(), which I don't want.

I would just

Code: Select all

   minIcon = defaultMinIcon;
   maxIcon = emptyIcon
except these variables are private.

I suppose I can copy the entire renderer class to make the one change- the drawItem method is full of private variables (this.objectIcon, this.lastCategory, this.min, this.max, this.groupPaint, this.groupStroke, this.plotLines) which has confounded my naive attempt to Override the drawItem() method by copying pretty much the whole method. If I could have done that, I would have simply checked the dataset value and called paintIcon only when appropriate.

Does anyone have a idea how to work around either the fireChangeEvent() or all the private variables?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: change icons in the MinMaxCategory Renderer?

Post by paradoxoff » Mon Sep 11, 2017 4:31 pm

jt_swanson wrote: Does anyone have a idea how to work around either the fireChangeEvent() or all the private variables?
No, not really. You might be better of by writing your own renderer that extends AbstractCategoryItemRenderer and only implements the features that you need. You could use the BarRenderer class as a starting point. Instead of drawing bars, you can draw lines from the baseline to a coordinate calculated from the value in the dataset, and draw a Shape at the end point of the line. To me, this seems a far better approach than copy-pasting an existing renderer and modify it to make it half-useful for a task for which it wasn't designed.

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

Re: change icons in the MinMaxCategory Renderer?

Post by jt_swanson » Mon Sep 11, 2017 5:08 pm

Thanks,

For right now I did the simplest, but not the best approach. I simply copied the MinMaxCategoryRenderer and changed the variables to protected. This allowed me to get the plot I was looking for by extending that renderer and adding eight lines of code to drawItem (pretty much what I showed earlier). I didn't need to make any other changes.

I'm doing this for a group that uses the 'just code it and we'll tell you what we think' philosophy of requirements, so maybe when the look is a little more certain I will write a new renderer, but there's not that much in MinMaxCategoryRenderer, almost everything comes from AbstractCategoryRenderer.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: change icons in the MinMaxCategory Renderer?

Post by paradoxoff » Tue Sep 12, 2017 9:35 pm

Looks like you habe now two new renderer classes (the modified MinMax and the one tha derives from the modified MinMax) instead of one.
That really looks like a dirty hack, and not neccessarily less work that the clean approach (which is writing a new renderer).
Anyway, your choice.

Locked