ChartExample.java
package sos.examples;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import sos.reports.*;
import sos.reports.Element;
import org.jfree.chart.*;
import org.jfree.chart.renderer.*;
import org.jfree.chart.plot.*;
import org.jfree.data.*;
import org.jfree.data.time.*;
public class ChartExample
{
private static final Random random = new Random( 0 );
private static Theme createBodyTheme()
{
StyleContext styleContext = new StyleContext();
Style cellStyle = styleContext.addStyle( "Cell", null );
ReportStyleConstants.setPadding( cellStyle, 12.0f );
ReportStyleConstants.setRenderer( cellStyle, new ChartRenderer() );
styleContext.addStyle( "Cell0,0", cellStyle );
styleContext.addStyle( "Cell0,1", cellStyle );
styleContext.addStyle( "Cell1,0", cellStyle );
styleContext.addStyle( "Cell1,1", cellStyle );
styleContext.addStyle( "Cell2,0", cellStyle );
styleContext.addStyle( "Cell2,1", cellStyle );
Theme bodyTheme = new DefaultTheme( "Some theme", styleContext );
return bodyTheme;
}
private static JFreeChart createCountryChart()
{
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue( "US", 44.3 );
pieDataset.setValue( "France", 12.1 );
pieDataset.setValue( "Japan", 10.0 );
pieDataset.setValue( "UK", 8.4 );
pieDataset.setValue( "Spain", 3.4 );
pieDataset.setValue( "Germany", 8.6 );
pieDataset.setValue( "Australia", 3.2 );
pieDataset.setValue( "China", 5.4 );
pieDataset.setValue( "Other", 1.4 );
JFreeChart countryChart = ChartFactory.createPieChart3D( "Visitors by Country", pieDataset, false, true, true );
countryChart.setBackgroundPaint( Color.white );
return countryChart;
}
private static JFreeChart createHitByFolderChart()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue( 30.0, "", "/" );
dataset.addValue( 8.0, "", "/evaluation/" );
dataset.addValue( 35.0, "", "/products/" );
dataset.addValue( 27.0, "", "/api/" );
dataset.addValue( 4.0, "", "Not Found" );
JFreeChart hitsByFolderChart = ChartFactory.createBarChart( "Hits By Folder", "Folder", "Percentage", dataset, PlotOrientation.HORIZONTAL, false, false, false );
hitsByFolderChart.setBackgroundPaint( Color.white );
CategoryPlot plot = hitsByFolderChart.getCategoryPlot();
BarRenderer renderer = (BarRenderer)plot.getRenderer();
renderer.setDrawBarOutline(false);
renderer.setSeriesPaint(0, new GradientPaint( 0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray ));
return hitsByFolderChart;
}
private static JFreeChart createHitsByHourChart()
{
TimeSeries hitsByHourData = new TimeSeries( "Hits", Hour.class );
Day day = new Day( 1, 1, 2004 );
for( int i = 0; i < 24; i++ )
hitsByHourData.add( new Hour( i, day ), random.nextInt( 20 ));
TimeSeriesCollection hitsByHourDataset = new TimeSeriesCollection( hitsByHourData );
hitsByHourDataset.setDomainIsPointsInTime( false );
JFreeChart hitsByHourChart = ChartFactory.createXYBarChart( "Average Hits by Hour", "Hour", true, "Avg Hits", hitsByHourDataset, PlotOrientation.VERTICAL, false, false, false );
hitsByHourChart.setBackgroundPaint( Color.white );
return hitsByHourChart;
}
private static JFreeChart createHitsPerDayChart()
{
TimeSeries hitsPerDayData = new TimeSeries( "Hits", Day.class );
Calendar calendar = Calendar.getInstance();
calendar.set( 2003, 0, 0 );
for( int i = 0; i < 365; i++ )
{
hitsPerDayData.add( new Day( calendar.getTime() ), random.nextInt( 200 ));
calendar.add( Calendar.DATE, 1 );
}
XYDataset hitsPerDayDataset = new TimeSeriesCollection( hitsPerDayData );
JFreeChart hitsPerDayChart = ChartFactory.createTimeSeriesChart( "Hits Per Day", "Date", "Hits", hitsPerDayDataset, false, false, false );
hitsPerDayChart.setBackgroundPaint( Color.white );
return hitsPerDayChart;
}
private static JFreeChart createHitsPerMonthChart()
{
TimeSeries hitsPerMonthData = new TimeSeries( "Hits", Month.class );
for( int i = 1; i <= 12; i++ )
hitsPerMonthData.add( new Month( i, 2003 ), random.nextInt( 1000 ) + 3000 );
TimeSeriesCollection hitsPerMonthDataset = new TimeSeriesCollection( hitsPerMonthData );
hitsPerMonthDataset.setDomainIsPointsInTime( false );
JFreeChart hitsPerMonthChart = ChartFactory.createXYBarChart( "Hits by Month", "Month", true, "Hits", hitsPerMonthDataset, PlotOrientation.VERTICAL, false, false, false );
hitsPerMonthChart.setBackgroundPaint( Color.white );
XYPlot plot1 = hitsPerMonthChart.getXYPlot();
XYItemRenderer renderer1 = (XYItemRenderer)plot1.getRenderer();
renderer1.setSeriesPaint(0, Color.blue );
return hitsPerMonthChart;
}
public static Theme createMainTheme()
{
TableReportTemplate bodyTemplate = new TableReportTemplate();
Theme bodyTheme = createBodyTheme();
StyleContext mainStyleContext = new StyleContext();
Style titleStyle = mainStyleContext.addStyle( "Cell0,0Object", null );
StyleConstants.setFontSize( titleStyle, 24 );
StyleConstants.setFontFamily( titleStyle, "Arial" );
StyleConstants.setBold( titleStyle, true );
StyleConstants.setAlignment( titleStyle, StyleConstants.ALIGN_CENTER );
StyleConstants.setUnderline( titleStyle, true );
Style bodyStyle = mainStyleContext.addStyle( "Cell1,0Object", null );
ReportStyleConstants.setReportTemplate( bodyStyle, bodyTemplate );
ReportStyleConstants.setTheme( bodyStyle, bodyTheme );
StyleConstants.setFontFamily( bodyStyle, "Arial" );
Theme theme = new DefaultTheme( mainStyleContext );
return theme;
}
private static JFreeChart createReferrerChart()
{
DefaultPieDataset referrerDataset = new DefaultPieDataset();
referrerDataset.setValue( "google.com", 82 );
referrerDataset.setValue( "yahoo.com", 8 );
referrerDataset.setValue( "somesite.com", 7 );
referrerDataset.setValue( "other", 3 );
JFreeChart referrerChart = ChartFactory.createPieChart3D( "Referrers", referrerDataset, false, false, false );
referrerChart.setBackgroundPaint( Color.white );
return referrerChart;
}
public static void main(String[] args)
{
SwingUtilities.invokeLater( new Runnable() {
public void run()
{
showReport();
}
} );
}
private static void showReport()
{
JFreeChart hitsPerMonthChart = createHitsPerMonthChart();
JFreeChart countryChart = createCountryChart();
JFreeChart referrerChart = createReferrerChart();
JFreeChart hitsByHourChart = createHitsByHourChart();
JFreeChart hitsPerDayChart = createHitsPerDayChart();
JFreeChart hitsByFolderChart = createHitByFolderChart();
Object[][] bodyData = new Object[][] {
{ hitsPerDayChart, hitsPerMonthChart },
{ countryChart, hitsByFolderChart },
{ hitsByHourChart, referrerChart }
};
Object[] mainReportData = new Object[] { "2003 Website Statistics", bodyData };
ReportTemplate template = new TableReportTemplate();
Theme mainTheme = createMainTheme();
Report report = template.createReport( mainReportData, mainTheme );
JReportPane reportPane = new JReportPane( report );
JScrollPane scrollPane = new JScrollPane( reportPane );
JFrame frame = new JFrame( "Chart Example by Side of Software" );
frame.getContentPane().add( scrollPane );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 800, 500 );
frame.show();
}
}
class ChartRenderer implements ElementRenderer
{
private ChartPanel chartPanel;
public Component getElementRendererComponent(JReportPane reportPane, Element element)
{
JFreeChart chart = (JFreeChart)element.getObject();
if( chartPanel == null )
chartPanel = new ChartPanel( chart );
else
chartPanel.setChart( chart );
chartPanel.setPreferredSize( new Dimension( 300, 250 ));
return chartPanel;
}
}