【问题标题】:JFreeChart not updatingJFreeChart 未更新
【发布时间】:2013-12-23 20:21:05
【问题描述】:

一些背景信息:我正在创建一个系统来登记丢失、找到和退回的行李。这 3 个变量的数量需要在 LineChart 中,以便从一段时间内丢失的行李数量等方面获得一个很好的概览。

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

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

这里有一些代码:

LineChartModel

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

图表控制器

首先是构造函数的参数

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

这里是按钮的动作监听器。

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

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

【问题讨论】:

    标签: java model-view-controller dataset jfreechart


    【解决方案1】:

    ChartPanel 实现了一个事件侦听器,在需要时提示它自己到repaint()JPanel 没有。例如,参见chartChanged()ChartChangeListener 的实现。调试时,请查找 ChartPanel 的一个实例,它会影响另一个实例或 JPanel 的错误使用。

    Addednum 框架是否也需要是ApplicationFrame 还是可以在JFrame 中工作

    任何一个都可以接受,如图这个JFrameexample或者这个ApplicationFrameexample;两者都动态更新。请注意,Swing GUI 对象应event dispatch thread 上构造和操作。

    【讨论】:

    • 框架是否也需要ChartFrame 或者它可以在JFrame 中使用?顺便说一句,我认为这与我创建图表的方式有关。我将在一秒钟内用更多代码更新我的问题。
    • JFrame 工作正常,对于 example;而不是添加更多代码片段,请编辑您的问题以包含一个显示问题的sscce;这样做通常有助于调试;另见Initial Threads
    • 帖子已更新,请参阅编辑!下的额外信息。你的帮助已经很感激了!
    • 没什么明显的;我已经编辑了解决框架问题的答案;查看我关于调试的更新评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多