JFreeChart制作简单的饼状图

<p class="MsoNormal">
</p>
<p class="MsoNormal"><span>最近一直有同学问我怎么用</span><span lang="EN-US">JFreeChart</span><span>做统计报表,所以在这里写一下,先讲一下饼状图的使用,之后再讲树状图和折线图吧</span>,如果问题请留言</p>

<p class="MsoNormal"><span lang="EN-US"></span></p>
<p class="MsoNormal">&lt;!--[if !supportLists]--&gt;<span lang="EN-US"><span>一.</span></span>&lt;!--[endif]--&gt;<span>导包</span></p>
<p class="MsoNormal"><span>下载</span><span lang="EN-US">jfreechart</span><span>框架包,百度上可以查到,开源的。这个是csdn上的</span></p>
<p class="MsoNormal"><span><a href="http://download.csdn.net/source/3108268">http://download.csdn.net/source/3108268</a></span></p>
<p class="MsoNormal"><span lang="EN-US"></span></p>
<p class="MsoNormal">&lt;!--[if !supportLists]--&gt;<span lang="EN-US"><span>二.</span></span>&lt;!--[endif]--&gt;<span>配置</span><span lang="EN-US">web.xml</span></p>
<p class="MsoNormal"><span>加上以下信息:</span> </p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet</span><span lang="EN-US">&gt;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span><span> </span></span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">DisplayChart</span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span><span lang="EN-US"><span> </span></span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-class</span><span lang="EN-US">&gt;</span><span lang="EN-US">org.jfree.chart.servlet.DisplayChart</span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-class</span><span lang="EN-US">&gt;&lt;/</span><span lang="EN-US">servlet</span><span lang="EN-US">&gt;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-mapping</span><span lang="EN-US">&gt;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">DisplayChart</span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span><span> </span></span><span lang="EN-US">&lt;</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span><span lang="EN-US">/<span style="text-decoration: underline;">servlet</span>/DisplayChart</span><span lang="EN-US">&lt;/</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span></p>
<p class="MsoNormal"><span lang="EN-US"><span> </span></span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-mapping</span><span lang="EN-US">&gt;</span></p>
<p class="MsoNormal"><span lang="EN-US"></span></p>
<p class="MsoNormal">&lt;!--[if !supportLists]--&gt;<span lang="EN-US"><span>三.</span></span>&lt;!--[endif]--&gt;<span>编写类</span></p>
<p class="MsoNormal"><span lang="EN-US"><span> </span></span><span>代码如下:</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">package</span></strong><span lang="EN-US"> com.qm;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> java.awt.Color;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> java.awt.Font;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> java.io.PrintWriter;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> javax.servlet.http.HttpSession;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> org.jfree.chart.ChartFactory;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> org.jfree.chart.ChartRenderingInfo;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> org.jfree.chart.ChartUtilities;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> org.jfree.chart.JFreeChart;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> org.jfree.chart.entity.StandardEntityCollection;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> org.jfree.chart.plot.PiePlot;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> org.jfree.chart.servlet.ServletUtilities;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> org.jfree.data.general.DefaultPieDataset;</span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">import</span></strong><span lang="EN-US"> org.jfree.data.general.PieDataset;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"></span></p>
<p class="MsoNormal" align="left"><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> </span><strong><span lang="EN-US">class</span></strong><span lang="EN-US"> AA {</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><span lang="EN-US">//</span><span>在此方法内填入数据,</span><span lang="EN-US">key-value.value</span><span>为数字。</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><strong><span lang="EN-US">private</span></strong><span lang="EN-US"> </span><strong><span lang="EN-US">static</span></strong><span lang="EN-US"> PieDataset createDataset()</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>{</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>DefaultPieDataset dpd=</span><strong><span lang="EN-US">new</span></strong><span lang="EN-US"> DefaultPieDataset();</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>dpd.setValue(</span><span lang="EN-US">"A"</span><span lang="EN-US">, 1);</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>dpd.setValue(</span><span lang="EN-US">"B"</span><span lang="EN-US">, 2);</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>dpd.setValue(</span><span lang="EN-US">"C"</span><span lang="EN-US">, 3);</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>dpd.setValue(</span><span lang="EN-US">"d"</span><span lang="EN-US">, 4);</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>dpd.setValue(</span><span lang="EN-US">"E"</span><span lang="EN-US">, 5);</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><strong><span lang="EN-US">return</span></strong><span lang="EN-US"> dpd;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><strong><span lang="EN-US">private</span></strong><span lang="EN-US"> </span><strong><span lang="EN-US">static</span></strong><span lang="EN-US"> JFreeChart createChart(PieDataset dpd)</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>{</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>JFreeChart jfc=ChartFactory.<em>createPieChart3D</em>(</span><span lang="EN-US">""</span><span lang="EN-US">,dpd,</span><strong><span lang="EN-US">true</span></strong><span lang="EN-US">,</span><strong><span lang="EN-US">true</span></strong><span lang="EN-US">,</span><strong><span lang="EN-US">false</span></strong><span lang="EN-US">);<span> </span></span><span lang="EN-US">//</span><span>这个有</span><span lang="EN-US">3D</span><span>效果</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><span lang="EN-US">//JFreeChart <span style="text-decoration: underline;">jfc</span>=ChartFactory.createPieChart("",<span style="text-decoration: underline;">dpd</span>,true,true,false);<span> </span>//</span><span>这个没有有</span><span lang="EN-US">3D</span><span>效果</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>jfc.setBackgroundPaint(Color.</span><em><span lang="EN-US">white</span></em><span lang="EN-US">);<span> </span></span><span lang="EN-US">//</span><span>背景色</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>jfc.getTitle().setFont(</span><strong><span lang="EN-US">new</span></strong><span lang="EN-US"> Font(</span><span lang="EN-US">"</span><span>宋体</span><span lang="EN-US">"</span><span lang="EN-US">,Font.</span><em><span lang="EN-US">BOLD</span></em><span lang="EN-US">,20));</span><span lang="EN-US">//</span><span>设置标题字体</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span><span> </span>PiePlot plot= (PiePlot) jfc.getPlot();</span><span lang="EN-US">//</span><span>获取图表区域对象</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span><span> </span>plot.setLabelFont(</span><strong><span lang="EN-US">new</span></strong><span lang="EN-US"> Font(</span><span lang="EN-US">"</span><span>宋体</span><span lang="EN-US">"</span><span lang="EN-US">,Font.</span><em><span lang="EN-US">BOLD</span></em><span lang="EN-US">,10));</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span><span> </span>jfc.getLegend().setItemFont(</span><strong><span lang="EN-US">new</span></strong><span lang="EN-US"> Font(</span><span lang="EN-US">"</span><span>宋体</span><span lang="EN-US">"</span><span lang="EN-US">,Font.</span><em><span lang="EN-US">BOLD</span></em><span lang="EN-US">,10));</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><strong><span lang="EN-US">return</span></strong><span lang="EN-US"> jfc;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> </span><strong><span lang="EN-US">static</span></strong><span lang="EN-US"> String generateBarChart(HttpSession session, PrintWriter pw)</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>{</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>String filename=</span><strong><span lang="EN-US">null</span></strong><span lang="EN-US">;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>PieDataset cd=<em>createDataset</em>();</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>JFreeChart chart=<em>createChart</em>(cd);</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>ChartRenderingInfo info=</span><strong><span lang="EN-US">new</span></strong><span lang="EN-US"> ChartRenderingInfo(</span><strong><span lang="EN-US">new</span></strong><span lang="EN-US"> StandardEntityCollection());</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><strong><span lang="EN-US">try</span></strong><span lang="EN-US"> {</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>filename=ServletUtilities.<em>saveChartAsPNG</em>(chart, 800, 400, info,session);</span><span lang="EN-US">//</span><span>设置生成的图片的大小</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>ChartUtilities.<em>writeImageMap</em>(pw, filename, info, </span><strong><span lang="EN-US">false</span></strong><span lang="EN-US">);</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>} </span><strong><span lang="EN-US">catch</span></strong><span lang="EN-US"> (Exception e) {</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>e.printStackTrace();</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>pw.flush();</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span></span><strong><span lang="EN-US">return</span></strong><span lang="EN-US"> filename;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>}</span></p>
<p class="MsoNormal"><span lang="EN-US">}</span></p>
<p class="MsoNormal"><span lang="EN-US"></span></p>
<p class="MsoNormal">&lt;!--[if !supportLists]--&gt;<span lang="EN-US"><span>四.<span> </span></span></span>&lt;!--[endif]--&gt;<span>在</span><span lang="EN-US">jsp</span><span>中使用图表</span></p>
<p class="MsoNormal"><span lang="EN-US"><span> </span></span><span>代码如下:</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US">&lt;%@</span><span lang="EN-US">page</span><span lang="EN-US"> <span>import</span><span>=</span><em><span>"com.qm.AA"</span></em><span>%&gt;</span></span></p>
<p class="MsoNormal" align="left"><span lang="EN-US">&lt;%@</span><span lang="EN-US">page</span><span lang="EN-US"> <span>import</span><span>=</span><em><span>"java.io.PrintWriter"</span></em><span>%&gt;</span></span></p>
<p class="MsoNormal" align="left"><span lang="EN-US">&lt;%</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>String filename=AA.generateBarChart(request.getSession(),</span><strong><span lang="EN-US">new</span></strong><span lang="EN-US"> PrintWriter(out));</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"><span> </span>String graphURl = request.getContextPath() + </span><span lang="EN-US">"/servlet/DisplayChart?filename="</span><span lang="EN-US"> + filename;</span></p>
<p class="MsoNormal" align="left"><span lang="EN-US"></span></p>
<p class="MsoNormal" align="left"><span lang="EN-US">&lt;img alt="</span><span>分析图</span><span lang="EN-US">" src="&lt;%=graphURl %&gt;" width="800" height="400"&gt;</span></p>
<p class="MsoNormal"><span lang="EN-US">%&gt;</span></p>

你可能感兴趣的:(jfreechart)