package com.darren.service;
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.category.CategoryDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.xy.XYSeriesCollection;
public class JframeChartService {
public void createJframeChartPicture() {
DataSetService dataSetService = new DataSetService();
CategoryDataset dataset = dataSetService.complicatedDataset();
String title = "水果产量图";
String categoryAxisLabel = "水果";
String valueAxisLabel = "产量";
PlotOrientation orientation = PlotOrientation.VERTICAL;
boolean legend = true;
boolean tooltips = false;
boolean urls = false;
JFreeChart jFreeChart = ChartFactory.createBarChart3D(
title, //图表标题
categoryAxisLabel, //目录轴的显示标签 :横轴
valueAxisLabel, //数值轴的显示标签:纵轴
dataset, //数据集
orientation, //图表方向,水平,垂直
legend, //是否显示图例(对于简单图必须是false)
tooltips, //是否生成工具
urls//是否生成url连接
);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("D:\\workplace\\eclipse\\jframeChart\\WebContent\\images\\jFreeChart.jpg");
ChartUtilities.writeChartAsJPEG(outputStream, jFreeChart, 400, 300);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void createJframeChartPiePicture() {
DataSetService dataSetService = new DataSetService();
PieDataset pieDataset = dataSetService.complicatedPieDataset();
String title = "水果产量图";
boolean legend = true;
boolean tooltips = false;
boolean urls = false;
JFreeChart jFreeChart = ChartFactory.createPieChart3D(
title, //图表标题
pieDataset, //数据集
legend, //是否显示图例
tooltips, //是否生成工具
urls//是否生成url连接
);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("D:\\workplace\\eclipse\\jframeChart\\WebContent\\images\\jFreeChart_pie.jpg");
ChartUtilities.writeChartAsJPEG(outputStream, jFreeChart, 400, 300);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void createJframeChartXyLinePicture() {
DataSetService dataSetService = new DataSetService();
XYSeriesCollection xyLineDataset = dataSetService.prepareXyLineDataset();
String title = "水果产量图";
String categoryAxisLabel = "水果";
String valueAxisLabel = "产量";
PlotOrientation orientation = PlotOrientation.VERTICAL;
boolean legend = true;
boolean tooltips = false;
boolean urls = false;
JFreeChart jFreeChart = ChartFactory.createXYLineChart(
title, //图表标题
categoryAxisLabel, //目录轴的显示标签 :横轴
valueAxisLabel, //数值轴的显示标签:纵轴
xyLineDataset, //数据集
orientation, //图表方向,水平,垂直
legend, //是否显示图例
tooltips, //是否生成工具
urls//是否生成url连接
);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("D:\\workplace\\eclipse\\jframeChart\\WebContent\\images\\jFreeChart_xy_line.jpg");
ChartUtilities.writeChartAsJPEG(outputStream, jFreeChart, 400, 300);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void createJframeChartLinePicture() {
DataSetService dataSetService = new DataSetService();
CategoryDataset lineDataset = dataSetService.prepareLineDataset();
String title = "水果产量图";
String categoryAxisLabel = "水果";
String valueAxisLabel = "产量";
PlotOrientation orientation = PlotOrientation.VERTICAL;
boolean legend = true;
boolean tooltips = false;
boolean urls = false;
JFreeChart jFreeChart = ChartFactory.createLineChart(
title, //图表标题
categoryAxisLabel, //目录轴的显示标签 :横轴
valueAxisLabel, //数值轴的显示标签:纵轴
lineDataset, //数据集
orientation, //图表方向,水平,垂直
legend, //是否显示图例
tooltips, //是否生成工具
urls//是否生成url连接
);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("D:\\workplace\\eclipse\\jframeChart\\WebContent\\images\\jFreeChart_line.jpg");
ChartUtilities.writeChartAsJPEG(outputStream, jFreeChart, 400, 300);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}