问题
I'm having issues with refreshing/repainting a BarChart after setting a zero value of a CategoryDataset to a very large number.
private class Test
extends ApplicationFrame {
private DefaultCategoryDataset set;
public Test(
String newTitle) {
super(newTitle);
set = new DefaultCategoryDataset();
set.addValue(0, "Test", "1");
JFreeChart barChart = ChartFactory.createBarChart(
"Test",
"Category",
"Score",
set,
PlotOrientation.VERTICAL,
true,
true,
false);
JPanel mainPanel = new JPanel(new GridLayout());
ChartPanel chartPanel = new ChartPanel(barChart);
chartPanel.setPreferredSize(new java.awt.Dimension(560, 367));
mainPanel.add(chartPanel);
JButton b = new JButton();
createDataset();
b.addActionListener(e -> {
set.setValue(Integer.MAX_VALUE, "Test", "1");
});
mainPanel.add(b);
setContentPane(mainPanel);
pack();
setVisible(true);
}
Here is the chart with a zero value.
Setting the value to a very large number.
Clicking inside the chart.
How can the chart be properly refreshed? I've tried to repaint the ChartPanel, but it hasn't worked.
回答1:
Be sure to construct and manipulate Swing GUI objects only on the event dispatch thread. In addition, override getPreferredSize()
, as shown here, to establish the chart's initial size. In the variation below, note how repeated clicks on the Clear, Zero and Update buttons leave the chart itself unchanged, while updating the dataset, plot, range axis and legend.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
/**
* @see https://stackoverflow.com/q/50854965/230513
*/
public class Test {
private final DefaultCategoryDataset set = new DefaultCategoryDataset();
private int i;
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
set.addValue(0, "Test", "1");
JFreeChart barChart = ChartFactory.createBarChart(
"Test", "Category", "Score", set,
PlotOrientation.VERTICAL, true, true, false);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(new ChartPanel(barChart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(567, 345);
}
});
JPanel p = new JPanel();
p.add(new JButton(new AbstractAction("Clear") {
@Override
public void actionPerformed(ActionEvent e) {
set.clear();
i = 0;
}
}));
p.add(new JButton(new AbstractAction("Zero") {
@Override
public void actionPerformed(ActionEvent e) {
set.setValue(0, "Test", "1");
i = 0;
}
}));
p.add(new JButton(new AbstractAction("Update") {
@Override
public void actionPerformed(ActionEvent e) {
set.setValue(i += 10, "Test", "1");
}
}));
mainPanel.add(p, BorderLayout.SOUTH);
f.add(mainPanel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}
来源:https://stackoverflow.com/questions/50854965/jfreechart-categorydataset-axis-refresh-repaint-problems