以下代碼用到了大多數(shù)方法(附有注釋):

private String createBarChart(String[] series,String[] categories,double[][] data,HttpSession session,PrintWriter printWriter)
{
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(series, categories, data);
JFreeChart chart = ChartFactory.createBarChart3D("預(yù)算統(tǒng)計(jì)圖", "預(yù)算科目","實(shí)際執(zhí)行額",
dataset,PlotOrientation.VERTICAL,true,true,false);
chart.setBackgroundPaint(Color.WHITE); //設(shè)定背景色為白色
CategoryPlot categoryPlot = chart.getCategoryPlot(); //獲得 plot:CategoryPlot!!
categoryPlot.setBackgroundPaint(Color.lightGray); //設(shè)定圖表數(shù)據(jù)顯示部分背景色
categoryPlot.setDomainGridlinePaint(Color.white); //橫坐標(biāo)網(wǎng)格線白色
categoryPlot.setDomainGridlinesVisible(true); //可見
categoryPlot.setRangeGridlinePaint(Color.white); //縱坐標(biāo)網(wǎng)格線白色
CategoryAxis domainAxis = categoryPlot.getDomainAxis();
//設(shè)置 categoryAxis 垂直顯示
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.4));
//設(shè)置哼坐標(biāo)軸的標(biāo)題字體,此處是“預(yù)算科目”
domainAxis.setLabelFont(new Font("SansSerif",Font.PLAIN,13));
//設(shè)置距離圖片左端距離,參數(shù)為圖片的百分比
domainAxis.setLowerMargin(0.05);
//設(shè)置距離圖片右端距離
domainAxis.setUpperMargin(0.05);
//設(shè)置橫坐標(biāo)的坐標(biāo)值的字體
domainAxis.setTickLabelFont(new Font("SansSerif",Font.PLAIN,12));
categoryPlot.setDomainAxis(domainAxis);
ValueAxis rangeAxis = categoryPlot.getRangeAxis();
//設(shè)置最高的一個(gè)柱與圖片頂端的距離
rangeAxis.setUpperMargin(0.05);
//設(shè)置最低的一個(gè)柱與圖片底端的距離
rangeAxis.setLowerMargin(0.05);
categoryPlot.setRangeAxis(rangeAxis);
//設(shè)置豎坐標(biāo)標(biāo)簽的旋轉(zhuǎn)角度
rangeAxis.setLabelAngle(0.05);
//設(shè)置豎坐標(biāo)軸的坐標(biāo)值字體
// rangeAxis.setTickLabelFont(new Font("SansSerif",Font.PLAIN,12));
BarRenderer3D renderer=(BarRenderer3D) categoryPlot.getRenderer();
renderer.setBaseOutlinePaint(Color.GREEN);
//設(shè)置 Wall 的顏色
renderer.setWallPaint(Color.PINK);
//設(shè)置每個(gè)柱的顏色
GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue,
0.0F, 0.0F, new Color(0, 0, 64)); //設(shè)定特定顏色
GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green,
0.0F, 0.0F, new Color(0, 64, 0));
renderer.setSeriesPaint(0, gradientpaint);
renderer.setSeriesPaint(1, gradientpaint1);
//設(shè)置柱的 Outline 顏色
renderer.setSeriesOutlinePaint(0, Color.BLACK);
renderer.setSeriesOutlinePaint(1, Color.BLACK);
//設(shè)置每個(gè)category所包含的平行柱的之間距離
renderer.setItemMargin(0.1);
//顯示每個(gè)柱的數(shù)值,并修改該數(shù)值的字體屬性
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
//設(shè)置柱子上數(shù)值的字體
renderer.setItemLabelFont(new Font("SansSerif",Font.PLAIN,13));
renderer.setItemLabelsVisible(true);
//設(shè)置柱子上數(shù)據(jù)的顏色
renderer.setItemLabelPaint(Color.RED);
//設(shè)置bar的最小寬度,以保證能顯示數(shù)值
renderer.setMinimumBarLength(0.02);
//最大寬度
renderer.setMaximumBarWidth(0.07);
//設(shè)置柱子上顯示的數(shù)據(jù)旋轉(zhuǎn)90度,最后一個(gè)參數(shù)為旋轉(zhuǎn)的角度值/3.14
ItemLabelPosition itemLabelPosition= new ItemLabelPosition(
ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT,
TextAnchor.CENTER_RIGHT,-1.57D);
//設(shè)置不能在柱子上正常顯示的那些數(shù)值的顯示方式,將這些數(shù)值顯示在柱子外面
ItemLabelPosition itemLabelPositionFallback=new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT,
TextAnchor.HALF_ASCENT_LEFT,-1.57D);
//設(shè)置正常顯示的柱子label的position
renderer.setPositiveItemLabelPosition(itemLabelPosition);
renderer.setNegativeItemLabelPosition(itemLabelPosition);
//設(shè)置不能正常顯示的柱子label的position
renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
categoryPlot.setRenderer(renderer);
//設(shè)置柱子的透明度
categoryPlot.setForegroundAlpha(0.8f);
//設(shè)置地區(qū)、銷量的顯示位置
// categoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
// categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);

try
{
ServletUtilities.setTempFilePrefix("zcz");
ServletUtilities.setTempOneTimeFilePrefix("zcz-onetime");
filename = ServletUtilities.saveChartAsPNG(chart, 926, 555, info, session);
chartName=filename;
System.out.println("圖片的臨時(shí)保存路徑:"+System.getProperty("java.io.tmpdir"));

} catch (IOException ex)
{
ex.printStackTrace();
}
String myMap=ChartUtilities.getImageMap(filename, info);
printWriter.println(myMap);
return filename;
}
posted on 2007-07-23 10:47
George Gong 閱讀(1353)
評(píng)論(2) 編輯 收藏 所屬分類:
JAVA&&J2EE