PIE Chart Using JFree Chart :
First required database connection, as per connection provide sql query to retrieve data from database.
JDBCPieDataset dataset = new JDBCPieDataset(connection);
try {
dataset.executeQuery("SQL QUERY");
JFreeChart chart = ChartFactory.createPieChart
("Performance - Report", dataset, true, true, false);
chart.setBorderPaint(Color.white);
chart.setBorderStroke(new BasicStroke(10.0f));
chart.setBorderVisible(true);
if (chart != null) {
int width = 500;
int height = 350;
final ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection());
response.setContentType("image/png");
OutputStream out=response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out, chart, width, height,info);
}
}catch (SQLException e) {
e.printStackTrace();
}
Line Chart Query :
try {
Statement dataset1 = connection.createStatement();
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
ResultSet rset = dataset1.executeQuery("SQL QUERY");
int count = 0;
while(rset.next()) {
System.out.println(rset.getString(1)+" : "+rset.getString(2)+" : "+rset.getString(3));
int value = Integer.parseInt(rset.getString(1));
dataset.addValue(value ,rset.getString(2), rset.getString(3) );
count++;
}
JFreeChart barChart = ChartFactory.createLineChart(
" Performance",
" Date",
"Response",
(CategoryDataset) dataset,
PlotOrientation.VERTICAL,
true, true, false);
barChart.setBorderPaint(Color.white);
barChart.setBorderStroke(new BasicStroke(10.0f));
barChart.setBorderVisible(true);
if (barChart != null) {
int width = 600;
int height = 400;
final ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection());
response.setContentType("image/png");
OutputStream out=response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out, barChart, width, height,info);
}
}catch (SQLException e) {
e.printStackTrace();
}
Bar Chart Using Database :
try {
Statement dataset1 = connection.createStatement();
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
ResultSet rset = dataset1.executeQuery("SQL QUERY");
int count = 0;
while(rset.next()) {
dataset.addValue(count ,rset.getString(2), rset.getString(3));
count++;
}
JFreeChart barChart = ChartFactory.createBarChart(
"Performance",
"Date",
"Response",
(CategoryDataset) dataset,
PlotOrientation.VERTICAL,
true, true, false);
barChart.setBorderPaint(Color.white);
barChart.setBorderStroke(new BasicStroke(10.0f));
barChart.setBorderVisible(true);
if (barChart != null) {
int width = 600;
int height = 400;
final ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection());
response.setContentType("image/png");
OutputStream out=response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out, barChart, width, height,info);
}
}catch (SQLException e) {
e.printStackTrace();
}
First required database connection, as per connection provide sql query to retrieve data from database.
JDBCPieDataset dataset = new JDBCPieDataset(connection);
try {
dataset.executeQuery("SQL QUERY");
JFreeChart chart = ChartFactory.createPieChart
("Performance - Report", dataset, true, true, false);
chart.setBorderPaint(Color.white);
chart.setBorderStroke(new BasicStroke(10.0f));
chart.setBorderVisible(true);
if (chart != null) {
int width = 500;
int height = 350;
final ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection());
response.setContentType("image/png");
OutputStream out=response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out, chart, width, height,info);
}
}catch (SQLException e) {
e.printStackTrace();
}
Line Chart Query :
try {
Statement dataset1 = connection.createStatement();
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
ResultSet rset = dataset1.executeQuery("SQL QUERY");
int count = 0;
while(rset.next()) {
System.out.println(rset.getString(1)+" : "+rset.getString(2)+" : "+rset.getString(3));
int value = Integer.parseInt(rset.getString(1));
dataset.addValue(value ,rset.getString(2), rset.getString(3) );
count++;
}
JFreeChart barChart = ChartFactory.createLineChart(
" Performance",
" Date",
"Response",
(CategoryDataset) dataset,
PlotOrientation.VERTICAL,
true, true, false);
barChart.setBorderPaint(Color.white);
barChart.setBorderStroke(new BasicStroke(10.0f));
barChart.setBorderVisible(true);
if (barChart != null) {
int width = 600;
int height = 400;
final ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection());
response.setContentType("image/png");
OutputStream out=response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out, barChart, width, height,info);
}
}catch (SQLException e) {
e.printStackTrace();
}
Bar Chart Using Database :
try {
Statement dataset1 = connection.createStatement();
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
ResultSet rset = dataset1.executeQuery("SQL QUERY");
int count = 0;
while(rset.next()) {
dataset.addValue(count ,rset.getString(2), rset.getString(3));
count++;
}
JFreeChart barChart = ChartFactory.createBarChart(
"Performance",
"Date",
"Response",
(CategoryDataset) dataset,
PlotOrientation.VERTICAL,
true, true, false);
barChart.setBorderPaint(Color.white);
barChart.setBorderStroke(new BasicStroke(10.0f));
barChart.setBorderVisible(true);
if (barChart != null) {
int width = 600;
int height = 400;
final ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection());
response.setContentType("image/png");
OutputStream out=response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out, barChart, width, height,info);
}
}catch (SQLException e) {
e.printStackTrace();
}