chart图之jfreechart

本文介绍如何使用JFreeChart库在Java中创建3D柱状图,并详细展示了配置图表样式、添加交互提示等功能的具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前不久了解了下有关chart图方面的,首先对于JAVA的来说,比较好的应该是jfreechart了。至于具体介绍,我就不对此做介绍了。这里我做了两个小的例子。分别是柱状图 具体就作为参考吧。网上关于jfreechart的资料很多。
代码如下:



HttpSession session = request.getSession();
response.setContentType("image/jpeg");
request.setCharacterEncoding("UTF-8");
CreateDate creat = new CreateDate();
CategoryDataset dateSet= creat.getDataSet();
JFreeChart chart = ChartFactory.createBarChart3D("水果销量图统计", null, null, dateSet, PlotOrientation.VERTICAL,true,true,true);

CategoryPlot plot = chart.getCategoryPlot();
BarRenderer3D renderer = new BarRenderer3D();
//设置图表的纵轴和横轴org.jfree.chart.axis.CategoryAxis
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLowerMargin(0.1);//设置距离图片左端距离此时为10%
domainAxis.setUpperMargin(0.1);//设置距离图片右端距离此时为百分之10
domainAxis.setCategoryLabelPositionOffset(10);//图表横轴与标签的距离(10像素)
domainAxis.setCategoryMargin(0.2);//横轴标签之间的距离20%
//设定柱子的属性
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setUpperMargin(0.1);//设置最高的一个柱与图片顶端的距离(最高柱的10%)
renderer.setBaseOutlinePaint(Color.red);
renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());//显示鼠标提示

renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
renderer.setBaseItemLabelsVisible(true);







renderer.setSeriesPaint(0, new Color(0, 255, 255));//计划柱子的颜色为青色
renderer.setSeriesOutlinePaint(0,Color.green);//边框为黑色
renderer.setSeriesPaint(1, new Color(0, 255, 0));//实报柱子的颜色为绿色
renderer.setSeriesOutlinePaint(1,Color.red);//边框为红色
renderer.setItemMargin(0.1);//组内柱子间隔为组宽的10%
//显示每个柱的数值,并修改该数值的字体属性

domainAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置Y轴坐标上的文字-----------*/
rangeAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置Y轴的标题文字------------*/
rangeAxis.setLabelFont(new Font("黑体",Font.PLAIN,15));


renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelFont(new Font("黑体",Font.BOLD,12));//12号黑体加粗
renderer.setItemLabelPaint(Color.black);//字体为黑色
renderer.setItemLabelsVisible(true);



plot.setRenderer(renderer);//使用我们设计的效果
//设置纵横坐标的显示位置
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);//学校显示在下端(柱子竖直)或左侧(柱子水平)
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); //人数显示在下端(柱子水平)或左侧(柱子竖直)

TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
chart.setTitle(textTitle);
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
//设置热点提示



Shape shape = new Rectangle(20, 10);
ChartEntity entity = new ChartEntity(shape);
StandardEntityCollection coll = new StandardEntityCollection();
coll.add(entity);
int width = 500;
int height = 300;



ChartRenderingInfo info = new ChartRenderingInfo(coll);
PrintWriter pw = response.getWriter();
//写入到输出流生成图像文件,同时把图片的具体信息放入ChartRenderingInfo的一个实例为以后生成Map提供信息
// ChartUtilities.saveChartAsPNG(pw, chart, width, height, info);


String filename = ServletUtilities.saveChartAsPNG(chart, width , height, info, session);
ChartUtilities.writeImageMap(pw, filename, info, false);


String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;

String strimg= ChartUtilities.getImageMap("map0", info);


request.setAttribute("strimg", strimg);
System.out.println(request.getAttribute("strimg"));
request.setAttribute("graphURL", graphURL);

RequestDispatcher dispatcher = request.getRequestDispatcher("/mobile2.jsp");
dispatcher .forward(request, response);

当然这里你需要有个xml配置 也就是上面设计的 graphURL 部分
配置如下
  <servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>

JSP页面需
  <%=request.getAttribute("strimg") %>// 页面必须要有这个才能实现当//你鼠标放到图上显示信息,也就是所谓的热点提示
<p><img src="${graphURL }" width="500" height="300" border="0" usemap="#map0"> </p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值