简体   繁体   English

在jfreechart中缩小后恢复手动域轴范围

[英]restoring manual domain axis range after zooming out in jfreechart

I'm using JFreeChart to create a timeseries chart in my application. 我正在使用JFreeChart在我的应用程序中创建时间序列图表。
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. 放大图表然后缩小(在图表本身上使用鼠标)会在两个轴上触发AutoRange ,使域轴范围更改为系列边框而不是我自己的手动范围。

Example (Look at Domain axis's range): 示例(查看域轴的范围):
Before zooming in-out (Correct): 在放大之前(正确):
在此输入图像描述

After zooming in-out (Incorrect-is Auto ranged): 放大后输出(不正确 - 是自动范围):
在此输入图像描述

How can I make it to zoom out to my manually set range? 如何缩小到我手动设定的范围?

Thanks 谢谢

You might try restoreAutoBounds() , shown here , followed by your custom domain setting. 您可以尝试restoreAutoBounds()所示, 在这里 ,其次是你的自定义域设置。

Addendum: The behavior you see is defined in the mouse listener implementation of ChartPanel . 附录:您看到的行为是在ChartPanel的鼠标侦听器实现中ChartPanel You could override chartProgress() and restore your domain axis when the chart is finished drawing and not zoomed. 您可以覆盖chartProgress()并在图表完成绘制而不是缩放时恢复域轴。

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). 创建MyNumberAx的实例,将布尔值设置为true,并在绘图中使用它(plot.setRangeAxis()方法)。 If you want to behave like the default NumberAxis, pass false as boolean. 如果你想表现得像默认的NumberAxis,则将false作为boolean传递。

Magallo's solution above worked great. Magallo的解决方案效果很好。 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);
}

I created a custom NumberAxis with a fixed range. 我创建了一个固定范围的自定义NumberAxis。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM