JFreeChart自定义拆线图
1.说明
现在市面上的画图工具很多,不过对于一个比较老的系统,还是会用到JFreeChart的工具。笔者第一次使用JFreeChart是在大学毕业设计中使用。那时候觉得这个框架还不错。可以画出柱状态图、折线图等。不过现在看到,觉得JFreeChart画出来的图不是非常好看。但是对于一个系统是面向B端来说,还是可以的。最近笔者又重新使用了一次JFreeChart来画图,对于这次的开发过程有几个点的地方比较好的,就在这个博客中总结一下。
- 1技术实现
2.1定义X轴以时间形式显示
更多时候,我们需要画出时时动态的折线图,而这个时时动态的图需要的是显示X轴为时间。所以,我们需要定义X轴的显示为时间,那么应该怎么定义呢?主要是使用DateAxis来实现。代码如下:
//对domain 轴上日期显示格式定义 DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis(); dateaxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
2.2X轴设置
有时候X轴如果是中文的话,我们要防止其乱码的出现,同时还要设置自己想要的字体。代码如下:
private static void setDomainAxis(ValueAxis domainAxis){ // 解决x轴坐标上中文乱码 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); // 解决x轴标题中文乱码 domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 14)); // 用于显示X轴刻度 domainAxis.setTickMarksVisible(true);// domainAxis.setLowerMargin(5);// 左边距 边框距离 // domainAxis.setUpperMargin(5);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。 }
2.3X轴的倾斜显示
当X轴的数据量很大时,我们需要倾斜显示文字,这样是可以节省空间。当然,有时候倾斜显示会让整个图看起来更加美观。所以倾斜设置代码如下:
//设置x轴坐标值斜着显示 DateAxis dateAxis = new DateAxis(" ") { @SuppressWarnings("unchecked") protected List<DateTick> refreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) { List ticks = super.refreshTicksHorizontal(g2, dataArea, edge); List<DateTick> newTicks = new ArrayList<DateTick>(); for (Iterator it = ticks.iterator(); it.hasNext();) { DateTick tick = (DateTick) it.next(); newTicks.add(new DateTick(tick.getDate(), tick.getText(), TextAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI/3)); } return newTicks; } }; xyplot.setDomainAxis(dateAxis);
2.4Y轴设置
Y轴设置时间间隔、乱码等问题。代码如下:
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // 是否显示零点 numberaxis.setAutoRangeIncludesZero(true); numberaxis.setAutoTickUnitSelection(false); // 解决Y轴标题中文乱码 numberaxis.setLabelFont(new Font("sans-serif", Font.PLAIN, 14)); NumberFormat numformatter = NumberFormat.getInstance();numformatter.setMaximumFractionDigits(2); // 设置数值小数点后最多2位 numformatter.setMinimumFractionDigits(2);numberaxis.setTickUnit(new NumberTickUnit(comMaxVlue/10,numformatter));// numberaxis.setTickUnit(new NumberTickUnit(comMaxVlue/10));//Y轴数据间隔 }
2.5自定义线条颜色
当我们的折线图轵有一条线的时候,且我们需要自己定义线条颜色,可以直接通过JFreeChart的接口来实现。代码如下:
//线条设置XYLineAndShapeRenderer xylinerenderer=(XYLineAndShapeRenderer)xyplot.getRenderer();xylinerenderer.setSeriesPaint(0, new Color(0,191,255));
2.6其它设置
默认的JFreeChart的框架画图是会含有内框的,同时背景也不是很看,为了解决这个问题,可以实现下面的设置:
XYPlot xyplot = (XYPlot)imgChart.getPlot(); xyplot.setBackgroundPaint(Color.white); xyplot.setDomainGridlinePaint(new Color(112, 128, 144)); xyplot.setRangeGridlinePaint(new Color(112, 128, 144)); xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); xyplot.setDomainCrosshairVisible(true); xyplot.setRangeCrosshairVisible(true);xyplot.setOutlineVisible(false);imgChart.setBorderVisible(false);
- 全部代码
3.1X轴设置
/** * 折线图--设置X轴 * @param domainAxis */ private static void setDomainAxis(ValueAxis domainAxis){ // 解决x轴坐标上中文乱码 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); // 解决x轴标题中文乱码 domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 14)); // 用于显示X轴刻度 domainAxis.setTickMarksVisible(true);// domainAxis.setLowerMargin(5);// 左边距 边框距离 // domainAxis.setUpperMargin(5);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。 }
3.2Y轴设置
/** * 折线图--设置Y轴 * @param numberAxis */ private static void setNumberAxis(NumberAxis numberaxis,Double comMaxVlue){ numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // 是否显示零点 numberaxis.setAutoRangeIncludesZero(true); numberaxis.setAutoTickUnitSelection(false); // 解决Y轴标题中文乱码 numberaxis.setLabelFont(new Font("sans-serif", Font.PLAIN, 14)); NumberFormat numformatter = NumberFormat.getInstance();numformatter.setMaximumFractionDigits(2); // 设置数值小数点后最多2位 numformatter.setMinimumFractionDigits(2);numberaxis.setTickUnit(new NumberTickUnit(comMaxVlue/10,numformatter));// numberaxis.setTickUnit(new NumberTickUnit(comMaxVlue/10));//Y轴数据间隔 }
3.3画图
/** * 画折线图* * @return */ public static String getJFreeChart(XYDataset dates, String cite,Double comMaxVlue,String imagePth,String resourceName,String key) { JFreeChart imgChart=null; // JFreeChart对象 参数:标题,目录轴显示标签,数值轴显示标签,数据集,是否显示图例,是否生成工具,是否生成URL连接 //平面 imgChart = ChartFactory.createTimeSeriesChart("", "", "", dates, false, true, true); imgChart.setBackgroundPaint(Color.white); imgChart.setBorderVisible(true);// 边框可见 TextTitle title = new TextTitle(cite, new Font("宋体", Font.BOLD, 20)); // 解决曲线图片标题中文乱码问题 imgChart.setTitle(title); //解决图表底部中文乱码问题 // imgChart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); //获得 plot : XYPlot!! XYPlot xyplot = (XYPlot)imgChart.getPlot(); xyplot.setBackgroundPaint(Color.white); xyplot.setDomainGridlinePaint(new Color(112, 128, 144)); xyplot.setRangeGridlinePaint(new Color(112, 128, 144)); xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); xyplot.setDomainCrosshairVisible(true); xyplot.setRangeCrosshairVisible(true);xyplot.setOutlineVisible(false);imgChart.setBorderVisible(false);//设置x轴坐标值斜着显示 DateAxis dateAxis = new DateAxis(" ") { @SuppressWarnings("unchecked") protected List<DateTick> refreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) { List ticks = super.refreshTicksHorizontal(g2, dataArea, edge); List<DateTick> newTicks = new ArrayList<DateTick>(); for (Iterator it = ticks.iterator(); it.hasNext();) { DateTick tick = (DateTick) it.next(); newTicks.add(new DateTick(tick.getDate(), tick.getText(), TextAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI/3)); } return newTicks; } }; xyplot.setDomainAxis(dateAxis); // Y轴 NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // setNumberAxis(numberaxis,comMaxVlue); // x轴 ValueAxis domainAxis = xyplot.getDomainAxis(); setDomainAxis(domainAxis); //线条设置XYLineAndShapeRenderer xylinerenderer=(XYLineAndShapeRenderer)xyplot.getRenderer();xylinerenderer.setSeriesPaint(0, new Color(0,191,255)); //对domain 轴上日期显示格式定义 DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis(); dateaxis.setDateFormatOverride(new SimpleDateFormat("HH:mm")); //保存图像的大小 int weight = 800; int height = 600; File resourceFile = new File(imagePth + File.separator+ resourceName+File.separator+key);if(!resourceFile.exists()){resourceFile.mkdirs();}String putImage = resourceFile.getPath() + File.separator+key+".png";//存放图像的目录 saveAsFile(imgChart, putImage, weight, height);return putImage; }
3.4保存
// 保存为图像文件 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, weight, height); out.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // do nothing } } } }
4.效果图
5.总结
这里主要是介绍JFreeChart的一些简单应用,调用其接口来设置自己想要的图片,使图片画出来更加清晰美观。
JFreeChart自定义拆线图相关推荐
- Python使用matplotlib函数subplot可视化多个不同颜色的折线图、自定义数据点的形状、自定义折线图的颜色
Python使用matplotlib函数subplot可视化多个不同颜色的折线图.自定义数据点的形状.自定义折线图的颜色 目录
- R语言使用ggradar包可视化基本雷达图(radar chart、蜘蛛图spider plot)、可视化单个数据对象的雷达图、自定义雷达图的线条类型、线条宽度、数据点大小、色彩等
R语言使用ggradar包可视化基本雷达图(radar chart.蜘蛛图spider plot).可视化单个数据对象的雷达图.自定义雷达图的线条类型.线条宽度.数据点大小.色彩等(Customize ...
- R语言ggplot2可视化:通过水平半小提琴图和抖动数据点可视化雨云图(Rain Cloud plots)、自定义雨云图中数据点的颜色(数据点的颜色和半小提琴图一致)
R语言ggplot2可视化:通过水平半小提琴图和抖动数据点可视化雨云图(Rain Cloud plots).自定义雨云图中数据点的颜色(数据点的颜色和半小提琴图一致) 目录
- R语言使用persp函数绘制三维图像实战(3D):自定义3D图、图像旋转、添加轴标签
R语言使用persp函数绘制三维图像实战(3D):自定义3D图.图像旋转.添加轴标签 目录
- Flutter 饼状图、柱状图、拆线图、Flutter动态饼图、Flutter图表 flutter_echart 开发文档
在码农的世界里,优美的应用体验,来源于程序员对细节的处理以及自我要求的境界,年轻人也是忙忙碌碌的码农中一员,每天.每周,都会留下一些脚印,就是这些创作的内容,有一种执着,就是不知为什么,如果你迷茫,不 ...
- power bi自定义地图_如何使用自定义形状图在Power BI中创建地理图
power bi自定义地图 介绍 (Introduction) This is the third article of a series dedicated to discovering geogr ...
- java jfreechart 折线图_java程序使用JfreeChart画折线图
一段简单的使用JfreeChart绘制折线图的java程序实现一个小例子. package org.ncut.decloud.linecharts; import javax.swing.JPanel ...
- Android 自定义折线图实现教程
前言: 各位同学大家好,有段时间没有给大家更新文章了,具体多久我也记不清楚了.最近重新复习了一下原生安卓的知识点,写了一个安卓原生自定义折线图的效果,就想着分享给大家.希望帮助到各位学习和工作,那份废 ...
- R语言patchwork包将多个ggplot2可视化结果组合起来、使用plot_annotation函数为组合图添加标题信息、使用theme函数自定义组合图中的字体类型(family)
R语言patchwork包将多个ggplot2可视化结果组合起来.使用plot_annotation函数为组合图添加标题信息.使用theme函数自定义组合图中的字体类型(family) 目录
- 百度地图瓦片 android,百度地图自定义瓦片图获取
nodejs代码 const request = require('request'); const fs = require('fs'); const bagpipe = require('bagp ...
最新文章
- 还应该如何实现“模板化函数指针”?
- C# 入门经典 第三版 下载。
- 小程序菜单栏吸顶效果
- android内部通信handler
- 什么是BNF EBNF 巴科斯范式及其扩展 BNF Augmented BNF
- 吴恩达机器学习作业(五):支持向量机
- 牛客13592 武藏牌牛奶促销
- python在线作业_南开大学20春学期《Python编程基础》在线作业参考答案
- 基于Spring+SpringMVC+Mybatis架构的开源博客
- C# 结合 PInvoke 对接 IP 摄像头的笔记
- 带你上手全新版本的Webpack 5
- LeetCode(530)——二叉搜索树的最小绝对差(JavaScript)
- 蓝桥杯2013c++真题:振兴中华
- python数据集_在Python中如何差分时间序列数据集
- [转]coolfire黑客入门教程系列之(四)
- 单片机微型计算机 原理及接口技术,微机原理及单片机接口技术
- 计算机毕业设计JAVA鸿鹄教育培训mybatis+源码+调试部署+系统+数据库+lw
- 考研二战上岸985的经验教训分享
- Acm - 隔壁老王买酒问题
- 170713 逆向-填数游戏
热门文章
- 四、kafka整体架构
- 自己的包增加为第三方包,使用Eclipse环境报Unresolved import错误(pycharm可用正常引用)...
- 利用 TypeConverter,转换字符串和各种类型只需写一个函数
- 机械表小案例之transform的应用
- FOUND MODULE 所在的表及刪除不啟作用的INCLUDE
- SQLite B/S使用(一)
- 8421转换法可以轻松实现各进制之间的转换
- 网络工程师HCIE-RS-ipv6第一节:IPv6地址(原理+实验)
- 游戏筑基开发之单链表及其增删改查(C语言)
- Security+ 学习笔记48 攻击框架