请教在swing中使用JFreeChart

soooooga 2010-01-12 11:22:54
如题。请各位大大讲讲 或者给个实际的例子程序
...全文
58 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hfliyy 2010-01-12
  • 打赏
  • 举报
回复
不用GUI了。
sdjavas 2010-01-12
  • 打赏
  • 举报
回复
/**
* 按退休类别生成饼状图
* @throws FileNotFoundException
*/
public String makePieChart() throws FileNotFoundException {
String sqlString ="select retireclass,count(retireclass) from Retirees where retireclass is not null group by retireclass ";
List<Object[]> list = this.getRetireesService().getBySQL(sqlString);
if(list.size()==0)
{
String path="image/100002.JPG";
String string = this.getRequest().getSession().getServletContext().getRealPath(path);
imageStream = new FileInputStream(string);
this.setInputStream(imageStream);
this.setContentType(contentType);
return SUCCESS;
}
else
{
double[]data = new double[list.size()] ;
String[] keys = new String[list.size()];
for(int i=0;i<list.size();i++)
{
data[i]=Double.parseDouble(list.get(i)[1].toString());
String string = list.get(i)[0].toString();
keys[i]=string;
/*if(string.equals("0"))
{
keys[i]="正常";
}
if(string.equals("1"))
{
keys[i]="病退";
}
if(string.equals("2"))
{
keys[i]="特退";
}*/
}
// String[] keys = { "","正常", "病退","特退" };

this.setInputStream(createValidityComparePimChar(getDataPieSetByUtil(data, keys), "退休类别饼状图","pie2.png", keys));
this.setContentType(contentType);
return SUCCESS;
}
}

// 饼状图 数据集
public PieDataset getDataPieSetByUtil(double[] data,
String[] datadescription) {

if (data != null && datadescription != null) {
if (data.length == datadescription.length) {
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < data.length; i++) {
dataset.setValue(datadescription[i], data[i]);
}
return dataset;
}

}

return null;
}
/**
* 饼状图
*
* @param dataset
* 数据集
* @param chartTitle
* 图标题
* @param charName
* 生成图的名字
* @param pieKeys
* 分饼的名字集
* @return
*/
public InputStream createValidityComparePimChar(PieDataset dataset,
String chartTitle, String charName, String[] pieKeys) {
JFreeChart chart = ChartFactory.createPieChart3D(chartTitle, // chart
// title
dataset,// data
true,// include legend
true, false);

// 使下说明标签字体清晰,去锯齿类似于
// chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);的效果
chart.setTextAntiAlias(false);
// 图片背景色
chart.setBackgroundPaint(Color.white);
// 设置图标题的字体重新设置title
Font font = new Font("隶书", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);

PiePlot3D plot = (PiePlot3D) chart.getPlot();
// 图片中显示百分比:默认方式

// 指定饼图轮廓线的颜色
// plot.setBaseSectionOutlinePaint(Color.BLACK);
// plot.setBaseSectionPaint(Color.BLACK);

// 设置无数据时的信息
plot.setNoDataMessage("无对应的数据,请重新查询。");

// 设置无数据时的信息显示颜色
plot.setNoDataMessagePaint(Color.red);

// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})"));

plot.setLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));

// 指定图片的透明度(0.0-1.0)
plot.setForegroundAlpha(0.65f);
// 指定显示的饼图上圆形(false)还椭圆形(true)
plot.setCircular(false, true);

// 设置第一个 饼块section 的开始位置,默认是12点钟方向
plot.setStartAngle(90);

// // 设置分饼颜色
plot.setSectionPaint(pieKeys[0], new Color(236, 228, 224));
if(pieKeys.length>1)
{
plot.setSectionPaint(pieKeys[1], new Color(144, 233, 144));
}

ByteArrayOutputStream fos_jpg = new ByteArrayOutputStream();
try {
// 高宽的设置影响椭圆饼图的形状
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 230);

imageStream = (InputStream)new ByteArrayInputStream(fos_jpg.toByteArray());
return imageStream;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
System.out.println("create pie-chart.");
} catch (Exception e) {
e.printStackTrace();
}
}

}
啊_sdf12 2010-01-12
  • 打赏
  • 举报
回复
兄弟不才,Sping还不是太了解,不能给你乱说,所以我也关注答案。
maer56 2010-01-12
  • 打赏
  • 举报
回复
下个jfreechart包里面有demo

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧