same space length between ticks in axis

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
PhilDeFer
Posts: 6
Joined: Sat Mar 07, 2009 1:12 am

same space length between ticks in axis

Post by PhilDeFer » Sat Mar 07, 2009 1:23 am

Hi guys, I'm a new user of this great library since last week.

But I'm also having a problem scaling the x-axis and the y-axis so they have the same scale i.e. length between ticks of the 2 axis should be exactly the same.

Here is what I've been able to do so far but that's not that good...

Code: Select all

import java.awt.Dimension;
 
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.CategoryTableXYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
 
public class ScatterPlotDemo extends ApplicationFrame {
 
    private static final long serialVersionUID = 1L;
 
    public ScatterPlotDemo(String title) {
	super(title);
	CategoryTableXYDataset dataset = new CategoryTableXYDataset();
	dataset.add(2, 4, "a");
	dataset.add(11, 2, "b");
	JFreeChart chart = ChartFactory.createScatterPlot("titre", "x", "y", dataset, PlotOrientation.VERTICAL, true, true, false);
	ChartPanel chartPanel = new ChartPanel(chart, false);
	chartPanel.setPreferredSize(new Dimension(500, 270));
	setContentPane(chartPanel);
 
	XYPlot plot = chart.getXYPlot();
 
	NumberAxis xAxe = (NumberAxis) plot.getDomainAxis();
	xAxe.setLabel("xXx");
	xAxe.setTickUnit(new NumberTickUnit(1));
 
	NumberAxis yAxe = (NumberAxis) plot.getRangeAxis();
	yAxe.setLabel("yYy");
	yAxe.setTickUnit(new NumberTickUnit(1));
    }
 
    public static void main(String[] args) {
	ScatterPlotDemo demo = new ScatterPlotDemo("demo");
	demo.pack();
	demo.setVisible(true);
    }
}
PS : sry for my rly bad english ^^

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

Re: same space length between ticks in axis

Post by paradoxoff » Sun Mar 08, 2009 8:29 am

There is no "built-in" mechanism to ensure that both axes are of the same length in Java2D space, i.e. that the data area is square.
You could try the approach described here:
Patch
Last edited by paradoxoff on Sun Oct 04, 2009 4:56 pm, edited 1 time in total.

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

Re: same space length between ticks in axis

Post by paradoxoff » Sun Mar 08, 2009 8:31 am

double post, deleted

PhilDeFer
Posts: 6
Joined: Sat Mar 07, 2009 1:12 am

Re: same space length between ticks in axis

Post by PhilDeFer » Mon Mar 09, 2009 2:39 pm

Hi, thank you for your answer.

I just launched FixedDataAreaSizeDemo.java after patching JFC witch FixedDataAndPlotArea_corr.diff.

The point is that I dont really see any square on the screen aside the red dots. Maybe this is just about setting a fixed size to the plot window using

Code: Select all

plot.setFixedDataAreaSize(new Size2D(400,600));
Here is the console output :
Plot area java.awt.geom.Rectangle2D$Double[x=8.0,y=30.140625,w=663.0,h=649.375]
Data area java.awt.geom.Rectangle2D$Double[x=67.8984375,y=34.140625,w=599.1015625,h=599.9921875]
Adding axis
Adding text title
Plot area java.awt.geom.Rectangle2D$Double[x=8.0,y=30.140625,w=713.71875,h=649.375]
Data area java.awt.geom.Rectangle2D$Double[x=67.8984375,y=34.140625,w=599.7265625,h=599.9921875]
Adding 2nd text title
Plot area java.awt.geom.Rectangle2D$Double[x=8.0,y=30.140625,w=713.71875,h=649.25]
Data area java.awt.geom.Rectangle2D$Double[x=67.8984375,y=34.140625,w=599.7265625,h=599.8671875]
Adding 3rd text title
Plot area java.awt.geom.Rectangle2D$Double[x=70.375,y=30.140625,w=713.34375,h=649.25]
Data area java.awt.geom.Rectangle2D$Double[x=130.2734375,y=34.140625,w=599.3515625,h=599.8671875]
I think I misexplained what I wanted... Here is the squares I want to get (gray lines are ticks, black ones are x and y axis) :

Image

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

Re: same space length between ticks in axis

Post by paradoxoff » Mon Mar 09, 2009 8:17 pm

There is a bug in the sample program included in the patch.
The ChartPanel does not respect the preferred size of the chart. In order to achieve that, as sammen has pointed out, you have to call

Code: Select all

chart.setUseFixedChartAreaSize(true);
Maybe I have too many flags in the patch, but with this additional flag the use of the preferredChartArea can be disabled both by the JFreeChart and the "context provider" (e. g. the ChartPanel).
The patch will give you a data area of the desired size. If you make sure that you get the same number of tick units for both the domain and range axis you should get a chart with a data area that is divided into squares by the gridlines.

PhilDeFer
Posts: 6
Joined: Sat Mar 07, 2009 1:12 am

Re: same space length between ticks in axis

Post by PhilDeFer » Tue Mar 10, 2009 11:44 am

I've just seen your last demo update. What your program does is much more clearer now. This is not exactly solving my problem by may help a lot since I got no better solution. Great work and thank you for your help.

dallimor
Posts: 2
Joined: Sat Jun 06, 2009 8:37 am

Re: same space length between ticks in axis

Post by dallimor » Sat Jun 06, 2009 8:47 am

I've got this working for a custom renderer by modifying the axes ranges after rendering all items

This is the code I used at the end of the drawItem method. I know my dataset only has one item but you could easily have an if to do this only on the last item

Code: Select all

      
 // Now modify axes ranges to ensure aspect ratio is 1
        double rl = rangeAxis.getRange().getLowerBound();
        double ru = rangeAxis.getRange().getUpperBound();

        double rls = rangeAxis.valueToJava2D(rl, dataArea,
            plot.getRangeAxisEdge());
        double rus = rangeAxis.valueToJava2D(ru, dataArea,
            plot.getRangeAxisEdge());

        double rangeratio = Math.abs((rus - rls) / (ru - rl));

        double dl = domainAxis.getRange().getLowerBound();
        double du = domainAxis.getRange().getUpperBound();

        double dls = domainAxis.valueToJava2D(dl, dataArea,
            plot.getDomainAxisEdge());
        double dus = domainAxis.valueToJava2D(du, dataArea,
            plot.getDomainAxisEdge());
        double domainratio = Math.abs((dus - dls) / (du - dl));

        // precision limit to ensure we don't keep rendering
        double eps = 1.01;
        if (rangeratio / domainratio > eps) {
            rangeAxis.setAutoRange(false);
            double totalrange = Math.abs(ru - rl) * rangeratio / domainratio;
            double l = (ru - rl) / 2 - totalrange / 2;
            double u = (ru - rl) / 2 + totalrange / 2;
            rangeAxis.setRange(new Range(l, u));
        } else if (domainratio / rangeratio > eps) {
            domainAxis.setAutoRange(false);
            double totalrange = Math.abs(du - dl) * domainratio / rangeratio;
            double l = (du - dl) / 2 - totalrange / 2;
            double u = (du - dl) / 2 + totalrange / 2;
            domainAxis.setRange(new Range(l, u));
        }

dbaileychess
Posts: 1
Joined: Fri Jul 17, 2009 10:19 pm

Re: same space length between ticks in axis

Post by dbaileychess » Fri Jul 17, 2009 10:22 pm

I had the same problem but got around it with this:

Code: Select all

        NumberAxis nax = new NumberAxis("X");
        NumberAxis nay = new NumberAxis("Y");
        Range xrange = nax.getRange();
        Range yrange = nay.getRange();
        double xlen = Math.round(xrange.getLength());
        double ylen = Math.round(yrange.getLength());
        if (xlen < ylen) 
            nax.setRangeAboutValue(xrange.getCentralValue(), ylen);
         else 
            nay.setRangeAboutValue(yrange.getCentralValue(), xlen); 
You can take out the rounds if you want.

I also set the size of the chartpanel i displayed the chart in to a square like this:

Code: Select all

        ChartPanel cp = new ChartPanel(chart,false);
        cp.setPreferredSize(new Dimension(500,500));

alsahmut
Posts: 8
Joined: Tue Feb 02, 2010 2:39 pm
antibot: No, of course not.

Re: same space length between ticks in axis

Post by alsahmut » Tue Feb 02, 2010 10:29 pm

How can i synchronize a DateAxis and a NumberAxis?

Locked