JFreeChart使用及亂碼解決及整合struts2

<%@ page language="java" import="org.jfree.chart.ChartFactory, org.jfree.chart.JFreeChart, org.jfree.chart.plot.PlotOrientation,org.jfree.chart.servlet.ServletUtilities,
org.jfree.data.category.DefaultCategoryDataset,org.jfree.data.category.*,org.jfree.chart.axis.CategoryAxis,
org.jfree.chart.axis.ValueAxis,
org.jfree.chart.plot.CategoryPlot,
org.jfree.chart.title.TextTitle,java.awt.*,org.jfree.chart.title.LegendTitle,org.jfree.chart.axis.NumberAxis" contentType="text/html;charset=GB18030" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'jfreechartJsptest.jsp' starting page</title>
      </head>
  <body>
   <%
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 dataset.addValue(610, "廣州", "豬肉");
 dataset.addValue(220, "廣州", "牛肉");
 dataset.addValue(530, "廣州", "雞肉");
 dataset.addValue(340, "廣州", "魚肉");
 JFreeChart chart = ChartFactory.createBarChart3D("肉類銷量統計圖",
                   "肉類",
                   "銷量",
                   dataset,
                   PlotOrientation.VERTICAL,
                   false,
                   false,
                   false);
                   CategoryPlot plot = chart.getCategoryPlot();
   CategoryAxis domainAxis = plot.getDomainAxis();//x軸
  
    NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();    
  
   //ValueAxis numberaxis = plot.getRangeAxis(); //y軸
 
  TextTitle t = chart.getTitle();
   t.setFont(new Font("宋體",Font.BOLD,40));//標題文字
 
  domainAxis.setLabelFont(new Font("宋體",Font.BOLD,20));//x軸標題文字
  domainAxis.setTickLabelFont(new Font("黑體",Font.BOLD,10));//x軸座標上文字
  numberaxis.setLabelFont(new Font("宋體",Font.BOLD,20));//y軸標題文字
  numberaxis.setTickLabelFont(new Font("黑體",Font.BOLD,10));//y軸座標上文字
 // LegendTitle title=chart.getLegend();
 
// title.setItemFont(new Font("黑體",Font.BOLD,5));//圖例文字
 String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,null,session);
 String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
 %>
  <img src="<%= graphURL %>"width=500 height=300 border=0 usemap="#<%= filename %>">
  </body>
</html>
 
 
附:標題亂碼
Java代碼  
  1. chart.getTitle().setFont(new Font("宋體", Font.BOLD,12));  


其他

Java代碼  
  1.  1. CategoryAxis domainAxis = plot.getDomainAxis();     
  2.  2.   
  3.  3// NumberAxis  valueAxis=(NumberAxis) plot.getRangeAxis();    
  4.  4//有人說這個是水平方向設置的 方法。   
  5.  5. ValueAxis numberaxis = plot.getRangeAxis();   
  6.  6.      
  7.  7/*------設置X軸座標上的文字-----------*/     
  8.  8. domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));      
  9.  9.      
  10. 10/*------設置X軸的標題文字------------*/     
  11. 11. domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12));      
  12. 12.      
  13. 13/*------設置Y軸座標上的文字-----------*/     
  14. 14. numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));      
  15. 15.      
  16. 16/*------設置Y軸的標題文字------------*/     
  17. 17. numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));      
  18. 18.      
  19. 19/*------這句代碼解決了底部漢字亂碼的問題-----------*/     
  20. 20. jfreechart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12));    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章