Hello again.
I have beaten my head bloody against this problem. The fix above helps me get the first plot right, but I need to be able to update the dataset when new calculation data is available.
Below is a sample program that illustrates what happens. Please excuse the messy code, it was difficult to distill the vital parts into a working program. When the plot is cleared (by giving it an empty dataset) the domain axis labels disappear. When the new data is injected they do not reappear.
If any of you wizards would try to run the problem and give me a solution (a hack, anything) I would be eternally greatful.
Code: Select all
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.StandardTickUnitSource;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.DefaultXYDataset;
public class TestAxisProblem {
private static XYPlot plot;
public static void main( String[] args ) {
// The user has created a plot
EventQueue.invokeLater( new Runnable() { public void run() { launchGUI(); } });
try { Thread.sleep( 2000 ); } catch( Exception e ) {}
// The user clicks the "calculate" button and data is cleared from memory.
EventQueue.invokeLater( new Runnable() { public void run() { clearPlot(); } } );
try { Thread.sleep( 2000 ); } catch( Exception e ) {}
// When the calculation is finished, the plot is updated
EventQueue.invokeLater( new Runnable() { public void run() { updatePlotAgain(); } } );
}
private static void fixAxis() {
plot.getRangeAxis().setAutoRangeMinimumSize(1e-50);
plot.getRangeAxis().setStandardTickUnits(new StandardTickUnitSource());
plot.getDomainAxis().setAutoRangeMinimumSize(1e-50);
plot.getDomainAxis().setStandardTickUnits(new StandardTickUnitSource());
}
private static void launchGUI() {
DefaultXYDataset firstDataset = new DefaultXYDataset();
firstDataset.addSeries("Test", new double[][]{{0,5e-30},{100,2e-30}});
JFreeChart chart = ChartFactory.createXYLineChart("Fjuff", "x", "y", firstDataset, PlotOrientation.HORIZONTAL, false, false, false);
plot = (XYPlot) chart.getPlot();
fixAxis();
ChartPanel panel = new ChartPanel(chart);
JFrame frame = new JFrame();
frame.setContentPane(panel);
frame.pack();
frame.setVisible( true );
}
private static void clearPlot() {
DefaultXYDataset emptyDataset = new DefaultXYDataset();
plot.setDataset(emptyDataset);
fixAxis();
}
private static void updatePlotAgain() {
DefaultXYDataset secondDataset = new DefaultXYDataset();
secondDataset.addSeries("Test", new double[][]{{0,1e-19},{100,1e-20}});
plot.setDataset(secondDataset);
fixAxis();
}
}
Regards
Erik