最近使用JFreeChart作了些东西,有人提出了这样的问题,“是否可以自己配置图片生成的路径,于是在便研究了几天,找到了自己的解决方案” 。
JFreeChart 自己的路径是这样生成的。首先它会使用类ServletUtilities的 createTempDir()来产生一个临时文件夹,该方法如下 :
protected static void createTempDir() {
String tempDirName = System.getProperty("java.io.tmpdir");//产生一个临时文件夹
if (tempDirName == null) {
throw new RuntimeException("Temporary directory system property "
+ "(java.io.tmpdir) is null.");
}
// create the temporary directory if it doesn't exist
File tempDir = new File(tempDirName);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
}
接下来生成图片的方法中调用该方法,并且使用该路径,下面是我的一个例子(柱状图):首先该类继承了ServletUtilities类
public class BarChart extends ServletUtilities {
private CategoryDataset webCategoryDataset;
private Font titleFont = new Font("黑体",Font.CENTER_BASELINE,18);
public void setWebCategoryDataset(CategoryDataset webCategoryDataset) {
this.webCategoryDataset = webCategoryDataset;
}
private JFreeChart createChart(CategoryDataset categorydataset)
{
JFreeChart jfreechart = ChartFactory.createBarChart("Bar Chart Demo", //图形标题名称
"Category",//domain 轴 Label 这里先简单理解为横坐标Label好了
"Value", //range 轴 Label这里也先简单理解为纵坐标Label好了
categorydataset, // dataset
PlotOrientation.VERTICAL, //垂直显示
true, // legend
true, // tooltips
false); //URLs
jfreechart.setBackgroundPaint(Color.white); //设定背景色为白色
CategoryPlot categoryplot = jfreechart.getCategoryPlot(); //获得 plot:CategoryPlot!!
categoryplot.setBackgroundPaint(Color.lightGray); //设定图表数据显示部分背景色
categoryplot.setDomainGridlinePaint(Color.white); //横坐标网格线白色
categoryplot.setDomainGridlinesVisible(true); //可见
categoryplot.setRangeGridlinePaint(Color.white); //纵坐标网格线白色
//下面两行使纵坐标的最小单位格为整数
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer barrenderer = (BarRenderer)categoryplot.getRenderer(); //获得renderer 注意这里是下嗍造型到BarRenderer!!
barrenderer.setDrawBarOutline(false); // Bar的外轮廓线不画
GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue,
0.0F, 0.0F, new Color(0, 0, 64)); //设定特定颜色
GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green,
0.0F, 0.0F, new Color(0, 64, 0));
GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red,
0.0F, 0.0F, new Color(64, 0, 0));
GradientPaint gradientpaint3 = new GradientPaint(0.0F, 0.0F, Color.orange,
0.0F, 0.0F, new Color(64, 0, 0));
barrenderer.setSeriesPaint(0, gradientpaint); //给series1 Bar设定上面定义的颜色
barrenderer.setSeriesPaint(1, gradientpaint1); //给series2 Bar 设定上面定义的颜色
barrenderer.setSeriesPaint(2, gradientpaint2);
barrenderer.setSeriesPaint(3, gradientpaint3);//给series3 Bar 设定上面定义的颜色
CategoryAxis categoryaxis = categoryplot.getDomainAxis(); //横轴上的 Label 45度倾斜
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//一些重要的方法:(增加一块标记)
IntervalMarker intervalmarker = new IntervalMarker(4.5D, 7.5D);
intervalmarker.setLabel("Target Range");
intervalmarker.setLabelFont(new Font("SansSerif", 2, 11));
intervalmarker.setLabelAnchor(RectangleAnchor.LEFT);
intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
intervalmarker.setPaint(new Color(222, 222, 255, 128));
categoryplot.addRangeMarker(intervalmarker, Layer.BACKGROUND);
return jfreechart;
}
//为生成的图片创建文件夹
protected static void createTempDir() {
String tempDirName = "D://graph"; //此路径可以在属性文件中配置
if (tempDirName == null) {
throw new RuntimeException("Temporary directory system property "
+ "(java.io.tmpdir) is null.");
}
// create the temporary directory if it doesn't exist
File tempDir = new File(tempDirName);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
}
public static String saveChartAsPNG(JFreeChart chart, int width, int height,//覆盖父类的方法
ChartRenderingInfo info, HttpSession session) throws IOException {
if (chart == null) {
throw new IllegalArgumentException("Null 'chart' argument.");
}
createTempDir();
String prefix = ServletUtilities.getTempFilePrefix();
if (session == null) {
prefix = ServletUtilities.getTempOneTimeFilePrefix();
}
File tempFile = File.createTempFile(prefix, ".png",
new File("D://graph"));
ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
if (session != null) {
ServletUtilities.registerChartForDeletion(tempFile, session);
}
return tempFile.getName();
}
public String generateStepRendererChart(
String title,
HttpSession session,
PrintWriter pw) {
String filename = null;
try {
JFreeChart chart = createChart(webCategoryDataset);
//设置图片的背景色
chart.setBackgroundPaint(new Color(255,255,255));
//设置图片标题的字体和大小
TextTitle _title = new TextTitle(title);
_title.setFont(titleFont);
chart.setTitle(_title);
//把生成的图片放到临时目录
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
//设置图片名称前缀
ServletUtilities.setTempFilePrefix("chart-");
//500是图片长度,300是图片高度
filename = saveChartAsPNG(chart, 600, 400, info, session); //create graph
ChartUtilities.writeImageMap(pw, filename, info, false); //add by ann.li Writes an image map to an output stream
pw.flush();
System.out.println(System.getProperty("java.io.tmpdir") + filename);
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_600x400.png";
}
return filename;
}
}
其中绿色就是我修改的地方,由于java基础还不是很好,希望有人给出好的建议与方案。