0
votes

I'm using JFreeChart to create a timeseries chart in my application.
I'm setting it's domain axis range manually using:

    ...
    plot.getDomainAxis().setAutoRange(false);
    Calendar c1=Calendar.getInstance();
    c1.set(Calendar.HOUR_OF_DAY, 10);
    c1.set(Calendar.MINUTE, 0);
    Calendar c2=Calendar.getInstance();
    c2.set(Calendar.HOUR_OF_DAY, 18);
    c2.set(Calendar.MINUTE, 0);
    plot.getDomainAxis().setRange(c1.getTimeInMillis(),c2.getTimeInMillis());
    ...

Zooming in to chart and then zooming out (Using mouse on chartplot itself) triggers AutoRange on both axis that makes Domain axis range change to series borders and not my own manual rages.

Example (Look at Domain axis's range):
Before zooming in-out (Correct):
enter image description here

After zooming in-out (Incorrect-is Auto ranged):
enter image description here

How can I make it to zoom out to my manually set range?

Thanks

4
Please provide an sscce that exhibits the problem you describe. - trashgod
hi, it edited it and tried to make it SSCCE. - Ariyan

4 Answers

2
votes

You might try restoreAutoBounds(), shown here, followed by your custom domain setting.

Addendum: The behavior you see is defined in the mouse listener implementation of ChartPanel. You could override chartProgress() and restore your domain axis when the chart is finished drawing and not zoomed.

0
votes

here a solution:

class MyNumberAxis extends org.jfree.chart.axis.NumberAxis
{
    private boolean m_RestoreDefaultAutoRange;

    MyNumberAxis()
    {
        super();
    }

    MyNumberAxis(String label)
    {
        super(label);
    }

    MyNumberAxis(String label, boolean restoreDefaulAutoRange)
    {
        super(label);
        m_RestoreDefaultAutoRange = restoreDefaulAutoRange;
    }

    @Override
    protected void autoAdjustRange()
    {
        if( m_RestoreDefaultAutoRange )
        {
            Plot plot = getPlot();
            if( plot != null && plot instanceof ValueAxisPlot)
            {
                Range r = getDefaultAutoRange();
                setRange(r, false, false);
            }
        }
        else
            super.autoAdjustRange();
    }
}

Create an instance of MyNumberAxis setting the boolean to true and use it in your plot (plot.setRangeAxis() method). If you want to behave like the default NumberAxis, pass false as boolean.

0
votes

Magallo's solution above worked great. I found it even more useful if I added another constructor:

MyNumberAxis(String label, boolean restoreDefaulAutoRange, Range defaultRange) {
  super(label);
  m_RestoreDefaultAutoRange = restoreDefaulAutoRange;
  setDefaultAutoRange(defaultRange);
}
0
votes

I created a custom NumberAxis with a fixed range. Zooming out will auto-zoom to this fixed range.

class FixedRangeNumberAxis extends NumberAxis
{
    private Range range;

    FixedRangeNumberAxis(String label, Range range)
    {
        super(label);
        this.range = range;
    }

    @Override
    protected void autoAdjustRange()
    {
        setRange(range, false, false);
    }
}