JFreeChart生成柱状图 jfreechart-0.9.16.jar

本文介绍了一个使用Java和JFreeChart库绘制3D柱状图的具体示例,展示了如何通过设置数据集、图表样式及参数来生成一张展示不同水果在多个城市的销量对比图表。

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

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.awt.Color,
                java.awt.Font,
                org.jfree.chart.ChartFactory,
                org.jfree.chart.JFreeChart,
                org.jfree.chart.plot.PlotOrientation,
                org.jfree.chart.servlet.ServletUtilities,
                org.jfree.data.CategoryDataset,
                org.jfree.data.DatasetUtilities,
                org.jfree.chart.plot.CategoryPlot,
                org.jfree.chart.axis.CategoryAxis,
                org.jfree.chart.axis.ValueAxis,
                org.jfree.chart.renderer.BarRenderer3D,
                org.jfree.chart.labels.StandardCategoryItemLabelGenerator,
                org.jfree.chart.axis.AxisLocation"%>
<%
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1832179689702023&dt=1167009113140&lmt=1167009113&format=250x250_as&output=html&url=http%3A%2F%2Fwww.java3z.com%2Fcwbwebhome%2Farticle%2Farticle5a%2F556.jsp%3Fid%3D121&color_bg=FFFFFF&color_text=000000&color_link=3366FF&color_url=3D81EE&color_border=FFFFFF&ad_type=text_image&ref=http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3DJFreeChart%2520%25E6%259F%25B1%25E7%258A%25B6%25E5%259B%25BE%26hl%3Dzh-CN%26lr%3D%26nxpt%3D20.53998530584467959101&cc=2993&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="250" scrolling="no" height="250" allowtransparency="allowtransparency">
??
  double[][] data = new double[][] {{672, 766, 223, 540, 126},{325, 521, 210, 340, 106},{332, 256, 523, 240, 526}};
  String[] rowKeys = {"苹果","梨子","葡萄"};
  String[] columnKeys = {"北京","上海","广州","成都","深圳"};
  CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);

  JFreeChart chart = ChartFactory.createBarChart3D("水果销量图统计",
                  null,
                  null,
                  dataset,
                  PlotOrientation.VERTICAL,
                  true,false,false);
  chart.setBackgroundPaint(Color.WHITE);
  CategoryPlot plot = chart.getCategoryPlot();

  CategoryAxis domainAxis = plot.getDomainAxis();
  domainAxis.setVerticalCategoryLabels(false);
  plot.setDomainAxis(domainAxis);

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

  BarRenderer3D renderer = new BarRenderer3D();
  renderer.setBaseOutlinePaint(Color.BLACK);
  //设置 Wall 的颜色
  renderer.setWallPaint(Color.gray);
  //设置每种水果代表的柱的颜色
  renderer.setSeriesPaint(0, new Color(0, 0, 255));
  renderer.setSeriesPaint(1, new Color(0, 100, 255));
  renderer.setSeriesPaint(2, Color.GREEN);
  //设置每个地区所包含的平行柱的之间距离
  renderer.setItemMargin(0.1);
  //显示每个柱的数值,并修改该数值的字体属性
  renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
  renderer.setItemLabelsVisible(true);
  plot.setRenderer(renderer);

  //设置柱的透明度
  plot.setForegroundAlpha(0.6f);
  //设置地区、销量的显示位置
  plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
  plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

  String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
  String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>"> 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值