JFreeChart生成图片到服务器指定目录

//曲线图
public String imageLine(TimeSeries chinaTs,HttpSession session) {
String title = "访问量统计";
String domain = "";
String range = "访问量";
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.removeAllSeries();
dataset.addSeries(chinaTs);
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain,
range, dataset, true, true, false);
Font font = new Font("宋体", Font.PLAIN, 12);
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) plot
.getRenderer();
plot.setBackgroundPaint(new Color(199, 237, 204));
xylineandshaperenderer.setBaseShapesVisible(true);
xylineandshaperenderer.setBaseItemLabelsVisible(true);
DateAxis axis = (DateAxis) plot.getDomainAxis();
plot.getRangeAxis().setLabelFont(font);
chart.getLegend().setItemFont(font);
axis.setLabelFont(font);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
axis.setDateFormatOverride(format);
chart.setTitle(new TextTitle(chart.getTitle().getText(), font));
chart.getLegend().setItemFont(font);
String filename = "";
try {
FreeChartUtil.setVisitImageBase(visitImageBase);
filename = FreeChartUtil.saveChartAsJPEG(chart, 700, 400, session);
} catch (IOException e) {
}
model.addAttribute("graphURL",
"<img src='http://jchen.iteye.com/admin/" + filename
+ "'>");
return "";
}
FreeChartUtil 重写了图片的保存路径

jfreeChart在unix下运行的中文字体问题还是给我们带来了诸多不便,如果在unix下找不到相应的中文字体,中文将会被小方框取代

//柱形图
public String imageBar(DefaultCategoryDataset dataset, HttpSession session) {
// 图表标题 X轴标签 Y轴标签 数据集合 图表方向:垂直 是否显示图例 是否生成工具 是否生成URL链接
JFreeChart chart = ChartFactory.createBarChart3D("", "", "访问量",
dataset, PlotOrientation.VERTICAL, false, false, false);
chart.setTitle(new TextTitle("访问量统计", new Font("宋体",
Font.ITALIC, 22)));
chart.setBackgroundPaint(Color.WHITE);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setSeriesPaint(0, Color.ORANGE);
renderer.setWallPaint(new Color(199, 237, 204));
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));
renderer.setItemLabelsVisible(true);
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(new Color(199, 237, 204));
CategoryAxis domainAxis = plot.getDomainAxis();
Font font = new Font("宋体", Font.PLAIN, 12);
domainAxis.setLabelFont(font);
domainAxis.setTickLabelFont(font);
domainAxis.setTickLabelPaint(Color.BLACK);
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
plot.setDomainAxis(domainAxis);
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(font);
rangeAxis.setLabelPaint(Color.BLACK);
rangeAxis.setTickLabelFont(font);
rangeAxis.setUpperMargin(0.15);
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
plot.setRenderer(renderer);
String filename = "";
try {
FreeChartUtil.setVisitImageBase(visitImageBase);
filename = FreeChartUtil.saveChartAsJPEG(chart, 700, 400, session);
} catch (IOException e) {
}
return "";
}

//饼图
public String imagePie(DefaultPieDataset defaultpiedataset, HttpSession session) {
JFreeChart chart = ChartFactory.createPieChart("访问量统计",
defaultpiedataset, false, true, false);
TextTitle txtTitle = chart.getTitle();
txtTitle.setFont(new Font("宋体", 0, 16));
PiePlot pieplot = (PiePlot) chart.getPlot();
pieplot.setLabelFont(new Font("宋体", 0, 12));
pieplot.setNoDataMessage("无数据");
pieplot.setCircular(true);
pieplot.setLabelGap(0.02D);
pieplot.setBackgroundPaint(new Color(199, 237, 204));
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0} {2}", NumberFormat.getNumberInstance(), new DecimalFormat(
"0.00%")));
LegendTitle legendtitle = new LegendTitle(chart.getPlot());
BlockContainer blockcontainer = new BlockContainer(
new BorderArrangement());
LabelBlock labelblock = new LabelBlock("", new Font("宋体", 1, 12));
labelblock.setPadding(5D, 5D, 5D, 5D);
blockcontainer.add(labelblock, RectangleEdge.TOP);
SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
LabelBlock labelblock1 = new LabelBlock(df.format(new Date()));
labelblock1.setPadding(8D, 20D, 2D, 5D);
blockcontainer.add(labelblock1, RectangleEdge.BOTTOM);
BlockContainer blockcontainer1 = legendtitle.getItemContainer();
blockcontainer1.setPadding(2D, 10D, 5D, 2D);
blockcontainer.add(blockcontainer1);
legendtitle.setWrapper(blockcontainer);
legendtitle.setPosition(RectangleEdge.RIGHT);
legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
chart.addSubtitle(legendtitle);
chart.setBackgroundPaint(new Color(199, 237, 204));
String filename = "";
try {
FreeChartUtil.setVisitImageBase(visitImageBase);
filename = FreeChartUtil.saveChartAsJPEG(chart, 700, 400, session);
} catch (IOException e) {
}
return "";
}

所需包
<dependency>
  <groupId>jfree</groupId>
  <artifactId>jfreechart</artifactId>
  <version>1.0.13</version>
  </dependency>
<dependency>
  <groupId>jfree</groupId>
  <artifactId>jcommon</artifactId>
  <version>1.0.2</version>
  </dependency>

你可能感兴趣的:(unix,jfreechart,UP)