java实现报表,怎么设计,求源代码
发布网友
发布时间:2022-04-22 05:34
我来回答
共1个回答
热心网友
时间:2023-07-31 06:55
你可以参考一下饼图:希望你能看得懂:代码如下:
package charts;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
public class PieChart
{
/**
* 构造报表的结果集
* @return
*/
private static PieDataset createDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("居民", new Double(0.25));
dataset.setValue("农业", new Double(0.14));
dataset.setValue("建筑业", new Double(0.03));
dataset.setValue("商业", new Double(0.15));
dataset.setValue("工业", new Double(0.30));
dataset.setValue("其他", new Double(0.13));
return dataset;
}
/**
* 根据结果集构造JFreechart报表对象
* @param xyDataSet
* @return
*/
private static JFreeChart createChart(PieDataset pieDataSet)
{
JFreeChart jfreechart = ChartFactory.createRingChart(
"短期人民币贷款结构", //报表标题
pieDataSet, //数据集
false, //是否显示图例
true, //是否有工具条提示
false //是否有链接
);
jfreechart.addSubtitle(new TextTitle("------《2008年银行业行业报告》", new Font(
"Dialog", Font.ITALIC, 10)));
RingPlot plot = (RingPlot) jfreechart.getPlot();
plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0} ({2} )"));
plot.setSectionDepth(0.50);
plot.setCircular(false);
plot.setLabelGap(0.02);
return jfreechart;
}
/**
* 生成饼图的图片,返回图片文件的名称
*
* @param session
* @param pw
* @return
*/
public static String generatePieChart(HttpSession session, PrintWriter pw)
{
String filename = null;
PieDataset pieDataset = createDataset();
JFreeChart chart = createChart(pieDataset);
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
try
{
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info,
session);
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
ChartUtilities.writeImageMap(pw, filename, info, false);
}
catch (IOException e)
{
e.printStackTrace();
}
pw.flush();
return filename;
}
}