java-JFreeChart CategoryDataset-轴刷新/重画问题

将CategoryDataset的零值设置为非常大的值后,刷新/重新绘制BarChart时遇到问题.

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

这是零值图表.

java-JFreeChart CategoryDataset-轴刷新/重画问题

将该值设置为非常大的数字.

java-JFreeChart CategoryDataset-轴刷新/重画问题

在图表内单击.

java-JFreeChart CategoryDataset-轴刷新/重画问题

图表如何正确刷新?我试图重新绘制ChartPanel,但没有成功.

解决方法:

确保仅在event dispatch thread上构造和操作Swing GUI对象.此外,覆盖here所示的getPreferredSize()来建立图表的初始大小.在下面的变体中,请注意在更新数据集,图,范围轴和图例时,重复单击“清除”,“清零”和“更新”按钮如何使图表本身保持不变.

java-JFreeChart CategoryDataset-轴刷新/重画问题

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://*.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);
    }
}
上一篇:java-每500毫秒平滑渲染一次Swing组件


下一篇:java-Jfreechart注释消失