JFreeChart生成圖片
<!-- jfreechart begin -->
<dependency>
<groupId>jfree</groupId>
<artifactId>jcommon</artifactId>
<version>1.0.23</version>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
</dependency>
<!-- jfreechart end -->
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
/**
* 實際取色的時候一定要16位的,這樣比較準確
*
*/
public class LineChart {
private String CHART_PATH = null;
static{
setChartTheme();
}
public static void setChartTheme() {
//創建主題樣式
StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
//設置標題字體
standardChartTheme.setExtraLargeFont(new Font("黑體",Font.BOLD,25));
//設置圖例的字體
standardChartTheme.setRegularFont(new Font("宋書",Font.TRUETYPE_FONT,12));
//設置軸向的字體
standardChartTheme.setLargeFont(new Font("宋書",Font.TRUETYPE_FONT,12));
//應用主題樣式
ChartFactory.setChartTheme(standardChartTheme);
}
/**
* 生成折線圖
* @throws Exception
*/
public String makeLineAndShapeChart(List<HashMap<String,double[]>> list,HttpServletRequest request) throws Exception {
CHART_PATH=request.getSession().getServletContext().getRealPath("fileDisk/report/monthSummary/img/");//圖片輸出路徑
int size = list.size();
//線的名稱 一維數組
String[] rowKeys = new String[size];
//二維數組存放折線圖數據
double[][] data = new double[size][12];
//線的名稱 與折線數據數組順序對應
String fileName="\\pic.png";
//橫坐標
String[] columnKeys = {"一月", "二月", "三月", "四月", "五月","六月","七月", "八月", "九月", "十月", "十一月","十二月"};
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
File directory=null;
File file=null;
String chartName = (CHART_PATH + fileName);
directory=new File(CHART_PATH);
if (!directory.exists()) {
directory.mkdirs();
}
file=new File(chartName);
createTimeXYChar("機車故障地理位置分布", "月份", "故障處理數量", dataset, file);
return "/fileDisk/report/monthSummary/img"+fileName;
}
// 折線圖 數據集
public CategoryDataset getBarData(double[][] data, String[] rowKeys,
String[] columnKeys) {
return DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
}
/**
* 折線圖
*
* @param chartTitle:折線圖標題
* @param x :X軸名稱
* @param y :Y軸名稱
* @param xyDataset
* @param charName
* @return
* @throws Exception
*/
public void createTimeXYChar(String chartTitle, String x, String y,CategoryDataset xyDataset, File file) throws Exception {
JFreeChart chart = ChartFactory.createLineChart(chartTitle, x, y, xyDataset, PlotOrientation.VERTICAL, true, true, false);
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);
// 設置面板字體
//Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
// x軸 // 分類軸網格是否可見
categoryplot.setDomainGridlinesVisible(true);
// y軸 //數據軸網格是否可見
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.WHITE);// 虛線色彩
categoryplot.setDomainGridlinePaint(Color.WHITE);// 虛線色彩
categoryplot.setBackgroundPaint(Color.lightGray);
// 設置軸和面板之間的距離
// categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
CategoryAxis domainAxis = categoryplot.getDomainAxis();
//domainAxis.setLabelFont(labelFont);// 軸標題
//domainAxis.setTickLabelFont(labelFont);// 軸數值
//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 橫軸上的
// Lable
// 45度傾斜
// 設置距離圖片左端距離
domainAxis.setLowerMargin(0.0);
// 設置距離圖片右端距離
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 點(即數據點)可見
lineandshaperenderer.setBaseLinesVisible(true); // series 點(即數據點)間有連線可見
// 顯示折點數據
lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
lineandshaperenderer.setBaseItemLabelsVisible(true);
FileOutputStream fos_jpg=null;
try {
fos_jpg = new FileOutputStream(file,false);
// 將報表保存為png文件
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 700, 490);
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
try {
fos_jpg.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
註意:X、Y坐標軸名稱在中間,不能放在末尾處
JFreeChart生成圖片