关于JFreechart柱状图 柱上不能显示数值的问题

关于JFreechart柱状图 柱上不能显示数值的问题

今天用jfreechart做图,发现生产的柱状图无法显示数值,经过一段时间摸索终于解决,现给一demo如下:

test.jsp

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="org.jfree.chart.*"%>
<%@ page import="org.jfree.chart.axis.AxisLocation"%>
<%@ page import="org.jfree.chart.plot.*"%>
<%@ page import="org.jfree.data.*"%>
<%@ page import ="java.text.DecimalFormat"%>
<%@ page import ="java.text.NumberFormat"%>
<%@ page import ="java.awt.Color"%>
<%@ page import="java.awt.Font"%>
<%@ page import="org.jfree.chart.renderer.category.*"%>
<%@ page import="org.jfree.chart.axis.*"%>
<%@ page import="org.jfree.chart.title.TextTitle"%>
<%@ page import="org.jfree.chart.labels.*"%>
<%@page import="org.jfree.data.category.*"%>
<%@page import="org.jfree.chart.plot.PlotOrientation"%>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%
CategoryDataset dcd= getDataset();


JFreeChart chart= ChartFactory.createStackedBarChart3D("各部门参加培训情况", "部门", "人数", dcd, PlotOrientation.VERTICAL, true, false, false);

// 图例字体清晰
//chart.setTextAntiAlias(false);
//chart.setBackgroundPaint(Color.WHITE);
// 2 .2 主标题对象 主标题对象是 TextTitle 类型
//chart .setTitle(new TextTitle("ok", new Font("隶书", Font.BOLD, 25)));
// 2 .2.1:设置中文 // x,y轴坐标字体
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
// 2 .3 Plot 对象 Plot 对象是图形的绘制结构对象
CategoryPlot plot = (CategoryPlot)chart.getPlot();  

ValueAxis rangeAxis = plot.getRangeAxis();
//设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
//设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);

StackedBarRenderer3D renderer=new StackedBarRenderer3D();
//renderer.setBaseOutlinePaint(Color.BLACK);

//设置 Wall 的颜色
//renderer.setWallPaint(Color.gray);

//设置每种柱的颜色
renderer.setSeriesPaint(0, new Color(153, 153, 255));
renderer.setSeriesPaint(1, new Color(204, 255, 255));
renderer.setSeriesPaint(2, Color.GREEN);

//显示每个柱的数值,并修改该数值的字体属性
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelFont(new Font("黑体",Font.PLAIN,9));
renderer.setItemLabelsVisible(true);

plot.setRenderer(renderer);

 

String filename=ServletUtilities.saveChartAsPNG(chart,500,300,null,session);
String url=request.getContextPath()+"/servletDisplayChart?filename="+filename;

%>
<p align="center">
 <img src="<%=url%>" width="500" height="300" border="0" usemap="#map0">
</p>
<%!

  private static CategoryDataset getDataset()
    {
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        defaultcategorydataset.addValue(10.399999999999999D, "培训人数", "中心机关");
        defaultcategorydataset.addValue(10.800000000000001D, "未培训人数", "中心机关");
        defaultcategorydataset.addValue(43.200000000000003D, "培训人数", "物探部");
        defaultcategorydataset.addValue(15.6D, "未培训人数", "物探部");
        defaultcategorydataset.addValue(23D, "培训人数", "遥感部");
        defaultcategorydataset.addValue(11.300000000000001D, "未培训人数", "遥感部");
        defaultcategorydataset.addValue(13D, "培训人数", "物业部");
        defaultcategorydataset.addValue(11.800000000000001D, "未培训人数", "物业部");
  defaultcategorydataset.addValue(15, "培训人数", "企业部");
        defaultcategorydataset.addValue(12D, "未培训人数", "企业部");
        return defaultcategorydataset;
    }

%>

你可能感兴趣的:(关于JFreechart柱状图 柱上不能显示数值的问题)