利用java生成柱状图

本文介绍了如何使用Java的JFreeChart库生成3D柱状图,包括设置图表标题、横纵轴标签、数据集,并将图表导出为JPG格式。示例代码展示了创建柱状图的过程,数据包括葡萄、柚子、苹果和梨子的销售量。
摘要由CSDN通过智能技术生成

在本人博客中将会列出柱状图、饼图、动态曲线图、静态曲线图、折线图,所有的代码都是参照于网上,在加上一些个人的注释,希望能过对大家有所帮助。

 

所有图片都是利用JFreeChart来生成,所以我们首先需要下载JFreeChart包(建议大家使用JFreeChart-1.0.9,我在使用JFreeChart-1.0.11版本的时候,一直出现IO异常)

下载地址:http://download.csdn.net/source/337198

 

此篇为柱状图:

 

  1. import java.awt.Font;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import org.jfree.chart.ChartFactory;
  5. import org.jfree.chart.ChartUtilities;
  6. import org.jfree.chart.JFreeChart;
  7. import org.jfree.chart.axis.CategoryAxis;
  8. import org.jfree.chart.axis.CategoryLabelPositions;
  9. import org.jfree.chart.axis.NumberAxis;
  10. import org.jfree.chart.plot.CategoryPlot;
  11. import org.jfree.chart.plot.PlotOrientation;
  12. import org.jfree.chart.title.TextTitle;
好的,以下是利用 JSP 和 CSS 生成一个简单的柱状图的示例代码: 1. 首先创建一个 JSP 页面,比如叫做 chart.jsp: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>柱状图示例</title> <style type="text/css"> .chart { width: 400px; height: 200px; border: 1px solid black; padding: 10px; box-sizing: border-box; } .bar { height: 100%; width: 20%; background-color: blue; margin-right: 5px; display: inline-block; vertical-align: bottom; box-sizing: border-box; } .label { display: inline-block; text-align: center; width: 20%; box-sizing: border-box; } </style> </head> <body> <div class="chart"> <div class="bar" style="height: 50%;"></div> <div class="bar" style="height: 80%;"></div> <div class="bar" style="height: 30%;"></div> <div class="bar" style="height: 60%;"></div> <div class="bar" style="height: 90%;"></div> <div class="label">1</div> <div class="label">2</div> <div class="label">3</div> <div class="label">4</div> <div class="label">5</div> </div> </body> </html> ``` 2. 在 CSS 样式中定义柱状图的样式,包括容器和柱子的样式。其中,容器的大小为 400x200 像素,每个柱子的宽度为 20%,间距为 5px。柱子的高度通过内联样式进行设置。 3. 在 HTML 中,使用 div 标签来创建柱状图的容器和柱子。柱子的高度通过内联样式来设置,柱子和标签使用 display: inline-block; 和 vertical-align: bottom; 来实现垂直对齐。 4. 在柱子下方添加标签,用于显示柱子所代表的数据。 运行代码后,你会看到一个简单的柱状图,其中每个柱子的高度和标签对应数据相关联。你可以根据实际需求修改柱子的数量、高度和标签的内容。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值