public String genLineChartHWL(BarChartInfo barChartInfo, List staticsResult,
HttpSession session, java.io.PrintWriter pw) {
//要返回的文件名,如果异常则返回null
String filename = null;
List forList = new ArrayList();
try {
//Throw a custom NoDataException if there is no data
if (staticsResult == null || staticsResult.size() == 0) {
logger.error("No data has been found");
return filename;
}
//Create and populate a CategoryDataset
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int currentSize = staticsResult.size();
for (int i = 0; i < currentSize; i++) {
ComDisplay info = (ComDisplay) staticsResult.get(i);
String curValue = null;
if (barChartInfo.getColString().equals("话务量")) {
curValue = info.getPhoneCapacityData();
}
String cityName = info.getCityName();
String dataDate = info.getDataDate().substring(5);
//添加实际数值
dataset.addValue( (curValue == null) ? 0 : Float.parseFloat(curValue), cityName, dataDate);
}
// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
"", // chart title
barChartInfo.getRowName(), // Domain axis label
barChartInfo.getColName(), // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
); chart.setBackgroundPaint(Color.white);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setMaxCategoryLabelWidthRatio(10);
//判断 Labels 是横 or 纵
categoryAxis.setVerticalCategoryLabels(true);
categoryAxis.setCategoryLabelPositionOffset(0); LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();
renderer.setDrawShapes(false);
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
//xiugai 8/20 begin
renderer.setItemLabelsVisible(false);
//xiugai 8/20
//OPTIONAL CUSTOMISATION COMPLETED.
plot.setRenderer(renderer);
plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
//Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
//生成图片600*400大小
filename = ServletUtilities.saveChartAsPNG(chart, 550, 350, info,session);
//Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush(); }
catch (Exception e) {
logger.error("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = null;
}
return filename;
}以上是java 代码,
jsp : String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
if ( null != filename )
{
%>
<tr>
<td class="b2" bgcolor="#FFFFFF" nowrap >
<div align="center">
<table width="100%" border="0" cellspacing="1" cellpadding="1" gcolor="#E7F5FF">
<tr>
<td width="100%" class="pt10" align="center" nowrap >
<img src="<%= graphURL %>" width=600 height=400 border=0 usemap="#<%= filename %>">
</td>
</tr>
</table>
</div>
</td>
</tr>
<%
}
HttpSession session, java.io.PrintWriter pw) {
//要返回的文件名,如果异常则返回null
String filename = null;
List forList = new ArrayList();
try {
//Throw a custom NoDataException if there is no data
if (staticsResult == null || staticsResult.size() == 0) {
logger.error("No data has been found");
return filename;
}
//Create and populate a CategoryDataset
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int currentSize = staticsResult.size();
for (int i = 0; i < currentSize; i++) {
ComDisplay info = (ComDisplay) staticsResult.get(i);
String curValue = null;
if (barChartInfo.getColString().equals("话务量")) {
curValue = info.getPhoneCapacityData();
}
String cityName = info.getCityName();
String dataDate = info.getDataDate().substring(5);
//添加实际数值
dataset.addValue( (curValue == null) ? 0 : Float.parseFloat(curValue), cityName, dataDate);
}
// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
"", // chart title
barChartInfo.getRowName(), // Domain axis label
barChartInfo.getColName(), // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
); chart.setBackgroundPaint(Color.white);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setMaxCategoryLabelWidthRatio(10);
//判断 Labels 是横 or 纵
categoryAxis.setVerticalCategoryLabels(true);
categoryAxis.setCategoryLabelPositionOffset(0); LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();
renderer.setDrawShapes(false);
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
//xiugai 8/20 begin
renderer.setItemLabelsVisible(false);
//xiugai 8/20
//OPTIONAL CUSTOMISATION COMPLETED.
plot.setRenderer(renderer);
plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
//Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
//生成图片600*400大小
filename = ServletUtilities.saveChartAsPNG(chart, 550, 350, info,session);
//Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush(); }
catch (Exception e) {
logger.error("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = null;
}
return filename;
}以上是java 代码,
jsp : String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
if ( null != filename )
{
%>
<tr>
<td class="b2" bgcolor="#FFFFFF" nowrap >
<div align="center">
<table width="100%" border="0" cellspacing="1" cellpadding="1" gcolor="#E7F5FF">
<tr>
<td width="100%" class="pt10" align="center" nowrap >
<img src="<%= graphURL %>" width=600 height=400 border=0 usemap="#<%= filename %>">
</td>
</tr>
</table>
</div>
</td>
</tr>
<%
}
<head>
<title>Chart</title>
</head>
<body bgcolor="#FFFFFF">
<img src="/路径/ChartDemoServlet"><br>
</body>
</html>就可以调用你上面的Servlet了
<servlet-name>ChartDemoServlet</servlet-name>
<servlet-class>com...ChartDemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChartDemoServlet</servlet-name>
<url-pattern>/路径/ChartDemoServlet</url-pattern>
</servlet-mapping>
<img src="/路径/ChartDemoServlet">
web.xml
web.xml <servlet>
<servlet-name>ChartDemoServlet</servlet-name>
<servlet-class>com...ChartDemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChartDemoServlet</servlet-name>
<url-pattern>/路径/ChartDemoServlet</url-pattern>
</servlet-mapping>