用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/>" );
问题二、如何设置图片固定分数值?现在默认是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/>" );
解决方案 »
- itext 生成 rtf时如何设置单元格背景图片
- 求教webservice客户端调用问题,在线等答案。
- 空指针异常要不要抛?
- 请求一个特效实现。大家帮帮忙
- 上传图片和rm用FileInputStream好,还是FileReader好写??
- MVC编程模式是什么意思呀?
- weblogic下运行servlet问题
- 从 Ms Sql Server 2000 中读出 text类型的数据,最好有源程序 (在线等待) ,我是用jsp
- Apache+Tomcat+JServer+JDK在XP下,行吗?
- 请问weblogic5.1如何配置与Oracle数据库通讯(重分恳请高手帮助)??
- jstl String转long
- 自定义标签和s:iterator标签
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));
//设置柱的颜色
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);
}
}