标题

上图就是生成的效果了, 下面是代码

package com.szboanda.ewaq_hn.jjrbg;import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.List;
import java.util.Vector;import javax.swing.SwingUtilities;import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PieLabelLinkStyle;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;/*** @Title:创建折线图* @author  * @since   JDK1.6* @history*/
public class CreatLineChart {/*** Jfreechart工具类* <p>* 解决中午乱码问题<br>* 用来创建类别图表数据集、创建饼图数据集、时间序列图数据集<br>* 用来对柱状图、折线图、饼图、堆积柱状图、时间序列图的样式进行渲染<br>* 设置X-Y坐标轴样式* <p>* */private static String NO_DATA_MSG = "数据加载失败";private static Font FONT = new Font("宋体", Font.PLAIN, 12);public static Color[] CHART_COLORS = {new Color(31,129,188), new Color(92,92,97), new Color(144,237,125), new Color(255,188,117),new Color(153,158,255), new Color(255,117,153), new Color(253,236,109), new Color(128,133,232),new Color(158,90,102),new Color(255, 204, 102) };//颜色/*** 静态代码块*/static {setChartTheme();}/*** 无参构造器*/public CreatLineChart() {}/** TODO  可以通过调用这个方法, 提供对应格式的参数即可生成图片,并存在指定位置* 生成一个这先出并保存为png格式,* @param title 图片标题* @param xtitle x轴标题* @param ytitle y轴标题* @param filepath 文件路径+文件名* @param categorie 横坐标类型* @param series 数据内容* @param width 图片宽度* @param height 图片高度* @throws Exception*/public  static void CreateNewLineChartForPng(String title,String xtitle,String ytitle,String filepath,List<String> categorie,List<Serie> series,int width,int height) throws Exception{ChartPanel  chartPanel = new CreatLineChart().createChart(title, xtitle, ytitle, categorie,series);//将图片保存为png格式 saveAsFile(chartPanel.getChart(),filepath,width,height);}/*** 中文主题样式 解决乱码*/public static void setChartTheme() {// 设置中文主题样式 解决乱码StandardChartTheme chartTheme = new StandardChartTheme("CN");// 设置标题字体chartTheme.setExtraLargeFont(FONT);// 设置图例的字体chartTheme.setRegularFont(FONT);// 设置轴向的字体chartTheme.setLargeFont(FONT);chartTheme.setSmallFont(FONT);chartTheme.setTitlePaint(new Color(51, 51, 51));chartTheme.setSubtitlePaint(new Color(85, 85, 85));chartTheme.setLegendBackgroundPaint(Color.WHITE);// 设置标注chartTheme.setLegendItemPaint(Color.BLACK);//chartTheme.setChartBackgroundPaint(Color.WHITE);// 绘制颜色绘制颜色.轮廓供应商// paintSequence,outlinePaintSequence,strokeSequence,outlineStrokeSequence,shapeSequencePaint[] OUTLINE_PAINT_SEQUENCE = new Paint[] { Color.WHITE };// 绘制器颜色源DefaultDrawingSupplier drawingSupplier = new DefaultDrawingSupplier(CHART_COLORS, CHART_COLORS, OUTLINE_PAINT_SEQUENCE,DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE);chartTheme.setDrawingSupplier(drawingSupplier);chartTheme.setPlotBackgroundPaint(Color.WHITE);// 绘制区域chartTheme.setPlotOutlinePaint(Color.WHITE);// 绘制区域外边框chartTheme.setLabelLinkPaint(new Color(8, 55, 114));// 链接标签颜色chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);chartTheme.setAxisOffset(new RectangleInsets(5, 12, 5, 12));chartTheme.setDomainGridlinePaint(new Color(192, 208, 224));// X坐标轴垂直网格颜色chartTheme.setRangeGridlinePaint(new Color(192, 192, 192));// Y坐标轴水平网格颜色chartTheme.setBaselinePaint(Color.WHITE);chartTheme.setCrosshairPaint(Color.BLUE);// 不确定含义chartTheme.setAxisLabelPaint(new Color(51, 51, 51));// 坐标轴标题文字颜色chartTheme.setTickLabelPaint(new Color(67, 67, 72));// 刻度数字chartTheme.setBarPainter(new StandardBarPainter());// 设置柱状图渲染chartTheme.setXYBarPainter(new StandardXYBarPainter());// XYBar 渲染chartTheme.setItemLabelPaint(Color.black);chartTheme.setThermometerPaint(Color.white);// 温度计ChartFactory.setChartTheme(chartTheme);}/*** 创建类别数据集合*/public static DefaultCategoryDataset createDefaultCategoryDataset(List<Serie> series, String[] categories) {DefaultCategoryDataset dataset = new DefaultCategoryDataset();for (Serie serie : series) {String name = serie.getName();Vector<Object> data = serie.getData();if (data != null && categories != null && data.size() == categories.length) {for (int index = 0; index < data.size(); index++) {String value = data.get(index) == null ? "" : data.get(index).toString();if (isPercent(value)) {value = value.substring(0, value.length() - 1);}if (isNumber(value)) {dataset.setValue(Double.parseDouble(value), name, categories[index]);}}}}return dataset;}/*** 设置图例无边框,默认黑色边框*/public static void setLegendEmptyBorder(JFreeChart chart) {chart.getLegend().setFrame(new BlockBorder(Color.WHITE));}/*** 是不是一个%形式的百分比* * @param str* @return*/public static boolean isPercent(String str) {return str != null ? str.endsWith("%") && isNumber(str.substring(0, str.length() - 1)) : false;}/*** 是不是一个数字* * @param str* @return*/public static boolean isNumber(String str) {return str != null ? str.matches("^[-+]?(([0-9]+)((([.]{0})([0-9]*))|(([.]{1})([0-9]+))))$") : false;}/*** 设置 折线图样式* * @param plot* @param isShowDataLabels*            是否显示数据标签 默认不显示节点形状*/public static void setLineRender(CategoryPlot plot, boolean isShowDataLabels) {setLineRender(plot, isShowDataLabels, false);}/*** 设置折线图样式* * @param plot* @param isShowDataLabels*            是否显示数据标签*/@SuppressWarnings("deprecation")public static void setLineRender(CategoryPlot plot, boolean isShowDataLabels, boolean isShapesVisible) {plot.setNoDataMessage(NO_DATA_MSG);plot.setInsets(new RectangleInsets(10, 10, 0, 10), false);LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();renderer.setStroke(new BasicStroke(1.5F));if (isShowDataLabels) {renderer.setBaseItemLabelsVisible(true);renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator(StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING,NumberFormat.getInstance()));renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));// weizhi}renderer.setBaseShapesVisible(isShapesVisible);// 数据点绘制形状setXAixs(plot);setYAixs(plot);}/*** 设置类别图表(CategoryPlot) X坐标轴线条颜色和样式* * @param axis*/public static void setXAixs(CategoryPlot plot) {Color lineColor = new Color(31, 121, 170);plot.getDomainAxis().setAxisLinePaint(lineColor);// X坐标轴颜色plot.getDomainAxis().setTickMarkPaint(lineColor);// X坐标轴标记|竖线颜色}/*** 设置类别图表(CategoryPlot) Y坐标轴线条颜色和样式 同时防止数据无法显示* * @param axis*/public static void setYAixs(CategoryPlot plot) {Color lineColor = new Color(192, 208, 224);ValueAxis axis = plot.getRangeAxis();axis.setAxisLinePaint(lineColor);// Y坐标轴颜色axis.setTickMarkPaint(lineColor);// Y坐标轴标记|竖线颜色// 隐藏Y刻度axis.setAxisLineVisible(false);axis.setTickMarksVisible(false);// Y轴网格线条plot.setRangeGridlinePaint(new Color(192, 192, 192));plot.setRangeGridlineStroke(new BasicStroke(1));plot.getRangeAxis().setUpperMargin(0.1);// 设置顶部Y坐标轴间距,防止数据无法显示plot.getRangeAxis().setLowerMargin(0.1);// 设置底部Y坐标轴间距}/*** 必须设置文本抗锯齿*/public static void setAntiAlias(JFreeChart chart) {chart.setTextAntiAlias(false);}//-----------------------------------------------------------------------------------------------------------------/*** * 折线图*       <p>*       创建图表步骤:<br/>*       1:创建数据集合<br/>*       2:创建Chart:<br/>*       3:设置抗锯齿,防止字体显示不清楚<br/>*       4:对柱子进行渲染,<br/>*       5:对其他部分进行渲染<br/>*       6:使用chartPanel接收<br/>* *       </p>*///创建折线图图表public DefaultCategoryDataset createDataset(List<String> categorie,List<Serie> series) {// 标注类别String[] categories = categorie.toArray(new String[categorie.size()]);//横坐标
//           String[] categories = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
//           series = new Vector<Serie>();
//           // 柱子名称:柱子所有的值集合
//           //纵坐标
//           series.add(new Serie("Tokyo", new Double[] { 49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4 }));
//           series.add(new Serie("New York", new Double[] { 83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3 }));
//           series.add(new Serie("London", new Double[] { 48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2 }));
//           series.add(new Serie("Berlin", new Double[] { 42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1 }));// 1:创建数据集合DefaultCategoryDataset dataset = CreatLineChart.createDefaultCategoryDataset(series, categories);return dataset;}/*** 创建折线图* @param title 折线图标题* @param xtitle x轴标题* @param ytitle y轴标题* @param categorie 横坐标类别* @param series 数据集* @return* @throws Exception*/public ChartPanel createChart(String title,String xtitle,String ytitle,List<String> categorie,List<Serie> series) throws Exception {// 2:创建Chart[创建不同图形]JFreeChart chart = ChartFactory.createLineChart(title, xtitle, ytitle, createDataset(categorie,series));// 3:设置抗锯齿,防止字体显示不清楚CreatLineChart.setAntiAlias(chart);// 抗锯齿// 4:对柱子进行渲染[[采用不同渲染]]CreatLineChart.setLineRender(chart.getCategoryPlot(), false,true);//// 5:对其他部分进行渲染CreatLineChart.setXAixs(chart.getCategoryPlot());// X坐标轴渲染CreatLineChart.setYAixs(chart.getCategoryPlot());// Y坐标轴渲染// 设置标注无边框chart.getLegend().setFrame(new BlockBorder(Color.WHITE));// 6:使用chartPanel接收ChartPanel chartPanel = new ChartPanel(chart);return chartPanel;}/*** 主方法 用来测试  `* @param args*/public static void main(String[] args) {
//           final JFrame frame = new JFrame();
//           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//           frame.setSize(1024, 420);
//           frame.setLocationRelativeTo(null);try {List<String> categorie = null;List<Serie> series = null;//横坐标String[] categories = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };series = new Vector<Serie>();// 柱子名称:柱子所有的值集合//纵坐标series.add(new Serie("Tokyo", new Double[] { 49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4 }));series.add(new Serie("New York", new Double[] { 83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3 }));series.add(new Serie("London", new Double[] { 48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2 }));series.add(new Serie("Berlin", new Double[] { 42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1 }));ChartPanel  chartPanel = new CreatLineChart().createChart(NO_DATA_MSG, NO_DATA_MSG, NO_DATA_MSG, categorie,series);
//            frame.getContentPane().add(chartPanel);
//            frame.setVisible(true);//将图片保存为png格式
//            saveAsFile(chartPanel.getChart(),"D:\\1\\lol.png",900,500);CreateNewLineChartForPng("lol2.png", NO_DATA_MSG, NO_DATA_MSG, NO_DATA_MSG, categorie, series, 900, 500);} catch (Exception e1) {e1.printStackTrace();}//swing 运行SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {// 创建图形try {} catch (Exception e) {e.printStackTrace();}}});}/*** 将图表保存为PNG、JPEG图片* @param chart  折线图对象* @param outputPath 文件保存路径, 包含文件名* @param weight  宽* @param height 高* @throws Exception*/public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height)throws Exception {      FileOutputStream out = null;      File outFile = new File(outputPath);      if (!outFile.getParentFile().exists()) {      outFile.getParentFile().mkdirs();      }      out = new FileOutputStream(outputPath);      // 保存为PNG      ChartUtilities.writeChartAsPNG(out, chart, weight, height);      // 保存为JPEG      // ChartUtilities.writeChartAsJPEG(out, chart, weight, height);      out.flush();      if (out != null) {      try {      out.close();      } catch (IOException e) {      // do nothing  e.printStackTrace();}      }      }}

下面是 一个实体类

package com.szboanda.ewaq_hn.jjrbg;import java.io.Serializable;
import java.util.Vector;/*** 系列:名字和数据集合 构成一条曲线</br> 可以将serie看作一根线或者一根柱子:* * <p>* 参照JS图表来描述数据:series: [{ name: 'Tokyo', data: [7.0, 6.9, 9.5, 14.5]* }, { name: 'New York', data: [-0.2, 0.8, 5.7, 11.3} ]* * */
public class Serie implements Serializable {private static final long serialVersionUID = 1L;private String name;// 名字private Vector<Object> data;// 数据值ֵpublic Serie() {}/*** * @param name*            名称(线条名称)* @param data*            数据(线条上的所有数据值)*/public Serie(String name, Vector<Object> data) {this.name = name;this.data = data;}/*** * @param name*            名称(线条名称)* @param array*            数据(线条上的所有数据值)*/public Serie(String name, Object[] array) {this.name = name;if (array != null) {data = new Vector<Object>(array.length);for (int i = 0; i < array.length; i++) {data.add(array[i]);}}}public String getName() {return name;}public void setName(String name) {this.name = name;}public Vector<Object> getData() {return data;}public void setData(Vector<Object> data) {this.data = data;}}

上面代码是一个完整的类, 可以通过调用其中的 CreateNewLineChartForPng() 方法 并传入对应参数自动生成折线图

java生成 折线图相关推荐

  1. java 生成折线图_jfree jsp java 生成折线图(详解带jar)

    1. 下载jfreechart-1.0.9.zip 包,解压将下面的.jar 文件放入自己工程的lib下. 2. 在web.xml 文件中添加一个servlet,如下所示: DisplayChart ...

  2. java生成折线图,饼状图,柱形图

    需要的jar包: jfreechart-1.0.13.jar jcommon-1.0.14.jar package com.viathink.lims.util;import java.awt.Col ...

  3. Java使用Poi实现导出Word段落以及表格,XWPFParagraph和XWPFRun详解,生成目录,生成折线图、柱状图、饼状图

    导出段落 public void exportSummarizeWord(HttpServletResponse response, Integer id) {Summarize summarize ...

  4. Poi 如何使用Java和POI技术生成折线图,柱状图,饼状图导出到word文档

    这篇文章主要介绍POI生成图表并导出word文档的基本操作.主要介绍三种图表:折线图.柱状图.饼状图. 一.效果展示 使用Java和POI技术生成的折线图,柱状图,饼状图的效果如下图所示: 二.环境准 ...

  5. Java使用POI生成折线图导出到word文档(折线图)

    本篇文章主要介绍,如何使用Apache POI组件生成折线图导出到word文档中,具体步骤看下文. 一.实现效果 Java使用POI技术生成折线图导出到word文档中,最终生成的折线图如下所示: 二. ...

  6. python曲线图数据爬取_python爬取二手房库存,存数数据库,生成折线图(上)

    python爬取二手房库存,存数据库,生成折线图(上) 想着快要买房了,可是房价又那么的贵.那么为啥不是自己爬点二手房的价格走势图.看看那里的房子适合自己(虫啊!!!) 打算用python3 djan ...

  7. JAVA生成甘特图Excel导出

    JAVA生成甘特图EXCEL 场景:以甘特图的方式,可以直观的看到任务的进展情况,资源的利用率等等,它也能帮助你考虑人力.资源.日期.项目中重复的要素和关键的部分 领域:如今甘特图不单单被应用到生产管 ...

  8. 写【Python折线图】的一百个技巧(一、生成折线图网页)

    写[Python折线图]的一百个技巧(一.生成折线图网页) 目录 写[Python折线图]的一百个技巧(一.生成折线图网页) 前言 学习环境 Python库环境 探究目标 绘制过程 引入库 初始化数据 ...

  9. python生成折线图怎么对特定点做颜色_python使用matplotlib绘制简单的折线图和散点图...

    生成数据 数据可视化 数据可视化指的是通过可视化表示来探索数据,它与数据挖掘机密相关. 安装matplotlib 请访问https://pypi.python.org/pypi/matplotlib/ ...

  10. python爬取二手房库存,存数据库,生成折线图(下)

    python爬取二手房库存,存数据库,生成折线图(下) 数据库有了房价的多阶段价格后,即可生成折线图.默认我的数据库已经有很多天的数据了 进入html页面的时候,默认加载房价走势图 在vue 的mou ...

最新文章

  1. 华师大计算机入门模拟卷,计算机入门模拟卷A-华东师范大学.docx
  2. redis在mac上的安装
  3. Linux Shell脚本编程 --split命令
  4. 一个关于微服务架构和monolithic架构的讨论
  5. TreeMap实现对中文的排序
  6. Android Studio------------重要提醒
  7. 怎么用计算机弹that girl,抖音使用教程 抖音that girl 歌曲歌词介绍
  8. java 网络实验_java网络聊天室实验
  9. LINUX开源监控平台Nagios(主机监控配置)
  10. golang cover协程异常
  11. java看视频可以学会吗,看it教程视频自学Java编程可以学会吗?
  12. 在github上建立自己的网站
  13. 简体中文与繁体中文的互相转换
  14. 2分钟完美激活Windows7旗舰版
  15. Activemq优点和缺点
  16. Elasticsearch相关操作梳理
  17. Uint8 Uint16等的区别
  18. 法拉克机器人自动怎么调_发那科机器人的正确操作方法及步骤
  19. 台式计算机箱ip5x,IP5X防水是个什么概念?
  20. 【FFH】为什么JSON将逐渐取代XML?

热门文章

  1. 瑞友天翼服务器ip地址怎么修改,瑞友天翼6.0版本iphone移动客户端操作手册
  2. 来自IT公司速查手册的各大IT公司薪资和待遇内幕
  3. 深度现场教学安徽省安庆市青年干部培训参观杭州梦想小镇实例
  4. xlsx文件打开乱码_xlsx文件怎么打开乱码
  5. 蜗牛角上争天地——吴清源大师、名人、棋魂
  6. 数据库之常用SQL语句整合
  7. 2016专业版Excel PQ没有提取功能
  8. 作为一个程序猿你必须会的技能----自学框架!
  9. 极路由HC5661a刷潘多拉固件后配置python环境运行脚本登陆dr.com校园网
  10. 你好,Wi-Fi 6