2013-12-23 31 views
0

一些背景信息:我創建了一個系統來註冊丟失,找到並返回的行李。這3個變量的數量需要在LineChart之內,以便從一段時間內得出行李丟失量等信息。JFreeChart沒有更新

現在的問題: 當我創建一個LineChart並將其添加到ChartPanelLineChart代表的數據集。我聽說在編輯/更新數據集時,圖表也會自動更新。那麼我有一個更新按鈕旁邊的圖表,必須點擊更新圖表。獨立於已存在的圖表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; 

} 

的線型圖類,他的構造

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

ChartController

首先構造

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

這裏的按鈕的ActionListener的PARAMS。

//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圖表與編輯後的方法創建(這是我的線型圖類的一部分,站在我下面的構造函數):

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

解決 它不得不這樣做與單擊提交按鈕時創建的另一個數據集。所以我已經修復了娛樂功能,現在它正在工作。不是一個圖表問題,而只是我模型中的一個問題。無論如何感謝您的幫助!

回答

1

ChartPanel實現一個事件偵聽器,在需要時提示它到repaint()本身; JPanel沒有。例如,請參閱chartChanged()ChartChangeListener的實現。在調試時,請查找ChartPanel的一個實例,該實例會遮擋另一個或錯誤使用JPanel

Addednum 是否框架還需要一個ApplicationFrame或者可以將其在JFrame工作?

兩者都可以接受,如JFrameexampleApplicationFrameexample所示;動態更新。請注意,應該在event dispatch thread上構建和操作Swing GUI對象只有

+0

框架是否也需要我'ChartFrame'或它可以在'JFrame'中工作?順便說一句,我認爲這與我如何創建圖表有關。我會在一秒鐘內用更多的代碼更新我的問題。 – Tom

+1

'JFrame'正常工作,因爲[示例](http://stackoverflow.com/a/5522583/230513);而不是添加更多的代碼片段,請編輯您的問題以包含顯示問題的[sscce](http://sscce.org/);這樣做的行爲通常有助於調試;另請參閱[*初始線程*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)。 – trashgod

+0

帖子已更新,請參閱**編輯!**下的額外信息。你的幫助已經被讚賞! – Tom