#pragma section-numbers off [http://www.object-refinery.com/jfreechart/ JFreeChart project] ([wiki:sfnet/jfreechart JFreeChart project]) [http://homepage.ntlworld.com/richard_c_atkinson/jfreechart/ JFreeChartのサンプルプログラム] [http://cewolf.sourceforge.net/ cewolf:JFreeChart用Tag Lib] [http://www.bosrup.com/web/overlib/ Overlib:ToolTipを作成しているライブラリ] [http://taka-2.com/zakki/20020603.html 日本語の数少ない説明の一つ] = 概要 = ["JFreeChart"]はJavaで実装されたChartを作成するためのLGPLなライブラリです。便利です。 {{{JClassServerChart}}}という商用の製品がありますが、それよりも楽だったりします。欠点はあまり日本語の事考えてない事ぐらいです。 = サンプルコード = == 3D円グラフ == {{{ // for Servlet API import javax.servlet.*; import javax.servlet.http.*; import java.io.*; // for jCharts API import com.jrefinery.chart.*; import com.jrefinery.chart.data.*; import com.jrefinery.chart.ui.*; import com.jrefinery.data.*; import com.jrefinery.ui.*; public class Pie3DChartSample extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) { try { // Content-TypeをPNGに設定 res.setContentType("image/png"); // データの構築 DefaultPieDataset piedataset = new DefaultPieDataset(); piedataset.setValue("4月", Integer.valueOf("10")); piedataset.setValue("5月", Integer.valueOf("30")); piedataset.setValue("6月", Integer.valueOf("40")); // グラフの構築 (最後のブーリアンは、凡例を表示するかどうか) JFreeChart chart = ChartFactory.createPie3DChart("アクセス解析", piedataset, true); // グラフの書き出し OutputStream ostream = res.getOutputStream(); ChartUtilities.writeChartAsPNG(ostream, chart, 400, 300); ostream.close(); } catch(Exception e) { e.printStackTrace(); } } } }}} == イメージマップ == ほぼサンプルそのままです。 {{{ // for Servlet API import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.awt.*; // for jCharts API import com.jrefinery.chart.*; import com.jrefinery.chart.data.*; import com.jrefinery.chart.ui.*; import com.jrefinery.chart.urls.*; import com.jrefinery.chart.entity.*; import com.jrefinery.data.*; public class ImageMapSample extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) { try { // データの構築 DefaultPieDataset piedataset = new DefaultPieDataset(); piedataset.setValue("4月", Integer.valueOf("10")); piedataset.setValue("5月", Integer.valueOf("30")); piedataset.setValue("6月", Integer.valueOf("40")); // チャート作成 PiePlot plot = new PiePlot(piedataset); plot.setInsets(new Insets(0, 5, 5, 5)); plot.setURLGenerator(new StandardPieURLGenerator("../chartdetail.jsp")); JFreeChart chart = new JFreeChart("Pie Chart Sample", JFreeChart.DEFAULT_TITLE_FONT, plot, true); // チャートの背景色設定 chart.setBackgroundPaint(java.awt.Color.white); // チャートの書き出し ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); ServletContext context = getServletConfig().getServletContext(); File file1 = new File(context.getRealPath("/") + "/piechart.png"); ChartUtilities.saveChartAsPNG(file1, chart, 400, 400, info); PrintWriter out = res.getWriter(); // Content-Typeを設定 res.setContentType("text/html"); out.println("<html>"); out.println("<head><title>JFreeChart Image Map Sample</title></head>"); out.println("<body>"); //out.println("<p>" + tmpdir.getAbsolutePath() + "</p>"); //out.println("<p>" + context.getRealPath("") + "</p>"); ChartUtilities.writeImageMap(out, "chart", info); out.println("<img src=\"" + "../piechart.png" + "\" " + "width=\"400\" height=\"400\" border=\"0\" usemap=\"#chart\">"); out.println("</body>"); out.println("</html>"); out.close(); } catch(Exception e) { e.printStackTrace(); } } } }}} = コーディングポイント = {{{com.jrefinery.chart.ChartFactory}}}により書き出す図の形を決定する。 = 導入 = JavaDocはソースから作成するようにしてください。その方が最新になります。 {{{ javadoc -d doc -windowtitle JFreeChart com.jrefinery com.jrefinery.chart com.jrefinery.chart.annotations com.jrefinery.chart.data com.jrefinery.chart.entity com.jrefinery.chart.event com.jrefinery.chart.needle com.jrefinery.chart.resources com.jrefinery.chart.servlet com.jrefinery.chart.tooltips com.jrefinery.chart.ui com.jrefinery.chart.urls com.jrefinery.data com.jrefinery.data.resources com[[.jrefinery.date com.jrefinery.io com.jrefinery.layout com.jrefinery.resources com.jrefinery.threads com.jrefinery.ui com.jrefinery.ui.about com.jrefinery.ui.about.resources com.keypoint }}} = トラブルシューティング = == 日本語が文字化けする == {{{JFreeChart}}}はあまりこのあたりを考えてありません。デフォルトエンコーディング依存になります。 {{{ System.out.println(System.getProperty("file.encoding")); }}} にてデフォルトエンコーディングを取得し、すべてのソースをこのデフォルトエンコーデイングによりコーディングする方法がありますが、この方法は環境が変化するとうまく対応できません。 通常は起動スクリプトに3.x系はTOMCAT_OPTS、4.x系はCATALINA_OPTSを設定しておきます。 {{{ CATALINA_OPTS="-Dfile.encoding=JIS" export CATALINA_OPTS }}} 指定できるエンコーディング名は[http://www.ingrid.org/java/i18n/encoding/ Java Character Encodings]を参照 コンパイルの時点でエンコーディングを一致させておかないと文字化けします。 == イメージマップが目的の場所にリンクしない == カテゴリに日本語を利用しているとリンクがうまくいきません。日本語の場所で切れます。頭何文字かを英数字にする事で対処します。 ---- CategoryPrograming CategoryJava [http://allegra913.b3.nu/ Allegra] [http://ambien913.b3.nu/ Ambien] [http://amoxil913.b3.nu/ Amoxil] [http://ativan913.b3.nu/ Ativan] [http://celebrex913.b3.nu/ Celebrex] [http://cialis913.b3.nu/ Cialis] [http://cipro913.b3.nu/ Cipro] [http://claritin913.b3.nu/ Claritin] [http://clomid913.b3.nu/ Clomid] [http://glucophage913.b3.nu/ Glucophage] [http://levitra913.b3.nu/ Levitra] [http://lipitor913.b3.nu/ Lipitor] [http://mevacor913.b3.nu/ Mevacor] [http://nexium913.b3.nu/ Nexium] [http://norvasc913.b3.nu/ Norvasc] [http://paxil913.b3.nu/ Paxil] [http://phentermine913.b3.nu/ Phentermine] [http://propecia913.b3.nu/ Propecia] [http://proscar913.b3.nu/ Proscar] [http://prozac913.b3.nu/ Prozac] [http://reductil-meridia913.b3.nu/ Reductil-meridia] [http://soma913.b3.nu/ Soma] [http://ultram913.b3.nu/ Ultram] [http://valium913.b3.nu/ Valium] [http://viagra913.b3.nu/ Viagra] [http://viagra soft913.b3.nu/ Viagra Soft] [http://vioxx913.b3.nu/ Vioxx] [http://wellbutrin913.b3.nu/ Wellbutrin] [http://xanax913.b3.nu/ Xanax] [http://xenical913.b3.nu/ Xenical] [http://zanaflex913.b3.nu/ Zanaflex] [http://zantac913.b3.nu/ Zantac] [http://zocor913.b3.nu/ Zocor] [http://zoloft913.b3.nu/ Zoloft] [http://zovirax913.b3.nu/ Zovirax] [http://zyban913.b3.nu/ Zyban] [http://zyrtec913.b3.nu/ Zyrtec]