jfreechart 多座標軸組合圖
最近做個報表專案,要用圖表來明確直觀的看出資料變化的趨勢,網上一搜jfreechart很好用,但網上都是些簡單的用法。因為經理要求要用多座標軸,而在網上又很少找到相關的例子,沒辦法呀,說真的一開始,真是一頭霧水,只有英文版的操作手冊,對我這個英文很爛的人來說,真是太難了,但是領導的話,不能不聽呀!O(∩_∩)O哈哈~,只好硬著頭皮往下看咯。
好了,廢話少說,下面進入正題!
本文主要介紹下CombinedDomainCategoryPlot類在組合分類軸時的例子,當然也可以用CombinedRangeCategoryPlot類來組合數值軸。
首先要匯入相關的類,核心程式碼如下:
<%
DefaultCategoryDataset dataset0 =new DefaultCategoryDataset();
if(rsArrayList.size()>0 && rsArrayList!=null)
{
for(int i= 0; i < rsArrayList.size(); i++)
{
java.util.Map map = (java.util.HashMap) rsArrayList.get(i);
String swjg_mc = map.get("SWJG_MC").toString();
String swjgmc=(swjg_mc.replaceAll("市國家稅務局","")).replaceAll("安徽省","");
String BYLSBL_ZT=(map.get("BYLSBL_ZT").toString());
String LJLSBL_YB=(map.get("LJLSBL_YB").toString());
String LJLSBL_XG=(map.get("LJLSBL_XG").toString());
dataset0.addValue(Double.parseDouble(BYLSBL_ZT),"藍色預警比例",swjgmc);
dataset0.addValue(Double.parseDouble(LJLSBL_YB),"黃色預警比例",swjgmc);
dataset0.addValue(Double.parseDouble(LJLSBL_XG),"紅色預警比例",swjgmc);
}
}
NumberAxis numberAxis0 = new NumberAxis("合肥市");
numberAxis0.setStandardTickUnits(NumberAxis.createStandardTickUnits());
//numberAxis0.setAutoRangeIncludesZero(false);
//設定數值軸的最小值
numberAxis0.setLowerBound(0);
//設定數值軸的最大值
numberAxis0.setUpperBound(1.0);
//numberAxis0.setAutoRangeMinimumSize(0.1d);
//BarRenderer3D renderer0 = new BarRenderer3D();
StackedBarRenderer3D renderer0 = new StackedBarRenderer3D();
//設定每種水果代表的柱的顏色
renderer0.setSeriesPaint(2, Color.red);
renderer0.setSeriesPaint(1, Color.yellow);
renderer0.setSeriesPaint(0, Color.blue);
renderer0.setItemMargin(0.2);
//設定當滑鼠放到相應顏色的柱上面,顯示相應的詳細資訊
//renderer0.setBaseToolTipGenerator(new StandardCategoryItemLabelGenerator());
CategoryPlot subplot0 = new CategoryPlot(dataset0, null, numberAxis0, renderer0);
subplot0.setDomainGridlinesVisible(true);
NumberAxis numberAxis1 = new NumberAxis("蕪湖市");
BarRenderer3D renderer1 = new BarRenderer3D();
//設定每種水果代表的柱的顏色
renderer1.setSeriesPaint(0, Color.red);
renderer1.setSeriesPaint(1, Color.yellow);
renderer1.setSeriesPaint(2, Color.blue);
renderer1.setItemMargin(0.1);
CategoryPlot subplot1 = new CategoryPlot(dataset0, null, numberAxis1, renderer1);
subplot0.setDomainGridlinesVisible(true);
CategoryAxis domainAxis = new CategoryAxis("所有行業");
CombinedDomainCategoryPlot combinedcategoryplot = new CombinedDomainCategoryPlot(domainAxis);
combinedcategoryplot.add(subplot0, 2);
combinedcategoryplot.add(subplot1, 2);
combinedcategoryplot.setOrientation(PlotOrientation.HORIZONTAL);
//java docs says JFreeChart(java.lang.String title, java.awt.Font titleFont, Plot plot, boolean createLegend)
JFreeChart jfreechart = new JFreeChart("Combined Range Category Plot Demo", new Font("SansSerif", 1, 12), combinedcategoryplot, false);
//---------combinedjfreechart--end--
//設定URL的連結
CategoryURLGenerator urls = new StandardCategoryURLGenerator("left.jsp&swjg_dm=");
renderer0.setItemURLGenerator(urls);
renderer1.setItemURLGenerator(urls);
//頁面根據dataset的值生成相應的值
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(out);//輸出MAP資訊
int picWidth = tasks.size()*30;
String filename ="";
if(tasks.size()<10){
//filename =ServletUtilities.saveChartAsPNG(chart, 500, 400, info, session);
filename =ServletUtilities.saveChartAsPNG(jfreechart, 500, 400, info, session);
}else{
if(picWidth>=500){
filename =ServletUtilities.saveChartAsPNG(jfreechart, picWidth, 400, info, session);
}else{
filename =ServletUtilities.saveChartAsPNG(jfreechart, 500, 400, info, session);
}
}
org.jfree.chart.ChartUtilities.writeImageMap(w, "map0", info, false);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
<%if(tasks.size()<10){%>
<img src="<%=graphURL %>" width=500 height=400 border=0 usemap="#map0">
<%}else{
if(picWidth>=500){
%>
<img src="<%=graphURL %>" width=<%=picWidth%> height=400 border=0 usemap="#map0">
<%}else{%>
<img src="<%=graphURL %>" width=500 height=400 border=0 usemap="#map0">
<%}}%>
至於,一些環境方面的設定,網上已經有很多,這裡就不多說了!