當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
用jfreechart在JSP页面显示点状分布图+分割线(转载:http://blog.csdn.net/ami121/article/details/2450945)
生活随笔
收集整理的這篇文章主要介紹了
用jfreechart在JSP页面显示点状分布图+分割线(转载:http://blog.csdn.net/ami121/article/details/2450945)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JSP頁面
?
<%@?page?language="java"??pageEncoding="gbk"%><%
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
<html>
??<head>
????<base?href="<%=basePath%>">
????
????<title>My?JSP?'index.jsp'?starting?page</title>
????
?
<jsp:useBean?id="barchart01"?scope="session"
?class="com.ami.chart.SeriesXY"?/>
</head>
<body>
<%
?String?fileName?=?barchart01.getImg(request.getSession(),?out);
?String?graphURL?=?request.getContextPath()
???+?"/servlet/DisplayChart?filename="?+?fileName;
%>
<br/>
<img?src="<%=?graphURL?%>"?border="0"?usemap="#<%=fileName?%>"?/>
<br?/>
</body>
</html>
WEB.XML
?
?<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>
?
Java實現代碼
?
package?com.ami.chart;import?java.awt.BasicStroke;
import?java.awt.Color;
import?java.awt.Font;
import?java.io.PrintWriter;
import?javax.servlet.http.HttpSession;
import?javax.servlet.jsp.JspWriter;
import?org.jfree.chart.ChartFactory;
import?org.jfree.chart.ChartRenderingInfo;
import?org.jfree.chart.ChartUtilities;
import?org.jfree.chart.JFreeChart;
import?org.jfree.chart.axis.NumberAxis;
import?org.jfree.chart.axis.NumberTickUnit;
import?org.jfree.chart.axis.ValueAxis;
import?org.jfree.chart.entity.StandardEntityCollection;
import?org.jfree.chart.plot.PlotOrientation;
import?org.jfree.chart.plot.ValueMarker;
import?org.jfree.chart.plot.XYPlot;
import?org.jfree.chart.renderer.xy.XYItemRenderer;
import?org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import?org.jfree.chart.servlet.ServletUtilities;
import?org.jfree.data.xy.XYSeries;
import?org.jfree.data.xy.XYSeriesCollection;
import?org.jfree.ui.RectangleAnchor;
import?org.jfree.ui.TextAnchor;
public?class?SeriesXY?{
????
????public?String?getImg(HttpSession?session?,JspWriter?out){
????????
????????String?filename=null;
????????double?[]xydata1_0={12.8d,12.3d,12.9d,12.2d,13.3d,12.0d,12.8d,13.3d,12.9d,13.9d,12.3d,12.0d,12.8d,12.3d,11.9d,13.2d,12.3d,14.0d,12.8d,12.3d,12.9d,13.2d,13.3d,14.0d,12.8d,13.3d,11.9d,13.9d,12.3d,12.0d,19.8d,12.3d,14.9d,18.2d,12.3d,14.0d,12.8d,12.3d,12.9d,18.2d,13.3d,14.0d,12.8d,14.3d,12.9d,13.9d,12.3d,12.0d,13.8d,12.3d,11.9d,13.2d,12.3d,14.0d,12.8d,12.3d,12.9d,18.2d,16.3d,18.0d,12.8d,12.3d,19.9d,13.9d,12.3d,12.0d,12.8d,12.3d,12.9d,13.2d,12.3d,14.0d,12.8d,12.3d,12.9d,12.2d,13.3d,12.0d,12.8d,13.3d,12.9d,13.9d};
????????double?[]xydata1_1={196.0d,223.0d,230.5d,241.0d,195.8d,200.3d,200.0d,230.0d,243.5d,261.0d,259.8d,245.3d,236.0d,260.0d,225.5d,230.0d,190.8d,205.3d,196.0d,223.0d,230.5d,241.0d,195.8d,200.3d,200.0d,230.0d,243.5d,261.0d,259.8d,245.3d,236.0d,260.0d,225.5d,230.0d,190.8d,205.3d,196.0d,223.0d,230.5d,241.0d,195.8d,200.3d,200.0d,230.0d,243.5d,261.0d,259.8d,245.3d,236.0d,260.0d,225.5d,230.0d,190.8d,205.3d,196.0d,223.0d,230.5d,241.0d,195.8d,200.3d,200.0d,230.0d,243.5d,261.0d,259.8d,245.3d,236.0d,260.0d,225.5d,230.0d,190.8d,205.3d,196.0d,223.0d,230.5d,241.0d,195.8d,200.3d,200.0d,230.0d,243.5d,261.0d};
????????
????????XYSeriesCollection?xyCollection=new?XYSeriesCollection();
????????XYSeries?xyseries1=new?XYSeries("ACompany");
????????for(int?i=0;i<82;i++){
????????????xyseries1.add(xydata1_0[i],?xydata1_1[i]);
????????}
????????XYSeries?xyseries2=new?XYSeries("BCompany");
????????
????????xyCollection.addSeries(xyseries1);
????????
????????//分布點狀圖
????????JFreeChart?chart=ChartFactory.createScatterPlot("電壓電流圖",?"電流","電壓",?xyCollection,?PlotOrientation.VERTICAL,?true,?false,?false);
????????XYPlot?plot=(XYPlot)?chart.getPlot();
????????
????????ValueMarker?vmarker=new?ValueMarker(230d);//設置分割線
????????vmarker.setLabel("平均電壓230V");
????????vmarker.setLabelPaint(Color.black);
????????vmarker.setPaint(Color.red);
????????vmarker.setLabelFont(new?Font("隸書",Font.PLAIN,10));
????????vmarker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
????????vmarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
????????plot.addRangeMarker(vmarker);
????????
????????ValueMarker?xMarker=new?ValueMarker(12.50d);
????????xMarker.setLabel("平均電流12.50A");
????????xMarker.setPaint(Color.orange);
????????xMarker.setLabelFont(new?Font("隸書",Font.BOLD,10));
????????xMarker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
????????xMarker.setLabelTextAnchor(TextAnchor.BOTTOM_RIGHT);
????????plot.addDomainMarker(xMarker);
????????plot.setRangeGridlinePaint(Color.black);//設置X軸的顏色
????????
????????ValueAxis?yAxis=plot.getRangeAxis();
????????yAxis.setAutoRange(true);
????????yAxis.setAutoRangeMinimumSize(50.0d);
????????NumberAxis?yNumber=(NumberAxis)?plot.getRangeAxis();
????????yNumber.setTickUnit(new?NumberTickUnit(50d));
????????yAxis.setLowerBound(50.0d);
????????plot.setRangeAxis(yAxis);
????????
????????ValueAxis?xAxis=plot.getDomainAxis();
????????xAxis.setAutoRange(false);
????????NumberAxis???numberaxis=(NumberAxis)plot.getDomainAxis();
????????numberaxis.setTickUnit(new?NumberTickUnit(0.1D));//設置X軸跨度每次增加1
????????xAxis.setUpperBound(14.2d);
????????xAxis.setLowerBound(11.8d);
????????
????????XYItemRenderer?itRender=plot.getRenderer();
????????
????????????????try?{
????????????PrintWriter??pw=new?PrintWriter(out);
????????????StandardEntityCollection?sec?=?new?StandardEntityCollection();
????????????ChartRenderingInfo?info?=?new?ChartRenderingInfo(sec);
????????????filename=ServletUtilities.saveChartAsJPEG(chart,650,?400,null,?session);
????????????ChartUtilities.writeImageMap(pw,?filename,info,?true);
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????????return?filename;
????}
}
效果圖
總結
以上是生活随笔為你收集整理的用jfreechart在JSP页面显示点状分布图+分割线(转载:http://blog.csdn.net/ami121/article/details/2450945)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 填了专项附加扣除为什么没减税
- 下一篇: 会议活动适合做互动吗?适合什么样的互动形