gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:08:08 28 4
gpt4 key购买 nike

将 CategoryDataset 的零值设置为非常大的数字后,我在刷新/重新绘制条形图时遇到问题。

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

这是值为零的图表。

enter image description here

将值设置为一个非常大的数字。

enter image description here

在图表内单击。

enter image description here

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

最佳答案

确保仅在 event dispatch thread 上构建和操作 Swing GUI 对象 。另外,重写getPreferredSize(),如图here ,确定图表的初始大小。在下面的变体中,请注意重复单击 ClearZeroUpdate 按钮如何使图表本身保持不变,同时更新数据集、绘图、范围轴和图例。

image

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

关于java - JFreeChart CategoryDataset - 轴刷新/重绘问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50854965/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com