Backing Bean/CSV
Query CSV file for JFreeChart Data
Xpages Link
System Requirements:
Download Domino Designer 8.5.2 Environment (DDE)
http://www.ibm.com/developerworks/downloads/ls/dominodesigner/
Download CSV file JDBC driver
http://sourceforge.net/projects/csvjdbc/
Download JFreeChart
http://www.jfree.org/jfreechart/download.html
Download Jcommon jar
http://www.jfree.org/jcommon/
Introduction:
Query CSV file into Java Application, add results to JfreeChart Bar chart.
Disclaimer:
Information contained in the following is presented as is. This tutorial assumes you have basic programming knowledge. All tutorials are based on an Eclipse/Eclipse-based software. Should you need to familiarize yourself with a certain Eclipse environment, prior to continuing this tutorial, please stop now and see our Tutorials page...
Write Java class to Build Chart
At this point we assume Domino Designer 8.5.2 is downloaded/installed, CSVJdbc, JFreeChart, and JCommon have been downloaded, all jar files are added to your CLASSPATH. You have also built a Java class read the CSV file and load its data to JFreeChart Copy and paste below code to your environment, areas of interest have been highlighted for your convenience.
ReadCSVIntoJFreeChartBean.java
[CODE]
/**
*
* @AppName: Docu.nsf
* Program: ReadCSVIntoJFreeChartVBean.java
* Created: 2012.04.23.8.16.AM
* Build Results into Bar Chart from CSV data 'UserNewLineOutboundInformation.csv'
*/
package com.dokoll.solutions.inc.cvs.trials;
/**
* @author Dököll Solutions, Inc.
* @version 2012.04.23.8.16.AM
*
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.jdbc.JDBCCategoryDataset;
//...
//Start of Program
public class ReadCSVIntoJFreeChartBean
{
// Set up static path/file variables
private static final String FILE_PATH = "C:\\temp\\JFREE_DATA\\";
private static final String OUTFILE_NAME = "JFreeChartCSVExplorerData.jpg";
//...
//run chart - Xpages button
public void doRunJFreeBarChartFromCSV() throws IOException {
String query = "SELECT * FROM UserNewLineOutboundInformation";
JDBCCategoryDataset dataset = null;
try {
dataset = new JDBCCategoryDataset("jdbc:relique:csv:C:\\temp\\CSV_DATA\\","org.relique.jdbc.csv.CsvDriver", "root", "root");
} catch (ClassNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
dataset.executeQuery(query);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JFreeChart chart = ChartFactory.createBarChart3D(
"JFreeChart CSV Trend", "SiteName", "SiteNumber", dataset,
PlotOrientation.VERTICAL, true, true, false);
try {
ChartUtilities.saveChartAsJPEG(new File(FILE_PATH+OUTFILE_NAME), chart, 400,300);
System.out.println("JFreeChart created from CSV file.");
} catch (IOException e) {
System.out.println("Problem in creating chart.");
}
// Load Chart to Explorer
// 2012.04.23.8.25.AM
try {
//Grab the file created from location
//Load to Explorer window for user
FileOutputStream fos = new FileOutputStream(FILE_PATH+OUTFILE_NAME);
ChartUtilities.writeChartAsJPEG(fos, 1, chart, 950, 600);
fos.flush();
fos.close();
Runtime run = Runtime.getRuntime();
run.exec("explorer.exe " + FILE_PATH+OUTFILE_NAME);
} catch (Exception e) {
}
}
}
Build Xpage file
Full code added below, jump ahead if necessary, areas of interest have been highlighted for your convenience.
xpcsvforcharts.xsp
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:link escape="true" text="Fetch CSV JFreeChart Bar Chart Data" id="link3" title="Fetch CSV JFreeChart Bar Chart Data">
<xp:eventHandler event="onclick" submit="true" refreshMode="complete" immediate="false" save="true" id="eventHandler3">
<xp:this.action><[CDATA[#{javascript:ReadCSVIntoJFreeChartBean.doRunJFreeBarChartFromCSV()}]]></xp:this.action>
</xp:eventHandler>
</xp:link>
</xp:view>
Conclusion:
You can now Query CSV files from Java, data is then used by your application in the making of JFreeChart items.
TIP: You must reference the all necessary jar files in your CLASSPATH, and attach them to your project via build path. Be certain to add all jar files to your /lib/ext folder within Notes. You will of course need to reference ReadCSVIntoJFreeChartBean.java in your faces-config.xml file.
Questions, comments, please post a brief message on our Contact form on the main site. Thank you for coming...