Code: Select all
package javaapplication24;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.*;
import java.io.IOException;
import java.net.URI;
import java.sql.*;
import java.util.Scanner;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import static org.jfree.chart.axis.CategoryLabelPositions.STANDARD;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.jdbc.JDBCCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.odftoolkit.odfdom.doc.OdfTextDocument;
public final class JavaApplication24 extends ApplicationFrame {
Scanner sc = new Scanner(System.in);
String Title = null;
String CategoryPosition = null;
public JavaApplication24(String s) throws IOException, Exception {
super(s);
CategoryDataset categorydataset = readData();
final JFreeChart chart = createChart(categorydataset);
CategoryDataset categorydataset1 = readData1();
final JFreeChart chart1 = createChart1(categorydataset1);
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
final File file = new File("BarChart.jpeg");
ChartUtilities.saveChartAsJPEG(file, chart, 800, 600, info);
final File file1 = new File("BarChart1.jpeg");
ChartUtilities.saveChartAsJPEG(file1, chart1, 800, 600, info);
OdfTextDocument outputDocument;
outputDocument =
OdfTextDocument.newTextDocument();
outputDocument.newParagraph("");
outputDocument.newImage(new URI("BarChart.jpeg"));
outputDocument.newParagraph("");
outputDocument.newImage(new URI("BarChart1.jpeg"));
outputDocument.save("TrendSummaryChart.odt");
}
public JFreeChart createChart(final CategoryDataset categorydataset) {
final JFreeChart chart = ChartFactory.createBarChart(
"Some Title",
"",
"",
categorydataset,
PlotOrientation.VERTICAL,
false,
true,
false
);
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.black);
plot.setRangeGridlinePaint(Color.black);
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setTickLabelFont(new Font("Arial", Font.BOLD, 16));
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.cyan,
0.0f, 0.0f, Color.cyan
);
renderer.setSeriesPaint(0, gp0);
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setTickLabelFont(new Font("Arial", Font.BOLD, 16));
domainAxis.setCategoryLabelPositions
(
STANDARD
);
return chart;
}
public JFreeChart createChart1(final CategoryDataset categorydataset1) {
final JFreeChart chart1 = ChartFactory.createBarChart(
"Some Title",
"",
"",
categorydataset1,
PlotOrientation.VERTICAL,
false,
true,
false
);
chart1.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final CategoryPlot plot = chart1.getCategoryPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.black);
plot.setRangeGridlinePaint(Color.black);
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setTickLabelFont(new Font("Arial", Font.BOLD, 12));
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.cyan,
0.0f, 0.0f, Color.cyan
);
renderer.setSeriesPaint(0, gp0);
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setTickLabelFont(new Font("Arial", Font.BOLD, 12));
domainAxis.setCategoryLabelPositions
(
STANDARD
);
return chart1;
}
private CategoryDataset readData()
{
JDBCCategoryDataset jdbccategorydataset = null;
String s = "sql database link";
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException classnotfoundexception)
{
System.err.print("ClassNotFoundException: ");
System.err.println(classnotfoundexception.getMessage());
}
try
{
Connection connection = DriverManager.getConnection(s, "root", "password");
jdbccategorydataset = new JDBCCategoryDataset(connection);
String s1 = "select month, sum(count)/31 from table where month like 'January' union\n"
+ "select month, sum(count)/28 from table where month like 'February'union\n"
+ "select month, sum(count)/31 from table where month like 'March' union\n"
+ "select month, sum(count)/30 from table where month like 'April' union\n"
+ "select month, sum(count)/31 from table where month like 'May';";
System.out.println("Creating Graph...");
jdbccategorydataset.executeQuery(s1);
connection.close();
}
catch (SQLException sqlexception)
{
System.err.print("SQLException: ");
System.err.println(sqlexception.getMessage());
}
catch (Exception exception)
{
System.err.print("Exception: ");
System.err.println(exception.getMessage());
}
return jdbccategorydataset;
}
private CategoryDataset readData1()
{
JDBCCategoryDataset jdbccategorydataset1 = null;
String s = "sql link";
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException classnotfoundexception)
{
System.err.print("ClassNotFoundException: ");
System.err.println(classnotfoundexception.getMessage());
}
try
{
Connection connection = DriverManager.getConnection(s, "root", "password");
jdbccategorydataset1 = new JDBCCategoryDataset(connection);
String s1 = "select name, sum(count) from table where month='may' group by name order by sum(count) desc;";
System.out.println("Creating Graph...");
jdbccategorydataset1.executeQuery(s1);
connection.close();
}
catch (SQLException sqlexception)
{
System.err.print("SQLException: ");
System.err.println(sqlexception.getMessage());
}
catch (Exception exception)
{
System.err.print("Exception: ");
System.err.println(exception.getMessage());
}
return jdbccategorydataset1;
}
public static void main(String args[]) throws IOException, Exception
{
JavaApplication24 Final = new JavaApplication24("such");
}
}
If no one understands the question, please don't hesitate to ask! I believe I can find a way to be more specific and make help anyone understand my dilemma. I understand everyone is busy, but this isn't a very urgent matter. I appreciate the help whenever it may arrive. Thanks.