JFreeChart生成图片
Posted 袋子里的袋鼠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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生成图片的主要内容,如果未能解决你的问题,请参考以下文章
freemarker+Jfreechart生成Word文档(含图片)