轉(zhuǎn)載:http://hi.baidu.com/ahli/blog/item/3aec09f78df33626730eec11.html
關(guān)鍵字: 報表 最近公司使用jfreechart來生成統(tǒng)計圖,使用了折線圖,餅圖等,因為用的1.0.8a版的jfreechart,與1.0.0版本的地方有很多不同,而且圖片生成清晰度比老版的高,如下是我做的demo.
1.所需包
(1) jfreechart-1.0.8a.jar
(2) jcommon-1.0.12.jar
2.運行環(huán)境
JDK 1.5
3.源代碼
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;


/** *//**
* 實際取色的時候一定要16位的,這樣比較準(zhǔn)確
*
* @author new
*/
public class CreateChartServiceImpl


{
private static final String CHART_PATH = "E:/test/";

public static void main(String[] args)


{
// TODO Auto-generated method stub
CreateChartServiceImpl pm = new CreateChartServiceImpl();
// 生成餅狀圖
pm.makePieChart();
// 生成單組柱狀圖
pm.makeBarChart();
// 生成多組柱狀圖
pm.makeBarGroupChart();
// 生成堆積柱狀圖
pm.makeStackedBarChart();
// 生成折線圖
pm.makeLineAndShapeChart();
}


/** *//**
* 生成折線圖
*/
public void makeLineAndShapeChart()


{
double[][] data = new double[][]


{


{ 672, 766, 223, 540, 126 },


{ 325, 521, 210, 340, 106 },


{ 332, 256, 523, 240, 526 } };
String[] rowKeys =


{ "蘋果", "梨子", "葡萄" };
String[] columnKeys =


{ "北京", "上海", "廣州", "成都", "深圳" };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createTimeXYChar("折線圖", "x軸", "y軸", dataset, "lineAndShap.png");
}


/** *//**
* 生成分組的柱狀圖
*/
public void makeBarGroupChart()


{
double[][] data = new double[][]


{


{ 672, 766, 223, 540, 126 },


{ 325, 521, 210, 340, 106 },


{ 332, 256, 523, 240, 526 } };
String[] rowKeys =


{ "蘋果", "梨子", "葡萄" };
String[] columnKeys =


{ "北京", "上海", "廣州", "成都", "深圳" };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createBarChart(dataset, "x坐標(biāo)", "y坐標(biāo)", "柱狀圖", "barGroup.png");
}


/** *//**
* 生成柱狀圖
*/
public void makeBarChart()


{
double[][] data = new double[][]


{


{ 672, 766, 223, 540, 126 } };
String[] rowKeys =


{ "蘋果" };
String[] columnKeys =


{ "北京", "上海", "廣州", "成都", "深圳" };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createBarChart(dataset, "x坐標(biāo)", "y坐標(biāo)", "柱狀圖", "bar.png");
}


/** *//**
* 生成堆棧柱狀圖
*/
public void makeStackedBarChart()


{
double[][] data = new double[][]


{


{ 0.21, 0.66, 0.23, 0.40, 0.26 },


{ 0.25, 0.21, 0.10, 0.40, 0.16 } };
String[] rowKeys =


{ "蘋果", "梨子" };
String[] columnKeys =


{ "北京", "上海", "廣州", "成都", "深圳" };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createStackedBarChart(dataset, "x坐標(biāo)", "y坐標(biāo)", "柱狀圖", "stsckedBar.png");
}


/** *//**
* 生成餅狀圖
*/
public void makePieChart()


{
double[] data =


{ 9, 91 };
String[] keys =


{ "失敗率", "成功率" };

createValidityComparePimChar(getDataPieSetByUtil(data, keys), "餅狀圖",
"pie2.png", keys);
}

// 柱狀圖,折線圖 數(shù)據(jù)集
public CategoryDataset getBarData(double[][] data, String[] rowKeys,
String[] columnKeys)


{
return DatasetUtilities
.createCategoryDataset(rowKeys, columnKeys, data);

}

// 餅狀圖 數(shù)據(jù)集
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 數(shù)據(jù)集
* @param xName x軸的說明(如種類,時間等)
* @param yName y軸的說明(如速度,時間等)
* @param chartTitle 圖標(biāo)題
* @param charName 生成圖片的名字
* @return
*/
public String createBarChart(CategoryDataset dataset, String xName,
String yName, String chartTitle, String charName)


{
JFreeChart chart = ChartFactory.createBarChart(chartTitle, // 圖表標(biāo)題
xName, // 目錄軸的顯示標(biāo)簽
yName, // 數(shù)值軸的顯示標(biāo)簽
dataset, // 數(shù)據(jù)集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例(對于簡單的柱狀圖必須是false)
false, // 是否生成工具
false // 是否生成URL鏈接
);
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);

/**//*
* VALUE_TEXT_ANTIALIAS_OFF表示將文字的抗鋸齒關(guān)閉,
* 使用的關(guān)閉抗鋸齒后,字體盡量選擇12到14號的宋體字,這樣文字最清晰好看
*/
// chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
chart.setTextAntiAlias(false);
chart.setBackgroundPaint(Color.white);
// create plot
CategoryPlot plot = chart.getCategoryPlot();
// 設(shè)置橫虛線可見
plot.setRangeGridlinesVisible(true);
// 虛線色彩
plot.setRangeGridlinePaint(Color.gray);

// 數(shù)據(jù)軸精度
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// vn.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#0.00");
vn.setNumberFormatOverride(df); // 數(shù)據(jù)軸數(shù)據(jù)標(biāo)簽的顯示格式
// x軸設(shè)置
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(labelFont);// 軸標(biāo)題
domainAxis.setTickLabelFont(labelFont);// 軸數(shù)值

// Lable(Math.PI/3.0)度傾斜
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions
// .createUpRotationLabelPositions(Math.PI / 3.0));

domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 橫軸上的 Lable 是否完整顯示

// 設(shè)置距離圖片左端距離
domainAxis.setLowerMargin(0.1);
// 設(shè)置距離圖片右端距離
domainAxis.setUpperMargin(0.1);
// 設(shè)置 columnKey 是否間隔顯示
// domainAxis.setSkipCategoryLabelsToFit(true);

plot.setDomainAxis(domainAxis);
// 設(shè)置柱圖背景色(注意,系統(tǒng)取色的時候要使用16位的模式來查看顏色編碼,這樣比較準(zhǔn)確)
plot.setBackgroundPaint(new Color(255, 255, 204));

// y軸設(shè)置
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
// 設(shè)置最高的一個 Item 與圖片頂端的距離
rangeAxis.setUpperMargin(0.15);
// 設(shè)置最低的一個 Item 與圖片底端的距離
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);

BarRenderer renderer = new BarRenderer();
// 設(shè)置柱子寬度
renderer.setMaximumBarWidth(0.05);
// 設(shè)置柱子高度
renderer.setMinimumBarLength(0.2);
// 設(shè)置柱子邊框顏色
renderer.setBaseOutlinePaint(Color.BLACK);
// 設(shè)置柱子邊框可見
renderer.setDrawBarOutline(true);

// // 設(shè)置柱的顏色
renderer.setSeriesPaint(0, new Color(204, 255, 255));
renderer.setSeriesPaint(1, new Color(153, 204, 255));
renderer.setSeriesPaint(2, new Color(51, 204, 204));

// 設(shè)置每個地區(qū)所包含的平行柱的之間距離
renderer.setItemMargin(0.0);

// 顯示每個柱的數(shù)值,并修改該數(shù)值的字體屬性
renderer.setIncludeBaseInRange(true);
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);

plot.setRenderer(renderer);
// 設(shè)置柱的透明度
plot.setForegroundAlpha(1.0f);

FileOutputStream fos_jpg = null;
try


{
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 500, true, 10);
return chartName;
}
catch (Exception e)


{
e.printStackTrace();
return null;
}
finally


{
try


{
fos_jpg.close();
}
catch (Exception e)


{
e.printStackTrace();
}
}
}


/** *//**
* 橫向圖
*
* @param dataset 數(shù)據(jù)集
* @param xName x軸的說明(如種類,時間等)
* @param yName y軸的說明(如速度,時間等)
* @param chartTitle 圖標(biāo)題
* @param charName 生成圖片的名字
* @return
*/
public String createHorizontalBarChart(CategoryDataset dataset,
String xName, String yName, String chartTitle, String charName)


{
JFreeChart chart = ChartFactory.createBarChart(chartTitle, // 圖表標(biāo)題
xName, // 目錄軸的顯示標(biāo)簽
yName, // 數(shù)值軸的顯示標(biāo)簽
dataset, // 數(shù)據(jù)集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例(對于簡單的柱狀圖必須是false)
false, // 是否生成工具
false // 是否生成URL鏈接
);

CategoryPlot plot = chart.getCategoryPlot();
// 數(shù)據(jù)軸精度
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
//設(shè)置刻度必須從0開始
// vn.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#0.00");
vn.setNumberFormatOverride(df); // 數(shù)據(jù)軸數(shù)據(jù)標(biāo)簽的顯示格式

CategoryAxis domainAxis = plot.getDomainAxis();

domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 橫軸上的
// Lable
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);

domainAxis.setLabelFont(labelFont);// 軸標(biāo)題
domainAxis.setTickLabelFont(labelFont);// 軸數(shù)值

domainAxis.setMaximumCategoryLabelWidthRatio(0.8f);// 橫軸上的 Lable 是否完整顯示
// domainAxis.setVerticalCategoryLabels(false);
plot.setDomainAxis(domainAxis);

ValueAxis rangeAxis = plot.getRangeAxis();
// 設(shè)置最高的一個 Item 與圖片頂端的距離
rangeAxis.setUpperMargin(0.15);
// 設(shè)置最低的一個 Item 與圖片底端的距離
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRenderer renderer = new BarRenderer();
// 設(shè)置柱子寬度
renderer.setMaximumBarWidth(0.03);
// 設(shè)置柱子高度
renderer.setMinimumBarLength(30);

renderer.setBaseOutlinePaint(Color.BLACK);

// 設(shè)置柱的顏色
renderer.setSeriesPaint(0, Color.GREEN);
renderer.setSeriesPaint(1, new Color(0, 0, 255));
// 設(shè)置每個地區(qū)所包含的平行柱的之間距離
renderer.setItemMargin(0.5);
// 顯示每個柱的數(shù)值,并修改該數(shù)值的字體屬性
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
// 設(shè)置柱的數(shù)值可見
renderer.setBaseItemLabelsVisible(true);

plot.setRenderer(renderer);
// 設(shè)置柱的透明度
plot.setForegroundAlpha(0.6f);

FileOutputStream fos_jpg = null;
try


{
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 500, true, 10);
return chartName;
}
catch (Exception e)


{
e.printStackTrace();
return null;
}
finally


{
try


{
fos_jpg.close();
}
catch (Exception e)


{
e.printStackTrace();
}
}
}


/** *//**
* 餅狀圖
*
* @param dataset 數(shù)據(jù)集
* @param chartTitle 圖標(biāo)題
* @param charName 生成圖的名字
* @param pieKeys 分餅的名字集
* @return
*/
public String createValidityComparePimChar(PieDataset dataset,
String chartTitle, String charName, String[] pieKeys)


{
JFreeChart chart = ChartFactory.createPieChart3D(chartTitle, // chart
// title
dataset,// data
true,// include legend
true, false);

// 使下說明標(biāo)簽字體清晰,去鋸齒類似于
// chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);的效果
chart.setTextAntiAlias(false);
// 圖片背景色
chart.setBackgroundPaint(Color.white);
// 設(shè)置圖標(biāo)題的字體重新設(shè)置title
Font font = new Font("隸書", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);

PiePlot3D plot = (PiePlot3D) chart.getPlot();
// 圖片中顯示百分比:默認(rèn)方式

// 指定餅圖輪廓線的顏色
// plot.setBaseSectionOutlinePaint(Color.BLACK);
// plot.setBaseSectionPaint(Color.BLACK);

// 設(shè)置無數(shù)據(jù)時的信息
plot.setNoDataMessage("無對應(yīng)的數(shù)據(jù),請重新查詢。");

// 設(shè)置無數(shù)據(jù)時的信息顯示顏色
plot.setNoDataMessagePaint(Color.red);

// 圖片中顯示百分比:自定義方式,{0} 表示選項, {1} 表示數(shù)值, {2} 表示所占比例 ,小數(shù)點后兩位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// 圖例顯示百分比:自定義方式, {0} 表示選項, {1} 表示數(shù)值, {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);

// 設(shè)置第一個 餅塊section 的開始位置,默認(rèn)是12點鐘方向
plot.setStartAngle(90);

// // 設(shè)置分餅顏色
plot.setSectionPaint(pieKeys[0], new Color(244, 194, 144));
plot.setSectionPaint(pieKeys[1], new Color(144, 233, 144));

FileOutputStream fos_jpg = null;
try


{
// 文件夾不存在則創(chuàng)建
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
// 高寬的設(shè)置影響橢圓餅圖的形狀
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 230);

return chartName;
}
catch (Exception e)


{
e.printStackTrace();
return null;
}
finally


{
try


{
fos_jpg.close();
System.out.println("create pie-chart.");
}
catch (Exception e)


{
e.printStackTrace();
}
}

}


/** *//**
* 判斷文件夾是否存在,如果不存在則新建
* @param chartPath
*/
private void isChartPathExist(String chartPath)


{
File file = new File(chartPath);
if (!file.exists())


{
file.mkdirs();
// log.info("CHART_PATH="+CHART_PATH+"create.");
}
}


/** *//**
* 折線圖
*
* @param chartTitle
* @param x
* @param y
* @param xyDataset
* @param charName
* @return
*/
public String createTimeXYChar(String chartTitle, String x, String y,
CategoryDataset xyDataset, String charName)


{

JFreeChart chart = ChartFactory.createLineChart(chartTitle, x, y,
xyDataset, PlotOrientation.VERTICAL, true, true, false);

chart.setTextAntiAlias(false);
chart.setBackgroundPaint(Color.WHITE);
// 設(shè)置圖標(biāo)題的字體重新設(shè)置title
Font font = new Font("隸書", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);
// 設(shè)置面板字體
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);

chart.setBackgroundPaint(Color.WHITE);

CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
// x軸 // 分類軸網(wǎng)格是否可見
categoryplot.setDomainGridlinesVisible(true);
// y軸 //數(shù)據(jù)軸網(wǎng)格是否可見
categoryplot.setRangeGridlinesVisible(true);

categoryplot.setRangeGridlinePaint(Color.WHITE);// 虛線色彩

categoryplot.setDomainGridlinePaint(Color.WHITE);// 虛線色彩

categoryplot.setBackgroundPaint(Color.lightGray);

// 設(shè)置軸和面板之間的距離
// categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));

CategoryAxis domainAxis = categoryplot.getDomainAxis();

domainAxis.setLabelFont(labelFont);// 軸標(biāo)題
domainAxis.setTickLabelFont(labelFont);// 軸數(shù)值

domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 橫軸上的
// Lable
// 45度傾斜
// 設(shè)置距離圖片左端距離
domainAxis.setLowerMargin(0.0);
// 設(shè)置距離圖片右端距離
domainAxis.setUpperMargin(0.0);

NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);

// 獲得renderer 注意這里是下嗍造型到lineandshaperenderer!!
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
.getRenderer();

lineandshaperenderer.setBaseShapesVisible(true); // series 點(即數(shù)據(jù)點)可見
lineandshaperenderer.setBaseLinesVisible(true); // series 點(即數(shù)據(jù)點)間有連線可見

// 顯示折點數(shù)據(jù)
// lineandshaperenderer.setBaseItemLabelGenerator(new
// StandardCategoryItemLabelGenerator());
// lineandshaperenderer.setBaseItemLabelsVisible(true);

FileOutputStream fos_jpg = null;
try


{
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);

// 將報表保存為png文件
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 510);

return chartName;
}
catch (Exception e)


{
e.printStackTrace();
return null;
}
finally


{
try


{
fos_jpg.close();
System.out.println("create time-createTimeXYChar.");
}
catch (Exception e)


{
e.printStackTrace();
}
}
}


/** *//**
* 堆棧柱狀圖
*
* @param dataset
* @param xName
* @param yName
* @param chartTitle
* @param charName
* @return
*/
public String createStackedBarChart(CategoryDataset dataset, String xName,
String yName, String chartTitle, String charName)


{
// 1:得到 CategoryDataset

// 2:JFreeChart對象
JFreeChart chart = ChartFactory.createStackedBarChart(chartTitle, // 圖表標(biāo)題
xName, // 目錄軸的顯示標(biāo)簽
yName, // 數(shù)值軸的顯示標(biāo)簽
dataset, // 數(shù)據(jù)集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例(對于簡單的柱狀圖必須是false)
false, // 是否生成工具
false // 是否生成URL鏈接
);
// 圖例字體清晰
chart.setTextAntiAlias(false);

chart.setBackgroundPaint(Color.WHITE);

// 2 .2 主標(biāo)題對象 主標(biāo)題對象是 TextTitle 類型
chart
.setTitle(new TextTitle(chartTitle, new Font("隸書", Font.BOLD,
25)));
// 2 .2.1:設(shè)置中文
// x,y軸坐標(biāo)字體
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);

// 2 .3 Plot 對象 Plot 對象是圖形的繪制結(jié)構(gòu)對象
CategoryPlot plot = chart.getCategoryPlot();

// 設(shè)置橫虛線可見
plot.setRangeGridlinesVisible(true);
// 虛線色彩
plot.setRangeGridlinePaint(Color.gray);

// 數(shù)據(jù)軸精度
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// 設(shè)置最大值是1
vn.setUpperBound(1);
// 設(shè)置數(shù)據(jù)軸坐標(biāo)從0開始
// vn.setAutoRangeIncludesZero(true);
// 數(shù)據(jù)顯示格式是百分比
DecimalFormat df = new DecimalFormat("0.00%");
vn.setNumberFormatOverride(df); // 數(shù)據(jù)軸數(shù)據(jù)標(biāo)簽的顯示格式
// DomainAxis (區(qū)域軸,相當(dāng)于 x 軸), RangeAxis (范圍軸,相當(dāng)于 y 軸)
CategoryAxis domainAxis = plot.getDomainAxis();

domainAxis.setLabelFont(labelFont);// 軸標(biāo)題
domainAxis.setTickLabelFont(labelFont);// 軸數(shù)值

// x軸坐標(biāo)太長,建議設(shè)置傾斜,如下兩種方式選其一,兩種效果相同
// 傾斜(1)橫軸上的 Lable 45度傾斜
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
// 傾斜(2)Lable(Math.PI 3.0)度傾斜
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions
// .createUpRotationLabelPositions(Math.PI / 3.0));

domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 橫軸上的 Lable 是否完整顯示

plot.setDomainAxis(domainAxis);

// y軸設(shè)置
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
// 設(shè)置最高的一個 Item 與圖片頂端的距離
rangeAxis.setUpperMargin(0.15);
// 設(shè)置最低的一個 Item 與圖片底端的距離
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);

// Renderer 對象是圖形的繪制單元
StackedBarRenderer renderer = new StackedBarRenderer();
// 設(shè)置柱子寬度
renderer.setMaximumBarWidth(0.05);
// 設(shè)置柱子高度
renderer.setMinimumBarLength(0.1);
//設(shè)置柱的邊框顏色
renderer.setBaseOutlinePaint(Color.BLACK);
//設(shè)置柱的邊框可見
renderer.setDrawBarOutline(true);

// // 設(shè)置柱的顏色(可設(shè)定也可默認(rèn))
renderer.setSeriesPaint(0, new Color(204, 255, 204));
renderer.setSeriesPaint(1, new Color(255, 204, 153));

// 設(shè)置每個地區(qū)所包含的平行柱的之間距離
renderer.setItemMargin(0.4);

plot.setRenderer(renderer);
// 設(shè)置柱的透明度(如果是3D的必須設(shè)置才能達(dá)到立體效果,如果是2D的設(shè)置則使顏色變淡)
// plot.setForegroundAlpha(0.65f);

FileOutputStream fos_jpg = null;
try


{
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 500, true, 10);
return chartName;
}
catch (Exception e)


{
e.printStackTrace();
return null;
}
finally


{
try


{
fos_jpg.close();
}
catch (Exception e)


{
e.printStackTrace();
}
}
}

