0

我在 RCP 插件的视图中创建条形图。图表中要显示的数据来自 sqlite3 数据库文件。我编写的代码根据作为输入提供的日期将条形图显示到视图上,我们可以使用日历选择该日期。sqlite3 连接数据库文件并根据日期查询并获取条形图数据。Jfreechart 用于创建条形图,ChartComposite 将其显示在视图上。

问题是当我一个接一个地选择多个日期并要求显示时,所有图表都显示在同一个视图上,我不需要。输出必须如下,当我第二次选择日期时,必须删除当前图表并显示新图表。我试图在代码中添加 dispose() 但它不起作用。

以下是我的相同代码,

公共类 BarChartDisplay 扩展 ViewPart {

Text searchfield = null;
String path = SelectDataBase.path;
public static int error=0;
public static int info=0;
public static int critical=0;
public static int warning=0;



public BarChartDisplay() {
    // TODO Auto-generated constructor stub
}

@Override
public void createPartControl(Composite parent) {
    // TODO Auto-generated method stub

    //Composite A:
    final Composite mainComposite = new Composite(parent, SWT.NONE);
    GridData mainLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
    mainLayoutData.horizontalSpan = 1;
    mainComposite.setLayoutData(mainLayoutData);
    GridLayout outerLayout = new GridLayout();
    outerLayout.marginTop = 30;
    outerLayout.marginLeft = 20;
    outerLayout.marginRight = 20;
    mainComposite.setLayout(outerLayout);

    //Composite B:
    final Composite selectComposite = new Composite(mainComposite, SWT.NONE);
    GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
    layoutData.horizontalSpan = 1;
    selectComposite.setLayoutData(layoutData);
    GridLayout layout = new GridLayout(4, false);
    selectComposite.setLayout(layout);

    //Composite C:
    final Composite barchartComposite = new Composite(mainComposite, SWT.NONE);
    GridData layoutData2 = new GridData(SWT.FILL, SWT.FILL, true, true);
    layoutData2.horizontalSpan = 1;
    barchartComposite.setLayoutData(layoutData2);
    GridLayout layout2 = new GridLayout(1, false);
    barchartComposite.setLayout(layout2); 


    final CalendarCombo ccombo = new CalendarCombo(selectComposite, SWT.READ_ONLY | SWT.FLAT);
    GridData layoutDataCal = new GridData(150, 40);

    ccombo.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    ccombo.showCalendar();
    ccombo.setLayoutData(layoutDataCal);


    org.eclipse.swt.widgets.Button button = new org.eclipse.swt.widgets.Button(selectComposite, SWT.PUSH);
    button.setText("Go");



button.addListener(SWT.Selection, new Listener() {
    public void handleEvent(Event e) {
      switch (e.type) {
      case SWT.Selection:

        error = 0;
        info = 0;
        warning = 0;
        critical = 0;

        DB db = new DB();
        Connection conn = null;
        conn = db.ConnTable(path);
        Statement statement;
        try {
            statement = conn.createStatement();
            String query = null;
            String textfielddata = ccombo.getDateAsString();

            System.out.println(textfielddata);

            query = "select priority from log where creation_date = '"+ textfielddata +"'";


            System.out.println(query);
            ResultSet rs = statement.executeQuery(query);

            while (rs.next()) {

                int  prioritydata = rs.getInt("priority");

                if (prioritydata == 1)
                    error++;
                else if (prioritydata == 2)
                    info++;
                else if (prioritydata == 3)
                    warning++;
                else if (prioritydata == 4)
                    critical++;
            }


        } catch (SQLException er) {
            // TODO Auto-generated catch block
            er.printStackTrace();
        }

        System.out.println("after query");          


      final CategoryDataset dataset = createDataset();
      final JFreeChart chart = createChart(dataset);

      ChartComposite frame = new ChartComposite(barchartComposite,SWT.NONE,chart,true);       

      frame.setLayoutData(new GridData(GridData.FILL_BOTH));
      frame.pack();
      frame.setVisible(true);
      frame.setChart(chart);
      frame.forceRedraw();
      barchartComposite.getLayout();

      break;
      }
    }
  });


}

 /**
 * Returns a sample dataset.
 * 
 * @return The dataset.
 */
private CategoryDataset createDataset() {

    // row keys...
    final String series1 = "First";


    // column keys...
    final String category1 = "error";
    final String category2 = "info";
    final String category3 = "warning";
    final String category4 = "critical";


    // create the dataset...
    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    dataset.addValue(10, series1, category1);
    dataset.addValue(5, series1, category2);
    dataset.addValue(6, series1, category3);
    dataset.addValue(9, series1, category4);


    return dataset;

}

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

    // create the chart...
    final JFreeChart chart = ChartFactory.createBarChart(
        "Priority BarChart",         // chart title
        "priority",               // domain axis label
        "Value",                  // range axis label
        dataset,                  // data
        PlotOrientation.VERTICAL, // orientation
        true,                     // include legend
        true,                     // tooltips?
        false                     // URLs?
    );

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

    // set the background color for the chart...
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);

    // set the range axis to display integers only...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // disable bar outlines...
    final BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);

    // set up gradient paints for series...
    final GradientPaint gp0 = new GradientPaint(
        0.0f, 0.0f, Color.blue, 
        0.0f, 0.0f, Color.lightGray
    );

    renderer.setSeriesPaint(0, gp0);


    final CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(
        CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
    );
    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;

}   

@Override
public void setFocus() {
    // TODO Auto-generated method stub

}

}

4

1 回答 1

1

You don't need to dispose the chart, just recreate the dataset with the new selected date, and use chart.getPlot().setDataset(new_dataset) to refresh the chart with the new data.

于 2012-11-08T11:20:10.583 回答