public void getJFreeChart()
{
response = ServletActionContext.getResponse();
DefaultPieDataset dataSet = new DefaultPieDataset();
if (!jobList.isEmpty())
{ //测试数据
dataSet.setValue("Failed", new Double(0.25));
dataSet.setValue("Executing", new Double(0.25));
dataSet.setValue("Completed", new Double(0.25));
dataSet.setValue("Scheduled", new Double(0.25));
}
JFreeChart freeChar = ChartFactory.createPieChart3D("JOB EXECUTION STATUS",
dataSet,
false,
true,
false);
getPiePlot(freeChar);
getLegendTitle(freeChar);
response.setContentType("image/jpeg");
try
{
ServletOutputStream stream = response.getOutputStream();
ChartUtilities.writeChartAsJPEG(stream, freeChar, 550, 300,null);
stream.flush();
stream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private PiePlot getPiePlot(JFreeChart freeChar)
{
PiePlot3D piePlot = (PiePlot3D)freeChar.getPlot();
piePlot.setOutlineVisible(false);
piePlot.setNoDataMessage("No data");
piePlot.setIgnoreNullValues(true);
piePlot.setIgnoreZeroValues(true);
piePlot.setStartAngle(90);
piePlot.setLabelGap(0.02D);
piePlot.setForegroundAlpha(0.7f);
piePlot.setBackgroundPaint(Color.WHITE);
piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {2}",
NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));
piePlot.setSectionPaint("Scheduled", Color.yellow);
piePlot.setSectionPaint("Failed", Color.red);
piePlot.setSectionPaint("Completed", Color.green);
piePlot.setSectionPaint("Executing", Color.blue);
return piePlot;
}
private LegendTitle getLegendTitle(JFreeChart freeChar)
{
LegendTitle legendTitle = new LegendTitle(freeChar.getPlot());
BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
BlockContainer blockcontainer1 = legendTitle.getItemContainer();
blockcontainer1.setPadding(2D, 10D, 5D, 2D);
blockcontainer.add(blockcontainer1);
legendTitle.setWrapper(blockcontainer);
legendTitle.setPosition(RectangleEdge.RIGHT);
legendTitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
freeChar.addSubtitle(legendTitle);
return legendTitle;
}
