用jfreechart做了个横向的柱状图问题一、我怎么才能把图片上每根柱上的文字在柱的最顶端(右端)显示,数字与上一根柱不重合就成。
问题二、如何设置图片固定分数值?现在默认是8.0,我要设置成100分
response.setContentType("text/html;charset=gbk"); 
String[] bgcolor = new String[2];
bgcolor[0]="F9F9F9";
bgcolor[1]="F0F0F0";
String deptname = request.getParameter("deptname");
session.setAttribute("deptname",deptname);
deptname = new String(deptname.getBytes("iso-8859-1"),"utf-8");
//tool.Debug("deptname=="+deptname);

JxManager manager = new JxManager(db) ;
HashMap param = new HashMap();
param.put("deptname",deptname);
param.put("sdate",(String)session.getAttribute("sdate"));
//得到公司信息
HashMap[] hmDept = manager.getDeptInfo(param);


if (hmDept!=null){
for(int i=0;i<hmDept.length;i++){
HashMap[] hmSys = manager.getSysInfo((String)hmDept[i].get("deptid"));
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
if(hmSys!=null){
for(int j=0;j<hmSys.length;j++){
//dataset.addValue(Double.parseDouble(hmSys[j].get("quanzhong").toString()),"应得分",hmSys[j].get("sname").toString());
dataset.addValue(Double.parseDouble(hmSys[j].get("sysfen").toString()),"实得分",hmSys[j].get("sname").toString());
}
}
JFreeChart chart = ChartFactory.createBarChart(hmDept[i].get("deptname").toString()+"得分情况",
  "系统名称",
  "分数",
  dataset,
  PlotOrientation.HORIZONTAL,
  true,false,false);
chart.setAntiAlias(true);
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderPaint(Color.BLACK);
chart.setBorderVisible(false);
chart.getLegend().setPosition(org.jfree.ui.RectangleEdge.RIGHT);  
chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,12));
chart.setTitle(new TextTitle(hmDept[i].get("deptname").toString()+"得分情况",new Font("宋体",Font.BOLD,18)));
//图表区域对象,基本上这个对象决定着什么样式的图表,创建该对象的时候需要Axis、Renderer以及数据集对象的支持
CategoryPlot plot = chart.getCategoryPlot();
plot.setDomainGridlinePaint(Color.RED); //横坐标网格线白色
plot.setDomainGridlinesVisible(true); //可见
//plot.setBackgroundPaint(Color.CYAN); //用于处理图表的两个轴:纵轴和横轴
CategoryAxis axis = plot.getDomainAxis();
axis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
//axis.setUpperMargin(0.01);//与图片顶端的距离
//axis.setLowerMargin(0.02);//与图片底端的距离


axis.setAxisLinePaint(Color.RED);
axis.setTickMarksVisible(true);
axis.setAxisLineVisible(true);
axis.setLabelFont(new Font("宋体",Font.BOLD,14));
axis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); NumberAxis numberaxis = (NumberAxis)plot.getRangeAxis();
numberaxis.setAutoTickUnitSelection(true);
numberaxis.setAutoRangeIncludesZero(false); 
numberaxis.setLabelFont(new Font("宋体",Font.BOLD,14));
//numberaxis.setLowerBound(0);
//numberaxis.setUpperBound(100);

BarRenderer renderer = new BarRenderer(); renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
renderer.setBaseOutlinePaint(Color.BLACK);
//renderer.setItemLabelFont(new Font("黑体",Font.BOLD,12)); //设置 Wall 的颜色
//renderer.setWallPaint(Color.GREEN);
//设置每种柱的颜色
//renderer.setSeriesPaint(0, new Color(0, 0, 255));
//renderer.setSeriesPaint(1, new Color(0, 100, 255));
//设置平行柱之间距离
renderer.setItemMargin(0.02);

renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("showjxtj.jsp", "", "sysname"));
//显示每个柱的数值,并修改该数值的字体属性
//renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
//renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer); //设置柱的透明度
plot.setForegroundAlpha(0.7f);
//设置地区、销量的显示位置
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); 

StandardEntityCollection entityCollection = new StandardEntityCollection();   
ChartRenderingInfo info = new ChartRenderingInfo(entityCollection);    

PrintWriter pw = response.getWriter();  

int syssum = manager.getSysSum(deptname);
String filename = ServletUtilities.saveChartAsPNG(chart, 720, syssum*35, info, session);   
ChartUtilities.writeImageMap(pw, filename, info, false);   
String p1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; 

out.println("<img src=\""+p1+"\" width=\"720\"  height=\""+syssum*35+"\" usemap=\"#"+filename+"\" border=\"0\"  /><br/><br/>" );

解决方案 »

  1.   

    设置横轴范围
    rangeAxis.setRange(0, 100);//在柱顶部显示值设置横轴范围BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseItemLabelPaint(Color.BLUE);ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_RIGHT);
    renderer.setBasePositiveItemLabelPosition(position);
    renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
      

  2.   

    sorry,忘记你是横向的图了,补充下把这句ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_RIGHT);改成下句即可ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.BASELINE_RIGHT);
      

  3.   

    我也刚刚做了按照颜色演示柱,一下是代码,希望对你有帮助 BarRenderer3D renderer = new BarRenderer3D();
                //设置柱的颜色
                for(int i=0;i<data2list.size();i++){
                 String[] array = (String[])data2list.get(i);
                 if(Float.parseFloat(array[1])/Float.parseFloat(array[2])>4.5){
                 renderer.setSeriesPaint(i, Color.GREEN);
                 }else if(Float.parseFloat(array[1])/Float.parseFloat(array[2])<4.5 &&Float.parseFloat(array[1])/Float.parseFloat(array[2])>3.5){
                 renderer.setSeriesPaint(i, Color.BLUE);
                 }else if(Float.parseFloat(array[1])/Float.parseFloat(array[2])<3.5 &&Float.parseFloat(array[1])/Float.parseFloat(array[2])>=3){
                 renderer.setSeriesPaint(i, Color.YELLOW);
                 }else if(Float.parseFloat(array[1])/Float.parseFloat(array[2])<3 ){
                 renderer.setSeriesPaint(i, Color.RED);
                 }
                }