XYBlockRender : impossible to override getItemPaint()

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
NaggingDaivy
Posts: 21
Joined: Mon Jan 09, 2017 2:04 pm
antibot: No, of course not.

XYBlockRender : impossible to override getItemPaint()

Post by NaggingDaivy » Mon May 08, 2017 10:22 am

Hello,
By look it at your FAQ, it is said that it is possible to change the chart color by overriding the "getItemPaint" method. I am trying to do that for creating a custom XYBlockRenderer ; however, overriding the method does nothing.
So, thanks to github, I looked at the code of XYBlockRenderer , and I saw something weird in the "drawItem" method :

Code: Select all

 
 @Override
    public void drawItem(Graphics2D g2, XYItemRendererState state,
            Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
            ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
            int series, int item, CrosshairState crosshairState, int pass) {

        double x = dataset.getXValue(series, item);
        double y = dataset.getYValue(series, item);
        double z = 0.0;
        if (dataset instanceof XYZDataset) {
            z = ((XYZDataset) dataset).getZValue(series, item);
        }

        Paint p = this.paintScale.getPaint(z); =====> By doing that, behaviour cannot be modified by user : the block renderer will always take the corresponding color in the lookup.
Actually, by forcing only to use the paintscale values, the user cannot make a new behavior by ovetriding the "getItemPaint" method because it is never used anywhere in the XYBlockRender class.
According to me, to respect the use of "getItemPaint", XYBlockRenderer should have a method like this :

Code: Select all

 /**
     *  Daivy Merlijs (08-05-2017)
     * XYBlockRenderer should use this function instead of using directly the paintscale, so user can override if he wants and modify the behaviour.
     * @param series    the series index
     * @param item  the item index  
     */
    @Override 
    public Paint getItemPaint(int series, int item)
    {
         
        return this.paintScale.getPaint(((XYZDataset) this.getPlot().getDataset()).getZValue(series, item));
    }
Then "drawItem" should loud like this:

Code: Select all

@Override
    public void drawItem(Graphics2D g2, XYItemRendererState state,
            Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
            ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
            int series, int item, CrosshairState crosshairState, int pass) {

        double x = dataset.getXValue(series, item);
        double y = dataset.getYValue(series, item);
        double z = 0.0;

        double transX = domainAxis.valueToJava2D(x, dataArea,
                plot.getDomainAxisEdge());
        double transY = rangeAxis.valueToJava2D(y, dataArea,
                plot.getRangeAxisEdge());

        if (dataset instanceof XYZDataset) {
            z = ((XYZDataset) dataset).getZValue(series, item);
        }

        Paint p = this.getItemPaint(series, item);  =====> By doing that, behaviour user can modifiy the behaviour by overriding the getItemPaint method.
}
I think that it should be fixed in that way, to keep the "getItemPaint" logic. David, what do you think ?

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

Re: XYBlockRender : impossible to override getItemPaint()

Post by paradoxoff » Mon May 08, 2017 1:28 pm

The approach you suggested will fail in the following cases:
- the XYBlockRenderer instance is used for a dataset with a index > 0. Keep in mind that an XYPlot can have an arbitrary number of datasets, and that the nth dataset will either be renderer by the nth renderer or by the first renderer (with index 0). In your code, you are alwayss using the first XYDataset (with index 0) to retrieve the values.
- If the dataset is not an XYZDataset, a ClassCastException will be thrown, and the programm will crash.
So I would leave the XYBlockRenderer code as it is.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: XYBlockRender : impossible to override getItemPaint()

Post by John Matthews » Mon May 08, 2017 11:28 pm

Some alternatives are examined here.

NaggingDaivy
Posts: 21
Joined: Mon Jan 09, 2017 2:04 pm
antibot: No, of course not.

Re: XYBlockRender : impossible to override getItemPaint()

Post by NaggingDaivy » Tue May 09, 2017 1:17 pm

Thanks to both of you ! The solution of John seems to be the best, then.

Locked