Search code examples
javajfreechart

JFreeChart : Zoom-in for domain axis is not working when CategoryAxis is used


I am using jfreechart library to draw a series chart. I have taken values on y-axis, time on x-axis and 3 categories as series. Everything is fine but I'm not able to zoom-in domain axis although it's working fine for range axis. Is this possible?

Following lines of code may help you to find some scenario of my code:

final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setDomainZoomable(true);
chartPanel.setRangeZoomable(true);
this.add(chartPanel, BorderLayout.CENTER);

and

//set plot specifications 
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(new Color(0xffffe0));
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.lightGray);

//CUSTOMIZE DOMAIN AXIS
final CategoryAxis domainAxis = (CategoryAxis) plot.getDomainAxis();

//customize domain label position
domainAxis.setCategoryLabelPositions(
       CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);

Solution

  • It looks like you are using a CategoryPlot which does not support zooing on the domain range

    enter image description here

    To allow zooming on the Domain axis switch over to a XYPlot if you are using the chart factory the code is

    JFreeChart chart = ChartFactory.createXYLineChart(...);
    

    If you are able to cast the plot to an CategoryPlot something has gone wrong. I've checked LineChartDemo3 and this code causes an error (java.lang.ClassCastException):

    try {
            final CategoryPlot cplot = (CategoryPlot) chart.getPlot();
        } catch (Exception e) {
            e.printStackTrace();
        }