Minor inconsistency in drawing of tic marks with grid lines

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jahjeremy
Posts: 31
Joined: Tue Sep 25, 2012 2:49 am
antibot: No, of course not.

Minor inconsistency in drawing of tic marks with grid lines

Post by jahjeremy » Tue Feb 26, 2013 10:47 pm

I think I have found a minor bug, or at least an inconsistency, in the way that display of tic marks and grid lines are handled.

Suppose you have the following code to display grid lines and inside tic marks on the same plot:

Code: Select all

package hep.aida.jfree;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Stroke;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;

public class TicAndGrid
{
    public static class ChartTheme extends StandardChartTheme
    {
        public ChartTheme() {
            super("Legacy");
        }
        
        public void apply(JFreeChart chart)
        {
            super.apply(chart);
        }
    }
    
    public static void main(String[] args) 
    {
        ChartFactory.setChartTheme(new ChartTheme());
        
        JFreeChart chart = ChartFactory.createXYBarChart("Test", "X Axis", false, "Y Axis", null, PlotOrientation.HORIZONTAL, false, false, false);
        XYPlot plot = chart.getXYPlot();
        
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.red);
        plot.setRangeGridlinePaint(Color.red);
        Stroke stroke = new BasicStroke(2.0f);
        plot.setDomainGridlineStroke(stroke);
        plot.setRangeGridlineStroke(stroke);
        
        plot.getDomainAxis().setAutoRange(true);
        plot.getRangeAxis().setAutoRange(true);
        
        plot.getDomainAxis().setMinorTickMarksVisible(true);
        plot.getRangeAxis().setMinorTickMarksVisible(true);
        
        plot.getDomainAxis().setTickMarkInsideLength(20.0f);
        plot.getDomainAxis().setTickMarkOutsideLength(0.0f);
        plot.getDomainAxis().setMinorTickMarkInsideLength(10.0f);
        plot.getDomainAxis().setMinorTickMarkOutsideLength(0.0f);
        plot.getDomainAxis().setTickMarkPaint(Color.black);

        plot.getRangeAxis().setTickMarkInsideLength(20.0f);
        plot.getRangeAxis().setTickMarkOutsideLength(0.0f);
        plot.getRangeAxis().setMinorTickMarkInsideLength(8.0f);
        plot.getRangeAxis().setMinorTickMarkOutsideLength(0.0f);
        plot.getRangeAxis().setTickMarkPaint(Color.black);
        
        plot.setBackgroundPaint(Color.white);
               
        JFrame frame = new JFrame();
        frame.setContentPane(new ChartPanel(chart));
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
In the above code, setting a custom Stroke for the grid lines, rather than using the default, results in the grid lines being drawn on top of the tic marks. So the major tic marks end up being partially obscured.

BUT, suppose that you remove these lines from the above code and rerun it:

Code: Select all

plot.setDomainGridlineStroke(stroke);
plot.setRangeGridlineStroke(stroke);
Now it displays "correctly", as the tic marks are displayed on top of the grid lines, using the default behavior with no custom Stroke.

So I was just wondering if this is a bug or intended behavior. I've worked around this in my plots by displaying outside tic marks, because I think this is a cleaner way to go and looks better. But if I did want to use inside tic marks on my plots, this bug means that they can be partially covered up when a custom stroke is set on the grid.

I think to be consistent it should be one way or another, and I think the proper way to go is always displaying tic marks on top of the grid, regardless of whether a custom Stroke object was set for the grid. At least, that behavior would make the most sense to me.

--Jeremy

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

Re: Minor inconsistency in drawing of tic marks with grid li

Post by paradoxoff » Fri Mar 01, 2013 7:03 pm

I can assure you that the rendering order between the grid lines of the chart and the tick marks of the axis is not changed when a custom stroke is applied.
The axis (and the associated tick marks) are drawn first, and the grid lines are always drawn on top of them.
I think the effect you ar seeing is simply coming from a thicker stroke that you have applied.
If you look closely at the chart without the custom stroke, you will see that the grid lines are drawn on top of the tick marks as well, but it might be hard to spot due to the thin grid lines. By default, the thickness of the gridlines is 0.5, which is generally translated to "as thin as possible" by the chosen output device.

jahjeremy
Posts: 31
Joined: Tue Sep 25, 2012 2:49 am
antibot: No, of course not.

Re: Minor inconsistency in drawing of tic marks with grid li

Post by jahjeremy » Mon Mar 04, 2013 8:04 pm

Thanks for the explanation. This makes sense.

I guess my main concern is that drawing the grid lines over the axis lines sometimes looks a bit weird. Is there any way to control this rendering order or is it basically hard-coded? By default, I would actually want the axis lines drawn over the grid lines (I think). I would do it the same for data lines too, e.g. on a bar chart. I would think the axis lines should always take precedence.

Locked