String chartTitle = ciinfoList.get(0).getHwname() + "_巡检情况统计";
HttpSession session = request.getSession();
//图表生成在tomcat下的temp目录
String filename = ServletUtilities.saveChartAsPNG(LineChart.getJFreeChart(defaultcategorydataset,chartTitle), 1000, 400, null, session);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
request.setAttribute("graphURL", graphURL);
request.setAttribute("filename", filename); public class LineChart {
/**
* 产生JFreeChart对象
*
* @return
*/
public static JFreeChart getJFreeChart(DefaultCategoryDataset chartData,String chartTitle) {
JFreeChart imgChart=null;
//JFreeChart对象 参数:标题,目录轴显示标签,数值轴显示标签,数据集,是否显示图例,是否生成工具,是否生成URL连接
imgChart = ChartFactory.createLineChart3D("", "巡检日期", "使用率", chartData, PlotOrientation.VERTICAL, true, true, false);
imgChart.setBackgroundPaint(Color.white);
imgChart.setBorderVisible(true);//边框可见
TextTitle title = new TextTitle(chartTitle, new Font("黑体", Font.BOLD, 20)); //解决曲线图片标题中文乱码问题
imgChart.setTitle(title);
imgChart.getLegend().setItemFont(new Font("黑体", Font.PLAIN, 18)); //解决图表底部中文乱码问题
CategoryPlot categoryplot = (CategoryPlot) imgChart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.white);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); //Y轴
setNumberAxis(numberaxis);
CategoryAxis domainAxis = (CategoryAxis) categoryplot.getDomainAxis(); //x轴
setDomainAxis(domainAxis);
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();//数据点
lineandshaperenderer.setBaseShapesVisible(true); //series 点(即数据点)可见
lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); //显示数据点的数据
lineandshaperenderer.setBaseItemLabelsVisible(true); //显示折线图点上的数据
/*纵坐标百分比显示*/
NumberFormat nfP = NumberFormat.getPercentInstance();
nfP.setMaximumFractionDigits(2);
lineandshaperenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",nfP));
return imgChart;
}
/**
* 设置X轴
* @param domainAxis
*/
private static void setDomainAxis(CategoryAxis domainAxis){
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 18)); //解决x轴坐标上中文乱码
domainAxis.setLabelFont(new Font("黑体", Font.PLAIN, 18)); //解决x轴标题中文乱码
domainAxis.setTickMarksVisible(true); //用于显示X轴刻度
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //设置X轴45度
}
/**
* 设置Y轴
* @param numberAxis
*/
private static void setNumberAxis(NumberAxis numberaxis){
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);//是否显示零点
numberaxis.setAutoTickUnitSelection(false);
numberaxis.setLabelFont(new Font("sans-serif", Font.PLAIN, 18)); //解决Y轴标题中文乱码
numberaxis.setTickUnit(new NumberTickUnit(0.1));//Y轴数据间隔
}
}
jsp显示生成的图片方式一:
FileOutputStream fos = new FileOutputStream("生成图片的完整路径+图片名"); ChartUtilities.writeChartAsJPEG(fos,1,LineChart.getJFreeChart("数据集","图片标题"),1000,400,null); request.setAttribute("jpgName", jpgName);//折线图名
<img src="<%=request.getContextPath() %>/images/${jpgName}" width=1000 height=400 usemap="${jpgName}"/>
jsp显示生成的图片方式二:
String chartTitle = ciinfoList.get(0).getHwname() + "_巡检情况统计"; HttpSession session = request.getSession(); //图表生成在tomcat下的temp目录 String filename =ServletUtilities.saveChartAsPNG(LineChart.getJFreeChart(defaultcategorydataset,chartTitle), 1000, 400, null, session); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; request.setAttribute("graphURL", graphURL); request.setAttribute("filename", filename);
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/servlet/DisplayChart</url-pattern> </servlet-mapping>
<img src="${graphURL}" width=1000 height=400 usemap="${filename}"/>