Linux下 JFreeChart1.0.13 柱图 图例显示问题

来源:互联网 发布:淘宝9.9限时抢购在哪里 编辑:程序博客网 时间:2024/05/22 14:20

环境如下:

1、os:ubuntu10.10

2、开发工具:MyEclipse9.0M1

3、jfreechart版本:1.0.13linux版

4、需要jar包:jfreechart-1.0.13.jar;jcommon-1.0.16.jar

5、更新系统字库,并使之生效。

 

测试代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="org.jfree.chart.ChartFactory,
 org.jfree.chart.JFreeChart,
 org.jfree.chart.plot.PlotOrientation,
 org.jfree.chart.servlet.ServletUtilities,
 org.jfree.data.category.CategoryDataset,
 org.jfree.data.general.DatasetUtilities,
 java.awt.Font,
 java.awt.Color,
 org.jfree.chart.title.TextTitle,
 org.jfree.chart.title.LegendTitle,
 org.jfree.chart.plot.CategoryPlot,
 org.jfree.chart.axis.CategoryAxis,
 org.jfree.chart.axis.NumberAxis,
 org.jfree.chart.axis.AxisLocation,
 org.jfree.chart.renderer.category.BarRenderer3D,
 org.jfree.chart.labels.StandardCategoryItemLabelGenerator,
 org.jfree.chart.labels.ItemLabelPosition,
 org.jfree.chart.labels.ItemLabelAnchor,
 org.jfree.ui.TextAnchor" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
 <%
  double[][] data = new double[][] {{23,34,54},{57,23,34},{34,23,54},{34,56,23}};
  String[] rowKeys = {"苹果","香蕉","芒果","布朗"};
  String[] columnKeys = {"上海","北京","广州"};
  CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys,columnKeys,data);
  //参数含义分别是标题、横坐标、纵坐标、数据、图标方向(水平、垂直)、是否显示图例、是否生成工具、是否生成URL
  JFreeChart chart = ChartFactory.createBarChart3D("aaa","横坐标-水果","纵坐标-销量",dataset,PlotOrientation.VERTICAL,true,true,true);
  //--------------------------------------------------------处理乱码问题开始---------------------------------------------------------------
  Font font = new Font("宋体",Font.BOLD,16);
  TextTitle title = new TextTitle("水果销售情况",font);
  TextTitle subTitle = new TextTitle("副标题",new Font("宋体",Font.BOLD,14));
  List subList = new ArrayList();
  subList.add(subTitle);
  CategoryPlot plot = chart.getCategoryPlot();
  CategoryAxis domainAxis = plot.getDomainAxis();
  NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
  
  //标题
  chart.setTitle(title);
  //副标题
  chart.setSubtitles(subList);
  //x轴标题
  domainAxis.setLabelFont(new Font("宋体",Font.PLAIN,12));
  //x轴坐标文字
  domainAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,11));
  //y轴标题
  numberAxis.setLabelFont(new Font("宋体",Font.PLAIN,12));
  //y轴坐标文字
  numberAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,11));
  //底部文字乱码
  LegendTitle legendTitle = chart.getLegend();
  if(legendTitle!=null){
   legendTitle.setItemFont(new Font("宋体",Font.PLAIN,12));
  }else{
   LegendTitle legendTitleNew = new LegendTitle(chart.getPlot());
   legendTitleNew.setItemFont(new Font("宋体",Font.PLAIN,12));
   chart.addLegend(legendTitleNew);
  }
  //--------------------------------------------------------处理乱码问题结束---------------------------------------------------------------
  
  
  
  //--------------------------------------------------------图形样式修改开始---------------------------------------------------------------
  //网络背景色
  plot.setBackgroundPaint(Color.white);
  //网络竖线颜色
  plot.setDomainGridlinePaint(Color.pink);
  //网络横线颜色
  plot.setRangeGridlinePaint(Color.pink);
  //显示每个柱子数值及修改数值字体
  BarRenderer3D renderer3D = new BarRenderer3D();
  renderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
  renderer3D.setBaseItemLabelsVisible(true);
  renderer3D.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
  renderer3D.setItemLabelAnchorOffset(10D);
  renderer3D.setItemLabelFont(new Font("宋体",Font.PLAIN,12));
  renderer3D.setItemLabelsVisible(true);
  //每个平行区域柱子之间的距离
  renderer3D.setItemMargin(0.1);
  plot.setRenderer(renderer3D);
  //横坐标放到最上面
  plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
  //纵坐标放到最右边
  plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
  //--------------------------------------------------------图形样式修改结束---------------------------------------------------------------
  
  
  String filename = ServletUtilities.saveChartAsPNG(chart,500,300,null,session);
  String graphURL = path + "/DisplayChart?filename="+filename+"&temp="+Math.random();
 %>
 <img src="<%=graphURL %>" width="500" height="300" border="0" usemap="<%=filename %>">
  </body>
</html>