一、引言: 實(shí)習單位安排讓調研報表工具選擇一款,讓學(xué)習和比較ChartDirector與Jasperreports,掌握技術(shù)路徑,即掌握安裝配置方式,接口,調用方法,例子等。 于是,下載了ChartDirector,挺簡(jiǎn)單的,照著(zhù)提供的jsp的例子,改一下數據、橫坐標內容就馬上能運行了,提供的函數命名也很規范,一看大概就知道用途,挺好理解的,生成的圖表也很漂亮。 于是,下載Jasperreports,這個(gè)東東要跑起來(lái)還真有點(diǎn)麻煩,首先得了解一下ANT,ant不難,但是為了運行一下示例還是被它拉路了一小會(huì ),然后是jdk版本兼容問(wèn)題,然后是數據源的連接問(wèn)題,折騰了一個(gè)星期,總會(huì )把它的示例都跑起來(lái)了,同時(shí)也寫(xiě)了三份學(xué)習筆記。接著(zhù)了解了一下它的可視化設計工具ireports??偟母杏X(jué)這個(gè)東西不如ChartDirector方便好用。 接著(zhù),跟莫sir討論一下,發(fā)現自己犯了一個(gè)很?chē)乐氐母拍钚藻e誤。那就是,圖表和報表是不同的!圖表是指圖片,即柱狀圖,折線(xiàn)圖,餅圖等,選擇圖表工具主要關(guān)注的是圖表是否漂亮;而報表是指表格形式的表,如財務(wù)報表等,選擇報表工具主要關(guān)注的是能否解決中國報表表頭復雜,交叉報表等問(wèn)題。ChartDirector是圖形報表工具;而Jasperreports是報表工具,生成圖表底層用的是JreeChart。所以,應該比較的是ChartDirector與JreeChart,而不是ChartDirector與Jasperreports的比較。 下面主要介紹和比較ChartDirector與JreeChart這兩款web圖形報表工具。
二、 ChartDirector與JreeChart的介紹與比較 2.1 官方網(wǎng)址 ChartDirector: http://www.advsofteng.com/ JreeChart: http://www.jfree.org/jfreechart/index.html 2.2 基本介紹 兩款都是流行的web圖表工具; ChartDirector: ChartDirector is a fast and powerful charting component for creating professional and clickable charts. It‘s innovative ChartDirector Mark Up Language, flexible object oriented API, layering architecture and advanced coloring system provide unprecedent chart design, formatting and customization capabilities to developers. Supports numerous chart types - Pie, donut, bar, line, spline, step line, trend line, curve-fitting, inter-line coloring, area, scatter, bubble, floating box, box-whisker, waterfall, finance (numerous indicators support), gantt, vector, radar, polar, rose, angular and linear meters and guages. ChartDirector is 100% pure Java code and is suitable for both client and server side usage. Specially designed graphics subsystem allows ChartDirector to run on headless servers without requiring headless support. Includes SWING controls to support client side applications. Easy to use and with comprehensive documentation. Come with numerous examples.
JreeChart: JFreeChart是一個(gè)開(kāi)源的 JAVA 項目,它主要用來(lái)開(kāi)發(fā)各種各樣的圖表,這些圖表包括:餅圖、柱狀圖 ( 普通柱狀圖以及堆棧柱狀圖 ) 、線(xiàn)圖、區域圖、分布圖、混合圖、甘特圖以及一些儀表盤(pán)等等。在這些不同式樣的圖表上可以滿(mǎn)足目前商業(yè)系統的要求。 JFreeChart 是一種基于 JAVA 語(yǔ)言的圖表開(kāi)發(fā)技術(shù)。 JFreeChart 可用于 Servlet 、 JSP 、 Applet 、 Java Appication 環(huán)境中,通過(guò) JDBC 可動(dòng)態(tài)顯示任何數據庫數據,結合 Itext 可以輸出至 PDF 文件。
2.3 收費情況 ChartDirector: 商業(yè);價(jià)格根據使用權限不同在59美元到749美元之間;也可以免費使用,只是在畫(huà)出來(lái)的圖形下面都有一條它的廣告條。網(wǎng)上有破解方法,破解后圖形下面不再出現它的廣告條。
JreeChart: 開(kāi)源;但是文檔要花錢(qián)買(mǎi),40美元;
2.4 支持語(yǔ)言 ChartDirector: 支持很多種語(yǔ)言,例如.NET, Java, ASP, COM,VB, PHP, Perl, Python,Ruby, ColdFusion, C++等; JreeChart: Java;
2.5 圖表比較 ChartDirector: 圖表特別精細,漂亮; 樣例庫:http://www.advsofteng.com/gallery.html
JreeChart: 畫(huà)出來(lái)的圖形不夠精細,看起來(lái)有些模糊;圖表的文字邊緣、顏色和顏色的分界也比較模糊。 樣例庫:http://www.jfree.org/jfreechart/samples.html
2.6 對中文問(wèn)題支持的比較 ChartDirector: 中文的問(wèn)題,比較容易解決。 JreeChart: 雖然有字體的解決辦法,但仍然存在問(wèn)題。他使用的默認字體顯示出來(lái)的中文會(huì )很模糊,你可能需要修改源代碼。
2.7 開(kāi)發(fā)使用易用性比較 從自己分別使用它們用jsp顯示柱狀圖的例子來(lái)看,兩者的開(kāi)發(fā)的易用性差不多,都是設置一下數據、橫坐標等就可以了。
三、 分別用ChartDirector和JFreeChart畫(huà)柱狀圖的JSP程序示例 3.1 用ChartDirector在JSP中畫(huà)統計圖 下面是一個(gè)柱狀圖的例子: 范例程序: <%@page import="ChartDirector.*" %> <% //The data for the bar chart double[] data = {85, 156, 179.5, 211, 123}; //The labels for the bar chart String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"}; //Create a XYChart object of size 300 x 280 pixels XYChart c = new XYChart(300, 280); //Set the plotarea at (45, 30) and of size 200 x 200 pixels c.setPlotArea(45, 30, 200, 200); //Add a title to the chart c.addTitle("Weekly Server Load"); //Add a title to the y axis c.yAxis().setTitle("MBytes"); //Add a title to the x axis c.xAxis().setTitle("Work Week 25"); //Add a bar chart layer with green (0x00ff00) bars using the given data c.addBarLayer(data, 0xff00).set3D(); //Set the labels on the x axis. c.xAxis().setLabels(labels); //output the chart String chart1URL = c.makeSession(request, "chart1"); //include tool tip for the chart String imageMap1 = c.getHTMLImageMap("", "", "title=‘{xLabel}: {value} MBytes‘") ; %> <html> <body topmargin="5" leftmargin="5" rightmargin="0"> <div style="font-size:18pt; font-family:verdana; font-weight:bold"> 3D Bar Chart </div> <hr color="#000080"> <a href="viewsource.jsp?file=<%=request.getServletPath()%>"> <font size="2" face="Verdana">View Chart Source Code</font> </a> </div> <br> <img src=‘<%=response.encodeURL("getchart.jsp?"+chart1URL)%>‘ usemap="#map1" border="0"> <map name="map1"><%=imageMap1%></map> </body> </html>
3.2 用JFreeChart畫(huà)柱狀圖的范例
這個(gè)范例說(shuō)明如何用JFreeChart畫(huà)簡(jiǎn)單的柱狀圖,下面是一個(gè)JSP的簡(jiǎn)單范例: <%@ page contentType="text/html; charset=GB2312" %> <%@ page import="java.awt.*, java.text.*, java.util.*" %> <%@ page import="org.jfree.chart.*" %> <%@ page import="org.jfree.chart.axis.*" %> <%@ page import="org.jfree.chart.labels.StandardCategoryItemLabelGenerator" %> <%@ page import="org.jfree.chart.plot.*" %> <%@ page import="org.jfree.chart.renderer.*" %> <%@ page import="org.jfree.chart.servlet.ServletUtilities" %> <%@ page import="org.jfree.data.DefaultCategoryDataset" %> <%@ page import="org.jfree.ui.TextAnchor" %> <% //The data for the bar chart double[] data = {85, 156, 179.5, 211, 123}; //The labels for the bar chart String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"}; DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i = 0; i < data.length; i++) { dataset.addValue(data[i], null, labels[i]); } JFreeChart chart = ChartFactory.createBarChart3D("Weekly Server Load", "Work Week 25", "MBytes", dataset, PlotOrientation.VERTICAL, false, false, false); chart.setBackgroundPaint(new Color(0xE1E1E1)); CategoryPlot plot = chart.getCategoryPlot(); // 設置Y軸顯示整數 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); CategoryAxis domainAxis = plot.getDomainAxis(); //設置距離圖片左端距離 domainAxis.setLowerMargin(0.05); BarRenderer3D renderer = new BarRenderer3D(); //設置柱的顏色 renderer.setSeriesPaint(0, new Color(0xff00)); plot.setRenderer(renderer); String filename = ServletUtilities.saveChartAsPNG(chart, 300, 280, null, session); String graphURL = request.getContextPath() + "/displayChart?filename=" + filename; %> <html> <body topmargin="5" leftmargin="5" rightmargin="0"> <div style="font-size:18pt; font-family:verdana; font-weight:bold"> 3D Bar Chart </div> <br> <img src="<%= graphURL %>" border=0> </body> </html> 畫(huà)出來(lái)的圖: 和ChartDirector畫(huà)出來(lái)的圖做一個(gè)比較: 四、 web圖表工具選擇 了解了關(guān)鍵的兩款web圖表工具ChartDirector與JreeChart之后,我們應該選擇哪一個(gè)報表工具呢?下面是本人提供的參考方案: ChartDirector:圖片精細漂亮,如果舍得花錢(qián)購買(mǎi),建議使用這款; JreeChart:圖片不夠清晰有些模糊,如果只考慮免費工具,建議使用這款;
五、 參考資料 1. http://www.advsofteng.com/ 2. http://www.jfree.org/jfreechart/index.html 3. ChartDirector (Java Edition) http://nuclearscripts.com/java/jsp-and-servlets/graphs-and-charts/chartdirector-java-edition.html 4. JFreeChat的介紹http://www.blogjava.net/chunkyo/archive/2007/01/13/jfreechat.html 5. 用JFreeChart畫(huà)柱狀圖的范例 用ChartDirector在JSP中畫(huà)統計圖 http://www.blogjava.net/georgehill/default.html?page=3 6. 一個(gè)很棒的WEB圖表項目??ChartDirector http://www.dlog.cn/html/diary/showlog.vm?sid=2&cat_id=-1&log_id=413 7. ChartDirector與JFreeChart http://blog.csdn.net/keelsike/archive/2006/05/30/762869.aspx | |