Importing JFreeChart in Report

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
rvijaiganesh
Posts: 19
Joined: Wed Mar 09, 2011 2:11 pm
antibot: No, of course not.

Importing JFreeChart in Report

Post by rvijaiganesh » Tue Mar 15, 2011 2:36 pm

Hi,

Sorry to ask questions regarding Reports in this forum because i can't able to post the messages in pentaho forum due to some problem.

I have to embedded the chart(Dynamic) in the report .I don't know how to embedded the chart,i have placed the code below...

Red color code contains chart object which display the chart

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot;
import org.pentaho.reporting.engine.classic.core.modules.gui.base.PreviewDialog;

import org.pentaho.reporting.engine.classic.core.Element;
import org.pentaho.reporting.engine.classic.core.ItemBand;
import org.pentaho.reporting.engine.classic.core.MasterReport;
import org.pentaho.reporting.engine.classic.core.ReportFooter;
import org.pentaho.reporting.engine.classic.core.ReportHeader;
import org.pentaho.reporting.engine.classic.core.TableDataFactory;
import org.pentaho.reporting.engine.classic.core.elementfactory.ContentFieldElementFactory;
import org.pentaho.reporting.engine.classic.core.elementfactory.LabelElementFactory;
import org.pentaho.reporting.engine.classic.core.elementfactory.NumberFieldElementFactory;
import org.pentaho.reporting.engine.classic.core.elementfactory.TextFieldElementFactory;
import org.pentaho.reporting.engine.classic.core.function.FormulaExpression;
import org.pentaho.plugin.jfreereport.reportcharts.*;

public class Chapter10SwingApp extends JFrame {

// constructor which displays the simple
// application shell
private static final long serialVersionUID = 1L;
private JDBCConnection jdbcConnection;

public Chapter10SwingApp() {
super("Chapter 10");
//jdbcConnection = new JDBCConnection();
//jdbcConnection.establishJDBCConnection();
// exit the JVM when the window is closed

this.addWindowStateListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});

// display a preview and exit button in the
// main window of the example application

add(new JLabel("Chapter 10 Swing Application"));

JPanel buttonPanel = new JPanel();
JButton previewButton = new JButton("Preview");
JButton exitButton = new JButton("Exit");

buttonPanel.add(previewButton);
buttonPanel.add(exitButton);

add(buttonPanel, BorderLayout.SOUTH);

previewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onPreview();
}
});

exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}

// The onPreview method is called when the preview
// button is pressed

public void onPreview() {
// Load Report and Launch the Preview Dialog

// load report definition
// create a new report object

MasterReport report = new MasterReport();

// add a table data source to the report

/*DefaultTableModel tableModel = new DefaultTableModel(
new Object[][] {
{"Student ID", jdbcConnection.getStudentID()},
{"firstName", jdbcConnection.getFirstName()},
{"lastName", jdbcConnection.getLastName()},
{"DOB", jdbcConnection.getDob()}
},
new String[] {"Name", "Value"}
);*/
DefaultTableModel tableModel = new DefaultTableModel(
new Object[][] {
{"libloader", 114287},
{"libformula", 331839}
},
new String[] {"Name", "Value"}
);
final TableDataFactory dataFactory = new TableDataFactory();
dataFactory.addTable("default", tableModel);
report.setDataFactory(dataFactory);

// add a formula expression to the report

FormulaExpression formula = new FormulaExpression();
formula.setName("Values");
formula.setFormula("=[Value]");
report.addExpression(formula);

// add the report header and footer

ReportHeader reportHeader = new ReportHeader();
ReportFooter reportFooter = new ReportFooter();
report.setReportHeader(reportHeader);
report.setReportFooter(reportFooter);

// add the item band, with a shortcut that
// handles creating the default group

ItemBand itemBand = new ItemBand();
report.setItemBand(itemBand);

// create a label and add it to the header

LabelElementFactory labelFactory = new LabelElementFactory();

labelFactory.setText("Library Information");
labelFactory.setX(1f);
labelFactory.setY(1f);
labelFactory.setMinimumWidth(200f);
labelFactory.setMinimumHeight(20f);
labelFactory.setBold(true);
labelFactory.setFontSize(20);

Element label = labelFactory.createElement();
reportHeader.addElement(label);

// Add a text field for the library name, added to
// the item band

TextFieldElementFactory textFactory = new TextFieldElementFactory();
textFactory.setFieldname("Name");
textFactory.setX(1f);
textFactory.setY(1f);
textFactory.setMinimumWidth(100f);
textFactory.setMinimumHeight(20f);
textFactory.setFontSize(20);
Element nameField = textFactory.createElement();
itemBand.addElement(nameField);


TextFieldElementFactory textFactory1 = new TextFieldElementFactory();
textFactory1.setFieldname("Values");
textFactory1.setX(101f);
textFactory1.setY(1f);
textFactory1.setMinimumWidth(100f);
textFactory1.setMinimumHeight(20f);
textFactory1.setFontSize(20);
Element nameField1 = textFactory1.createElement();
itemBand.addElement(nameField1);


BarChart barChart = new BarChart("Sample");
JFreeChart chart = barChart.createStackedBarChart();

//JFreeChartReportDrawable rep = new JFreeChartReportDrawable(chart,true);
/*final ChartPanel chartPanel = new ChartPanel(rep);
chartPanel.setPreferredSize(new java.awt.Dimension(200, 200));
chartPanel.setMouseZoomable(false);*/


final PreviewDialog preview = new PreviewDialog(report);
preview.addWindowListener(new WindowAdapter() {
public void windowClosing (final WindowEvent event) {
preview.setVisible(false);
}
});
preview.pack();
//preview.setContentPane(chartPanel);
preview.setVisible(true);
}

// the main method is the entry point into our application

public static void main(String args[]) {

// Initialize the Reporting Engine

ClassicEngineBoot.getInstance().start();
Chapter10SwingApp app = new Chapter10SwingApp();
app.pack();
app.setVisible(true);
}




}

Locked