Listening for zoom reset event in JFreeChart

牧云@^-^@ 提交于 2019-12-20 03:47:12

问题


How can I listen to JFreeChart's zoom reset event?


回答1:


I did it using this:

ChartPanel DCP=new ChartPanel(DailyChart){
    @Override
    public void restoreAutoBounds(){
        super.restoreAutoDomainBounds();
        super.restoreAutoRangeBounds();

        XYPlot plot=(XYPlot)getChart().getPlot();

        Calendar Cal=Calendar.getInstance();
        String dayName=Cal.getDisplayName(Calendar.DAY_OF_WEEK,Calendar.SHORT,new Locale("en", "us")).toLowerCase();
        String tmp[]=((String)Configurations.getWeeklyWorkingSchedule().get(dayName).get("start")).split(":");
        Cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tmp[0]));
        Cal.set(Calendar.MINUTE, Integer.parseInt(tmp[1]));
        Cal.set(Calendar.SECOND, 0);
        long start=Cal.getTimeInMillis();
        tmp=((String)Configurations.getWeeklyWorkingSchedule().get(dayName).get("end")).split(":");
        Cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tmp[0]));
        Cal.set(Calendar.MINUTE, Integer.parseInt(tmp[1]));
        Cal.set(Calendar.SECOND, 0);
        long end=Cal.getTimeInMillis();

        plot.getDomainAxis().setAutoRange(false);
        plot.getDomainAxis().setRange(start,end);
    }
};
DCP.restoreAutoBounds();

Thank You all.




回答2:


I'll just add up on @trashgod suggestion, in case you want to disable zoom reset on a specific axis: create an overriden ChartPanel where you either "null" restoreAutoDomainBounds() or restoreAutoRangeBounds(), as shown below.

That can be useful when you control the viewing area from different components than the chart itself (in my case: the X axis is set by the program but the user can freely zoom in/out the Y axis).

ChartPanel cp = new ChartPanel(null) {
    @Override public void restoreAutoDomainBounds() {
        // Empty body: do not reset X zoom
    }
};


来源:https://stackoverflow.com/questions/8712648/listening-for-zoom-reset-event-in-jfreechart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!