JFreeChart应用(生成折线图)
2024-09-08 04:46:44
1.jar包,jcommon.jar和jfreechart.jar,具体用哪个版本官网去down吧;
还有另外一个jar包,gnujaxp.jar,这个引入之后编译的时候会报错,应该是xsd校验的问题,索性直接去掉了,不影响实现。
2.具体实现:
public static void main(String [] args){
//数据源
String[] rk = getRowKeys(bid);
double[][]data = getData(bid,rk);
String[] colKeys = getColKey();
// 准备数据
CategoryDataset dataset = createDataset(rk, data,colKeys );
// 生成JFreeChart对象
JFreeChart freeChart = createChart(dataset);
// 输出图片到浏览器
response.setContentType("image/jpeg");
OutputStream ops = response.getOutputStream();
ChartUtilities.writeChartAsJPEG(ops, freeChart, 640, 480);
ops.close();
//保存图片到本地
saveAsFile(freeChart, "yourPath", 600, 400); }
private String[] getRowKeys(byte[] bid) throws PicException {
//实现
}
private double[][] getData(byte[] bid,String [] rk){
//实现
}
private String[] getColKey() {
//实现
}
public static CategoryDataset createDataset(String[] rowKeys, double[][] data,String[] colKeys) { return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data);
}
// 保存为文件
public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) {
FileOutputStream out = null;
try {
File outFile = new File(outputPath);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
out = new FileOutputStream(outputPath);
// 保存为PNG
//ChartUtilities.writeChartAsPNG(out, chart, 600, 400);
// 保存为JPEG
ChartUtilities.writeChartAsJPEG(out, chart, 600, 400); out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// do nothing
}
}
}
}
// 根据CategoryDataset创建JFreeChart对象
public static JFreeChart createChart(CategoryDataset categoryDataset) {
// 创建JFreeChart对象:ChartFactory.createLineChart
JFreeChart jfreechart = ChartFactory.createLineChart("当年每月排放总量", // 标题
"月份", // categoryAxisLabel (category轴,横轴,X轴标签)
"数值", // valueAxisLabel(value轴,纵轴,Y轴的标签)
categoryDataset, // dataset
PlotOrientation.VERTICAL, true, // legend
false, // tooltips
false); // URLs
// 使用CategoryPlot设置各种参数。以下设置可以省略。
//设置字体解决乱码问题
CategoryPlot plot = (CategoryPlot) jfreechart.getPlot();
jfreechart.getTitle().setFont(new Font("微软雅黑", Font.BOLD, 18));
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setLabelFont(new Font("微软雅黑", Font.BOLD, 18)); CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(new Font("微软雅黑", Font.BOLD, 18)); // 背景色 透明度
plot.setBackgroundAlpha(0.5f);
// 前景色 透明度
plot.setForegroundAlpha(0.5f);
// 其他设置 参考 CategoryPlot类 return jfreechart;
}
最新文章
- 转载:《TypeScript 中文入门教程》 6、命名空间
- PHP文件包含漏洞攻防实战(allow_url_fopen、open_basedir)
- python UnicodeDecodeError: 'ascii' codec can't decode byte 0xa6 in position 907: ordinal not in range(128)
- ECMAScript 6 入门
- iOS项目开发知识点
- 实现js中的重载
- web sevice 生成代理类及使用
- 转Spring+Hibernate+EHcache配置(二)
- C#中的OLEDB连接2
- SpringCloud(8)---zuul权限校验、接口限流
- SQL Server安全
- Django学习手册 - pycharm 安装/建立第一个网站hello world
- laravel的csrf token 的了解及使用
- docker原理与上帝进程
- bzoj 4358 Permu - 莫队算法 - 链表
- 转载 React.createClass 对决 extends React.Component
- taro 知识点
- Django具体操作(六)
- Tomcat服务器的安装配置图文教程(推荐)
- 开源WebGIS实施方案(六):空间数据(PostGIS)与GeoServer服务迁移
热门文章
- Android 网络调试 adb tcpip 开启方法
- [Android6.0][RK3399] 电池系统(三)电量计 CW2015 驱动流程分析【转】
- JFreeChart教程(一)(转)
- 并不对劲的bzoj4560:p3269:[JLOI2016]字符串覆盖
- 异或运算(2014西安网络赛H题)
- VS2013文件同步插件开发
- form表单提交的几种方法
- bzoj 5281: [Usaco2018 Open]Talent Show【dp】
- bzoj 1625: [Usaco2007 Dec]宝石手镯【背包】
- 实现简单版的LinkedList