ChartDirector除了一個英文件的幫助以外,也沒有再提供java DOC形式的文檔,為了方便,寫以下一個例子說明使用ChartDirector生成柱狀圖的方法.jsp方式實質與JAVA方式沒有區別,這里是我從JSP中取的代碼(JSP改起來方便,不過手動)
代碼如下:
<%@ page language="java" contentType="text/Html; charset=UTF-8"
pageEncoding="UTF-8" import="ChartDirector.*;"%>
<%
request.setCharacterEncoding("UTF-8");
//以兩個系列數據為例
double[] data = {185, 156, 179.5, 211, 123};
double[] data1 = {55, 76, 34.5, 88, 43};
//數據列名
String[] labels = {"一月", "二月", "三月", "四月", "五月"};
//生成圖片大小 250 x 250
XYChart c = new XYChart(550, 350);
//圖標題
c.addTitle("第一個圖","",15);
//支持中文
c.setDefaultFonts("SIMSUN.TTC","simhei.ttf");
//圖表在圖片中的定位及區域大小
c.setPlotArea(30, 40, 400, 250);
//=========================
//加入單個數據
//BarLayer layer = c.addBarLayer(data,0xff3456,"我的測試");
//=========================
//加入多個BAR數據(多個datasets)
BarLayer layer = c.addBarLayer2(Chart.Side, 3);
layer.addDataSet(data, 0xff8080, "我測試1");
layer.addDataSet(data1, 0x008080, "你也測2");
//3d化
layer.set3D();
//設置BAR邊框形式
layer.setBarShape(0);
//bar寬度
layer.setBarWidth(50);
//設置BAR邊框顏色
//layer.setBorderColor(0xff9999);
//圖例形式
layer.setLegend(1);
//每個BAR頂部加入數據顯示
layer.setAggregateLabelStyle();
//設置BAR底部的名稱顯示
TextBox t = c.xAxis().setLabels(labels);
//名稱文字大小
t.setFontSize(9);
//加圖例
//LegendBox legend = c.addLegend(260, 120,true);
//legend.addKey("錢財",0xff8080);
//圖例位置
c.addLegend(450, 120,true);
//output the chart
String chart1URL = c.make
session(request, "chart1");
//include tool t
ip for the chart
String imageMap1 = c.getHTMLImageMap("#", "", "title='{xLabel}: US${value}K'");
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>圖表測試</title>
</head>
<body>
<h1>中文</h1>
<hr color="#000080">
<br>
<img src='<%=response.encodeURL("getchart.jsp?"+chart1URL)%>'
usemap="#map1" border="0">
<map name="map1"><%=imageMap1%></map>
</body>
</html>