0

I' m using the ChartPanel class from JFreeChart and want to know the width of the plot's data area.
The returned value from the method chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea().getWidth() is 0.0d. I'm using the JFreeChart version 1.5.0.
My suspicion was a thread-problem, which means that the plotting isn't finished, when I call the .getWidth()-method. So I tried to use ChartChange events, but the result is always a width of 0.0 pixels.

Here is a short example class I created to Show the Problem:
The println in the main() gives "xAxisLengthInPixels = 0.0":

package jFreeChartTest;

import java.awt.BorderLayout;
import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.event.ChartChangeEvent;
import org.jfree.chart.event.ChartChangeListener;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Panel_Chart extends JFrame implements ChartChangeListener {
    private ChartPanel chartPanel = null;   
    private int width  = 1024;
    private int height = 820;

    public Panel_Chart() {
        this.setTitle("XY Plot");
        setBounds(0, 0, width, height); 
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries testXYSeries = new XYSeries("Test-Series", true);
        testXYSeries.add(4,10);
        testXYSeries.add(5,20);
        testXYSeries.add(6,30);
        xySeriesCollection.addSeries(testXYSeries);

        boolean includeLegend = true;
        boolean useTooltips = true;
        boolean useUrls = true;

        JFreeChart chart = ChartFactory.createScatterPlot(//createXYLineChart(
    "",
    "X-Axis",
    "Y-Axis",
    xySeriesCollection,
    PlotOrientation.VERTICAL,
    includeLegend,
    useTooltips,
    useUrls);

        this.chartPanel = new ChartPanel(chart);        
        getContentPane().add(chartPanel, BorderLayout.PAGE_START);  
    }

    public Double getXAxisLengthInPixels(){
        return chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea().getWidth();
    }

    @Override
    public void chartChanged(ChartChangeEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("ChartChangeEvent:" + arg0);
    }

    public static void main(String[] args){
        Panel_Chart panelChart = new Panel_Chart();
        panelChart.setVisible(true);
        System.out.println("xAxisLengthInPixels = " + panelChart.getXAxisLengthInPixels());
    }
}
Gsk
  • 2,929
  • 5
  • 22
  • 29
cabfdb
  • 1
  • More on this topic may be found [here](https://stackoverflow.com/a/50867973/230513). – trashgod Jul 10 '18 at 16:15
  • Update: as suggested in another thread, I tried `code` .repaint() .pack() -methods before calling setWidth(). Unfortunately no Change in the Output (still 0.0d) – cabfdb Jul 12 '18 at 12:38
  • What other thread? Have you fixed the event dispatch threading problem? – Catalina Island Jul 13 '18 at 16:45
  • No, I couldn't figure the problem out yet. The mentioned thread about a similar issue is here: [link](https://stackoverflow.com/questions/6999794/java-jframe-getwidth-returns-0) – cabfdb Jul 16 '18 at 06:46

0 Answers0