java - JFreeChart 未更新

标签 java model-view-controller dataset jfreechart

一些背景信息:我正在创建一个系统来登记丢失、找到和退回的行李。这 3 个变量的数量需要显示在 LineChart 中,以便更好地了解一段时间内丢失了多少行李等。

现在的问题是: 当我创建 LineChart 并将其添加到 ChartPanel 时,LineChart 代表一个数据集。我听说当您编辑/更新数据集时,图表也会自动更新。好吧,我在图表旁边有一个更新按钮,单击该按钮时必须更新图表。独立于现有 JPanel/ChartPanel 中的图表,我还创建了一个代表 ChartFrame 的新框架。

当我单击更新按钮时,会弹出一个新框架,其中包含更新数据集中的最新数据。当我再次单击它时,显然会创建另一个框架,但已经存在的框架也会更新,而 JPanel 中的图表则不会更新,尽管它们使用相同的静态数据集 code> 来自 LineChart 模型。

这里有一些代码:

折线图模型

//At the top of the document the dataset is initialize static
public static DefaultCategoryDataset dataset = null;

/*****************Other code omitted***********************/
/**
 * 
 * Updates an already existing dataset
 * 
 * @param dataset
 * @return dataset
 */
public CategoryDataset updateDataset(DefaultCategoryDataset dataset) {

    this.dataset = dataset; 

    // row keys...
    final String rowLost = "Lost";
    final String rowFound = "Found";
    final String rowReturned = "Returned";


    //Don't pay attention to this. It's setting the value for the dataset from different arrays which I know of the are filled correctly
    int i = 0;
    while (i < 12) {

        dataset.setValue(lost[i], rowLost, type[i]);
        System.out.println("There were " + lost[i]
                + " lost luggages in month: " + type[i]);
        i++;
    }
    for (int j = 0; j < 12; j++) {
        dataset.setValue(found[j], rowFound, type[j]);
        System.out.println("There were " + found[j]
                + " found luggages in month: " + type[j]);
    }
    for (int j = 0; j < 12; j++) {
        dataset.setValue(returned[j], rowReturned, type[j]);
        System.out.println("There were " + returned[j]
                + " returned luggages in month: " + type[j]);
    }
    return dataset;

}

LineChart 类及其构造函数

LineChartModel model = new LineChartModel();
public ChartPanel chartPanel;
/**
 * Creates a new demo.
 *
 * @param title  the frame title.
 */
public LineChart(final String title, LineChartModel m) {
    m = model;
    m.selectRange();
    m.createDataset();
    final JFreeChart chart = createChart(m.dataset);
    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(500, 270));
}

图表 Controller

首先是构造函数的参数

public ChartController(final ManCharts v, final LineChartModel m) {
    view = v;
    model = m;

这里是按钮的 Action 监听器。

    //Select a range by sumbitting the variables
    v.date.btnSubmit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            //selectRange select all the data between 2 dates but isn't important for this problem
            m.selectRange(/*v.date.dateChooserFrom, v.date.dateChooserTo*/);

            //updateDataset updates the dataset from the LineChartModel which is static
            m.updateDataset(m.dataset);

            //The data in the chart should already be updated but here I'm trying to replace the current chart by a new one
            v.chart.chartPanel = new ChartPanel(v.chart.createChart(m.dataset));

            //This is the new chart which does automatically update when the button is pressed
            JFreeChart chart = ChartFactory.createLineChart("Something chart", "Date", "Value", m.dataset);
            ChartFrame frame = new ChartFrame("New line chart", chart);
            frame.setVisible(true);
        }
    });

我真的希望有人能够提供帮助,并且如果没有完整的代码,这个问题也不会太复杂。

如果您需要更多代码或其他内容。就这么说吧。

提前致谢!

编辑! 我认为这与我创建图表的方式有关。在我的应用程序开始时创建的 JPanel 中的图表是使用编辑的方法创建的(这是我的 LineChart 类的一部分,位于我的构造函数下方):

/**
 * Creates a sample chart.
 * 
 * @param dataset  a dataset.
 * 
 * @return The chart.
 */
public JFreeChart createChart(final CategoryDataset dataset) {

    // create the chart...
    final JFreeChart chart = ChartFactory.createLineChart(
        "Luggage",       // chart title
        "Time",                    // domain axis label
        "Value",                   // range axis label
        dataset,                   // data
        PlotOrientation.VERTICAL,  // orientation
        true,                      // include legend
        true,                      // tooltips
        false                      // urls
    );



    chart.setBackgroundPaint(Color.decode("#d6d9df"));

    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.white);

    // customise the range axis...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(true);


    // customise the renderer...
    final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    //renderer.setDrawShapes(true);

    renderer.setSeriesStroke(
        0, new BasicStroke(
            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] {10.0f, 6.0f}, 0.0f
        )
    );
    renderer.setSeriesStroke(
        1, new BasicStroke(
            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] {6.0f, 6.0f}, 0.0f
        )
    );
    renderer.setSeriesStroke(
        2, new BasicStroke(
            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] {2.0f, 6.0f}, 0.0f
        )
    );
    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;
}

已解决 它与单击提交按钮时创建的另一个数据集有关。所以我已经修复了游戏并且它现在可以工作了。并不是真正的图表问题,而只是我的模型中的问题。无论如何,感谢您的帮助!

最佳答案

ChartPanel实现一个事件监听器,在需要时提示其重新绘制(); JPanel 没有。参见ChartChangeListener的实现在 chartChanged() , 例如。调试时,查找遮蔽另一个 ChartPanel 实例的 ChartPanel 实例或错误使用 JPanel

Addednum 框架是否还需要是 ApplicationFrame 或者它可以在 JFrame 中工作吗?

两者都可以接受,如JFrame example所示或这个 ApplicationFrame example ;两者都是动态更新的。请注意,Swing GUI 对象应该event dispatch thread 上构建和操作。 .

关于java - JFreeChart 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20750591/

相关文章:

java - 使用 for 循环和用户输入将元素添加到 LinkedList

java - 正则表达式后顾之忧

php - 我对 Web 开发中的 MVC 理解正确吗?

asp.net-mvc - 这种逻辑的正确位置在哪里?

c# - "LINQ to Entities", "LINQ to SQL"和 "LINQ to Dataset"有什么区别

java - Hibernate - 检索所有表信息 - 列名、索引、长度并填充为表

java - 哪个范围与 vaadin 和 spring 一起使用?

javascript - 在 Angular 中,我们应该在 .controller.js 中还是在 .html 中调用验证函数?

scala - 循环遍历 Map Spark Scala

entity-framework-4 - 在 Entity Framework 4.0 中,我们如何在一次调用中从数据库中获取多个记录集并将所有数据传递给 View