0

The barchart is displayed as small icon on a composite of a view in Eclipse RCP plugin. The chart does not cover the entire composite which should be the actual case. what additional setting needs to be made in code to display the graph on entire composite

Following is the code for displaying the bargraph

      final CategoryDataset dataset = createDataset();
      final JFreeChart chart = createChart(dataset);
      if(flag == false){
          frame.dispose();
      }

      frame = new ChartComposite(barchartComposite,SWT.NONE,chart,true);
      frame.setLayoutData(new GridData(GridData.FILL_BOTH));
      frame.setChart(chart);
      frame.forceRedraw();
      frame.pack();
      frame.setVisible(true);
      flag= false;

The method createDataset() generates the data for the barchart and method createChart(dataset) generates the barchart.

THE COMPLETE SOURCE CODE FOR DISPLAY OF VIEW

public class BarChartDisplay extends 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;

ChartComposite frame;
boolean flag=true;

public BarChartDisplay() {

}

@Override
public void createPartControl(Composite parent) {

    //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(new GridLayout(1, false));

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

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

    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) {
            er.printStackTrace();
        }



      final CategoryDataset dataset = createDataset();
      final JFreeChart chart = createChart(dataset);
      if(flag == false){
          frame.dispose();
      }

      frame = new ChartComposite(barchartComposite,SWT.BORDER,chart,true);
      frame.setLayoutData(new GridData(GridData.FILL_BOTH));
      frame.setChart(chart);
      frame.forceRedraw();
      frame.pack();
      frame.setVisible(true);
      flag= false;
      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(error, series1, category1);
    dataset.addValue(info, series1, category2);
    dataset.addValue(warning, series1, category3);
    dataset.addValue(critical, 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() {

}

}

4

1 回答 1

0

您必须修改名为barchartComposite.

parent.setLayout(new GridLayout(1, false));
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

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

Composite barchartComposite = new Composite(parent, SWT.NONE);
barchartComposite.setLayout(new GridLayout(1, false));
barchartComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

ChartComposite frame = new ChartComposite(barchartComposite, SWT.BORDER,chart,true);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));

您必须确保它barchartcomposite抓住了父复合材料的整个空间。这可以通过GridLayoutGridData来实现。

您可以在此处找到有关所有 SWT 布局的非常有用的教程:

于 2012-11-09T19:39:43.517 回答