2
votes

I am little bit new in Java.

Right now I am trying to plot a XYPlot using JFreeChart where the domain axis (X-axis) and range axis (Y-axis) contain the same range. But unfortunately the tick-units are different! For this reason, I tried using NumberAxis to set the range and tick-units on domain axis based on the range and tick-units from range axis. But still the difference remains. I still can't find out why this is happening.

I am attaching the code for this plotting. Also I am attaching the screen shots of the problem and what I really want! Please guide me about what I am doing wrong here ...

    XYSeriesCollection lineChartDataAD = new XYSeriesCollection();

    XYSeries seriesAD = new XYSeries("Real Surface Heights", false, true);

    for (int m = 0; m < pt.delayedy.length - 1; m++) {

        seriesAD.add((double)pt.delayedy[m], (double)pt.delayedy[m+1]);

    }

    lineChartDataAD.addSeries(seriesAD);               

    if (jRadioButton10.isSelected()) { //as it is       checkbox2.getState() == true
        pt.xaxisAD = pt.yvar+" ("+pt.xvar+") ["+pt.yunit+"]";
        pt.yaxisADsupport = String.format("%f", (pt.xspace*pt.delay));
        //pt.yaxisAD = pt.yvar +" (i+"+pt.yaxisADsupport+")";
        pt.yaxisAD = pt.yvar+" ("+pt.xvar+"+d) ["+pt.yunit+"]";
        jLabel44.setText("(Delay (d) = "+pt.yaxisADsupport+" "+pt.xunit+")");
    }
    else if (jRadioButton11.isSelected()) { //as time series  checkbox1.getState() == true
        //pt.yaxisAD = pt.yvar +" (i+"+pt.delay+")";
        pt.xaxisAD = pt.yvar+" (i) ["+pt.yunit+"]";
        pt.yaxisAD = pt.yvar +" (i+d) ["+pt.yunit+"]";
        jLabel44.setText("(Delay (d) = "+pt.delay+")");
    }


    JFreeChart lineChartAD = ChartFactory.createXYLineChart("", pt.xaxisAD, pt.yaxisAD, (XYDataset) lineChartDataAD, PlotOrientation.VERTICAL, false, false, false);

    XYPlot plotAD  = lineChartAD.getXYPlot();

    NumberAxis D = (NumberAxis) plotAD.getDomainAxis();
    NumberAxis R = (NumberAxis) plotAD.getRangeAxis();

    D.setRange(R.getRange());
    D.setTickUnit(R.getTickUnit());                

    XYLineAndShapeRenderer rendererAD = new XYLineAndShapeRenderer();

    rendererAD.setSeriesPaint(0, Color.BLACK);
    double sizeAD = 0;
    double deltaAD = sizeAD / 2.0;
    Shape shapeAD = new Rectangle2D.Double(-deltaAD, -deltaAD, sizeAD, sizeAD);
    rendererAD.setSeriesShape(0, shapeAD);
    rendererAD.setSeriesStroke(0, new BasicStroke(1.0f));

    Font F1AD = new Font ("Times New Roman", Font.PLAIN, 14);

    plotAD.getDomainAxis().setLabelFont(F1AD);
    plotAD.getRangeAxis().setLabelFont(F1AD);

    plotAD.setOutlinePaint(Color.BLACK);
    plotAD.setOutlineStroke(new BasicStroke(0.5f));
    plotAD.setRenderer(rendererAD);
    plotAD.setBackgroundPaint(Color.WHITE);
    plotAD.setRangeGridlinesVisible(true);
    plotAD.setRangeGridlinePaint(Color.GRAY);
    plotAD.setDomainGridlinesVisible(true);
    plotAD.setDomainGridlinePaint(Color.GRAY);

    ChartPanel linePanelAD = new ChartPanel(lineChartAD, true, true, false, false, true); //Properties, save, print, zoom in pop-up menu, and tooltip
    linePanelAD.setMouseZoomable(false);
    panelChartRMA4D.removeAll();
    panelChartRMA4D.add(linePanelAD, BorderLayout.CENTER);
    panelChartRMA4D.setVisible(true);
    panelChartRMA4D.setBorder(new LineBorder (Color.BLACK));
    panelChartRMA4D.validate();

Screen-shot of the Problem || tick-units are different

Screen-shot of desired result || tick-units are same on both axes

1
Please edit your question to include a minimal reproducible example that exhibits the problem you describe; this example may offer some insight.trashgod
@trashgod, I have edited the code that exhibits this problem. Using NumberAxis I've been trying to get the tick units from range axis and set it on the domain axis but couldn't!Ghosh
I can't reproduce the problem that you illustrate. See also this related example.trashgod
@trashgod Thank you for your response. Case is this is exactly one small part of the whole code. The XYdata points are fetched from a text file to plot this one (return map or delay map). This might be the reason for what you can't reproduce it. I have looked into your example. In that case, you have shown how to set tick units based on some inputs! However my problem is slightly different! My domain and range data range are both [-2.09440..., 1.71831...]. I just want to get the tick units from range axis and set it on domain axis so that they appear as same as shown in the second picture.Ghosh
Also check your resize behavior, as suggested here and here.trashgod

1 Answers

0
votes

Finally I've a work-around solution to this problem! Whereas I don't know the exact solution to this problem yet.

Instead of trying with the following lines of code,

NumberAxis D = (NumberAxis) plotAD.getDomainAxis(); 
NumberAxis R = (NumberAxis) plotAD.getRangeAxis(); 
D.setRange(R.getRange()); 
D.setTickUnit(R.getTickUnit());

I tried the following lines of code:

//getting the number axes from the plot

NumberAxis D = (NumberAxis) plotAD.getDomainAxis();
NumberAxis R = (NumberAxis) plotAD.getRangeAxis();

//creating custom tick units based on lower and upper bound

Double DT = (D.getUpperBound() - D.getLowerBound())/5;
DecimalFormat DF = new DecimalFormat("#.#");
DF.setRoundingMode(RoundingMode.FLOOR);
String DTS = DF.format(DT);
DT = Double.parseDouble(DTS);
D.setTickUnit(new NumberTickUnit(DT));
Double RT = (R.getUpperBound() - R.getLowerBound())/5;
String RTS = DF.format(RT);
RT = Double.parseDouble(RTS);
R.setTickUnit(new NumberTickUnit(RT));

And it works !!! See the attached screen-shot below which is what I wanted, same tick-units on both axes ...

Final Result as Expected!

The full code for this sort of plotting is also given below (may be it increases some lines of code but I am happy that it works for any cases until I get the exact solution to this problem):

//Creating XYseries based on an array (i.e., pt.delayedy)

XYSeriesCollection lineChartDataAD = new XYSeriesCollection();
XYSeries seriesAD = new XYSeries("Real Surface Heights", false, true);

for (int m = 0; m < pt.delayedy.length - 1; m++) {            
    seriesAD.add((double)pt.delayedy[m], (double)pt.delayedy[m+1]);
}

lineChartDataAD.addSeries(seriesAD);

//Customizing my axis labels (required for my purpose)

if (jRadioButton10.isSelected()) {
   pt.xaxisAD = pt.yvar+" ("+pt.xvar+") ["+pt.yunit+"]";
   pt.yaxisADsupport = String.format("%f", (pt.xspace*pt.delay));            
   pt.yaxisAD = pt.yvar+" ("+pt.xvar+"+d) ["+pt.yunit+"]";      
}
else if (jRadioButton11.isSelected()) {            
   pt.xaxisAD = pt.yvar+" (i) ["+pt.yunit+"]";
   pt.yaxisAD = pt.yvar +" (i+d) ["+pt.yunit+"]";
   jLabel44.setText("(Delay (d) = "+pt.delay+")");
}

//Creating a JFreechart with my labels and series

JFreeChart lineChartAD = ChartFactory.createXYLineChart("", pt.xaxisAD, pt.yaxisAD, (XYDataset) lineChartDataAD, PlotOrientation.VERTICAL, false, false, false);

//Getting the chart plot

XYPlot plotAD  = lineChartAD.getXYPlot();

//Creating the renderer for the chart

XYLineAndShapeRenderer rendererAD = new XYLineAndShapeRenderer();
rendererAD.setSeriesPaint(0, Color.BLACK);
double sizeAD = 0;
double deltaAD = sizeAD / 2.0;
Shape shapeAD = new Rectangle2D.Double(-deltaAD, -deltaAD, sizeAD, sizeAD);
rendererAD.setSeriesShape(0, shapeAD);
rendererAD.setSeriesStroke(0, new BasicStroke(1.0f));

//Customizing the font of the axes labels

Font F1AD = new Font ("Times New Roman", Font.PLAIN, 14);        
plotAD.getDomainAxis().setLabelFont(F1AD);
plotAD.getRangeAxis().setLabelFont(F1AD);

//The below lines are for exact same x-scaling and y-scaling in plot

NumberAxis D = (NumberAxis) plotAD.getDomainAxis();
NumberAxis R = (NumberAxis) plotAD.getRangeAxis();
D.setAutoRangeIncludesZero(false);
R.setAutoRangeIncludesZero(false);
Double DT = (D.getUpperBound() - D.getLowerBound())/5;
DecimalFormat DF = new DecimalFormat("#.#");
DF.setRoundingMode(RoundingMode.FLOOR);
String DTS = DF.format(DT);
DT = Double.parseDouble(DTS);
D.setTickUnit(new NumberTickUnit(DT));
Double RT = (R.getUpperBound() - R.getLowerBound())/5;
String RTS = DF.format(RT);
RT = Double.parseDouble(RTS);
R.setTickUnit(new NumberTickUnit(RT));

//Plot customization

plotAD.setOutlinePaint(Color.BLACK);
plotAD.setOutlineStroke(new BasicStroke(0.5f));
plotAD.setRenderer(rendererAD);
plotAD.setBackgroundPaint(Color.WHITE);
plotAD.setRangeGridlinesVisible(true);
plotAD.setRangeGridlinePaint(Color.GRAY);
plotAD.setDomainGridlinesVisible(true);
plotAD.setDomainGridlinePaint(Color.GRAY);

//Creating ChartPanel

ChartPanel linePanelAD = new ChartPanel(lineChartAD, true, true, false, false, true);
linePanelAD.setMouseZoomable(false);

//Adding the ChartPanel to the JPanel 

panelChartRMA4D.removeAll();
panelChartRMA4D.add(linePanelAD, BorderLayout.CENTER);
panelChartRMA4D.setVisible(true);
panelChartRMA4D.setBorder(new LineBorder (Color.BLACK));
panelChartRMA4D.validate();