JFreeChart CategoryDataset - Axis refresh/repaint problems

后端 未结 1 425
时光取名叫无心
时光取名叫无心 2021-01-25 15:08

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 A         


        
1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 15:34

    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);
        }
    }
    

    0 讨论(0)
提交回复
热议问题