为什么JAVA里没有图表提问区呢,问个jfreechart的问题

kong_d_f 2010-06-24 03:02:51
用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/>" );
...全文
114 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mzry262 2010-07-23
  • 打赏
  • 举报
回复
我也刚刚做了按照颜色演示柱,一下是代码,希望对你有帮助

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);
}
}
kong_d_f 2010-06-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 stl0 的回复:]
sorry,忘记你是横向的图了,补充下

把这句

Java code

ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_RIGHT);


改成下句即可

Java code

ItemLabelPosition posit……
[/Quote]

谢谢热心的帮助~~
现在客户提出了新的要求
要求80分以上显示绿色
60~80显示蓝色
60以下显示红色
这个有点郁闷,该从哪里设置颜色啊。。。。。。。
stl0 2010-06-24
  • 打赏
  • 举报
回复
sorry,忘记你是横向的图了,补充下

把这句

ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_RIGHT);

改成下句即可

ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.BASELINE_RIGHT);
xql80329 2010-06-24
  • 打赏
  • 举报
回复
顶 1楼
stl0 2010-06-24
  • 打赏
  • 举报
回复
设置横轴范围
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));

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧