package com.potevio.rnd.tobacco.mine;

import java.util.Map;

/**

* @description 数据BEAN

* @author Zhou-Jingxian

*/

public class Bean {

private String goods_name ;

private Map priceindexMap;

public String getGoods_name() {

return goods_name;

}

public void setGoods_name(String goods_name) {

this.goods_name = goods_name;

}

public Map getPriceindexMap() {

return priceindexMap;

}

public void setPriceindexMap(Map priceindexMap) {

this.priceindexMap = priceindexMap;

}

}

package com.potevio.rnd.tobacco.mine;

import java.awt.Color;

import java.awt.Font;

import java.awt.GradientPaint;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartUtilities;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.labels.ItemLabelAnchor;

import org.jfree.chart.labels.ItemLabelPosition;

import org.jfree.chart.labels.StandardXYItemLabelGenerator;

import org.jfree.chart.plot.XYPlot;

import org.jfree.chart.renderer.xy.XYItemRenderer;

import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.time.Month;

import org.jfree.data.time.TimeSeries;

import org.jfree.data.time.TimeSeriesCollection;

import org.jfree.ui.RectangleInsets;

import org.jfree.ui.TextAnchor;

/**

* @description 使用JFreeChart组建,生成一个价格随日期的走势图表

* @author Zhou-Jingxian

*/

public class TimeSeriesChartUtil {

private String type;//month,year

private int width ;//后台计算

private int height;//后台计算

private String title;//图表的主标题

private String subTitle;//图表的子标题

private String xName;//可默认值:月份

private String yName;//可默认值:价格指数

/***

* constructor function

* @param type

* @param title

* @param subTitle

* @param xName

* @param yName

*/

public TimeSeriesChartUtil(String type,String title,String subTitle,String xName,String yName){

this.type = type;

this.title = title;

this.subTitle = subTitle;

this.xName = xName;

this.yName = yName;

if("month".equals(type)){

this.width = 800;

this.height = 600;

}else if("year".equals(type)){

this.width = 600;

this.height = 400;

}

}

/** 根据TimeSeriesCollection创建JFreeChart对象*/

public JFreeChart createChart(TimeSeriesCollection dataset) {

JFreeChart chart = ChartFactory.createTimeSeriesChart(this.title, this.xName,this.yName, dataset, true, true, true);

XYPlot plot = (XYPlot) chart.getPlot();

XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();

//设置网格背景颜色

plot.setBackgroundPaint(Color.white);

//设置网格竖线颜色

plot.setDomainGridlinePaint(Color.pink);

//设置网格横线颜色

plot.setRangeGridlinePaint(Color.pink);

//设置曲线图与xy轴的距离

plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));

//设置曲线是否显示数据点

xylineandshaperenderer.setBaseShapesVisible(true);

//设置曲线显示各数据点的值

XYItemRenderer xyitem = plot.getRenderer();

xyitem.setBaseItemLabelsVisible(true);

xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));

xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());

xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));

plot.setRenderer(xyitem);

//设置子标题

TextTitle subtitle = new TextTitle(this.subTitle, new Font("黑体", Font.BOLD, 12));

chart.addSubtitle(subtitle);

//设置主标题

chart.setTitle(new TextTitle(this.title, new Font("隶书", Font.ITALIC, 15)));

//设置背景颜色

chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000,Color.blue));

chart.setAntiAlias(true);

return chart;

}

/**创建TimeSeriesCollection对象  */

public TimeSeriesCollection createDataset(List datalist){

//时间曲线数据集合

TimeSeriesCollection lineDataset = new TimeSeriesCollection();

for(int i=0;i

Bean bean = datalist.get(i);

TimeSeries series = new TimeSeries(bean.getGoods_name(),Month.class);

Map pimap = bean.getPriceindexMap();

Set piset = pimap.entrySet();

Iterator piIterator = piset.iterator();

while(piIterator.hasNext()){

Map.Entry hiddenMapEntry = (Map.Entry)piIterator.next();

String key = hiddenMapEntry.getKey();

int year = Integer.parseInt(key.substring(0,4));

int month = Integer.parseInt(key.substring(4, 6));

series.add(new Month(month,year),hiddenMapEntry.getValue());

}

lineDataset.addSeries(series);

}

return lineDataset;

}

/**保存为文件*/

public void saveAsFile(JFreeChart chart, String outputPath) {

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, width, height);

// 保存为JPEG

// ChartUtilities.writeChartAsJPEG(out, chart, width, 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

}

}

}

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

this.width = width;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

public String getXName() {

return xName;

}

public void setXName(String name) {

xName = name;

}

public String getYName() {

return yName;

}

public void setYName(String name) {

yName = name;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getSubTitle() {

return subTitle;

}

public void setSubTitle(String subTitle) {

this.subTitle = subTitle;

}

}

package com.potevio.rnd.tobacco.mine;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.jfree.chart.JFreeChart;

import org.jfree.data.time.TimeSeriesCollection;

/**

* @description 构造数据,测试图片生成

* @author Zhou-Jingxian

*/

public class Main {

public static void main(String[] args) {

TimeSeriesChartUtil util = new TimeSeriesChartUtil("year", "重点品牌价格走势图", "2009年8-10月走势图", "时间", "价格指数");

List datalist = new ArrayList();

Bean bean1 = new Bean();

bean1.setGoods_name("中华");

Map priceindexMap1 = new HashMap();

priceindexMap1.put("200903", 99.86);

priceindexMap1.put("200904", 99.8);

priceindexMap1.put("200905", 99.97);

priceindexMap1.put("200906", 99.96);

priceindexMap1.put("200907", 99.86);

priceindexMap1.put("200908", 99.8);

priceindexMap1.put("200909", 99.97);

priceindexMap1.put("200910", 99.96);

bean1.setPriceindexMap(priceindexMap1);

datalist.add(bean1);

Bean bean2 = new Bean();

bean2.setGoods_name("芙蓉王");

Map priceindexMap2 = new HashMap();

priceindexMap2.put("200903", 100.12);

priceindexMap2.put("200904", 100.2);

priceindexMap2.put("200905", 100.0);

priceindexMap2.put("200906", 100.08);

priceindexMap2.put("200907", 100.12);

priceindexMap2.put("200908", 100.2);

priceindexMap2.put("200909", 100.0);

priceindexMap2.put("200910", 100.08);

bean2.setPriceindexMap(priceindexMap2);

datalist.add(bean2);

Bean bean3 = new Bean();

bean3.setGoods_name("云烟");

Map priceindexMap3 = new HashMap();

priceindexMap3.put("200903", 99.77);

priceindexMap3.put("200904", 99.7);

priceindexMap3.put("200905", 99.83);

priceindexMap3.put("200906", 99.93);

priceindexMap3.put("200907", 99.77);

priceindexMap3.put("200908", 99.7);

priceindexMap3.put("200909", 99.83);

priceindexMap3.put("200910", 99.93);

bean3.setPriceindexMap(priceindexMap3);

datalist.add(bean3);

//步骤1:创建XYDataset对象(准备数据)

TimeSeriesCollection dataset = util.createDataset(datalist);

//步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置

JFreeChart freeChart = util.createChart(dataset);

//步骤3:将JFreeChart对象输出到文件,Servlet输出流等

util.saveAsFile(freeChart, "D:\\jfreechart\\lineXY_"+Math.random()*20+".png");

}

}

需要导入jfreechart.jar包方可运行,最后生成一张图片D:\\jfreechart\\lineXY_"+Math.random()*20+".png。

折线图 java_java报表折线图相关推荐

  1. Excel制作甘特图+自动报表

    这里写自定义目录标题 Excel制作 甘特图 +自动报表 甘特图 项目管理文档 Excel制作 甘特图 +自动报表 甘特图 横轴 – 时间: 纵轴 – 项目里各任务: 通过条状图显示项目里各个任务,随 ...

  2. Java后台生成图表——主代码(折线图,饼状图,柱状图,-》并产出图片PDF或其他格式的图片内容)

    声明: 本文采用的数据均来源于网络,本人只用于学习记录,若有侵权,还望能及时联系. Maven 的 POM 依赖 <!--必要--><!--用于jfreechart生成图片 --&g ...

  3. Python使用matplotlib函数subplot可视化多个不同颜色的折线图、在折线图上为每个数据点添加日期数据标签

    Python使用matplotlib函数subplot可视化多个不同颜色的折线图.在折线图上为每个数据点添加日期数据标签 目录

  4. Python使用matplotlib函数subplot可视化多个不同颜色的折线图、在折线图上为每个数据点添加数值标签

    Python使用matplotlib函数subplot可视化多个不同颜色的折线图.在折线图上为每个数据点添加数值标签 目录

  5. Android之玩转MPAndroidChart让(折线图、柱形图、饼状图、散列图、雷达图)优雅的舞...

    2019独角兽企业重金招聘Python工程师标准>>> 把开源项目MPAndroidChart里面的折线图.柱形图.饼状图.散列图.雷达图怎么使用和一些属性详细的介绍,当我们项目 g ...

  6. Py之Seaborn:数据可视化Seaborn库的柱状图、箱线图(置信区间图)、散点图/折线图、核密度图/等高线图、盒形图/小提琴图/LV多框图的组合图/矩阵图实现

    Py之Seaborn:数据可视化Seaborn库的柱状图.箱线图(置信区间图).散点图/折线图.核密度图/等高线图.盒形图/小提琴图/LV多框图的组合图/矩阵图实现 目录

  7. 中绘制折线_啥是折线图?啥时候用?怎么用呢?这里全都有,满足你的味蕾

    大家好: 今天为大家带来图表选项下的折线图内容讲解,话不多说,开始今天的理论讲述和案例操作. 折线图常用来显示某一事项随时间变化的趋势,比如一周内步数的变化.公司各月份的产值变化等:也可用来表示有一种 ...

  8. echarts折线图背景线_echarts设置折线线条颜色和折线点颜色的实例

    本文主要介绍了jQuery插件echarts设置折线图中折线线条颜色和折线点颜色的方法,结合实例形式分析了jQuery图表插件echarts设置折线图的相关操作技巧,需要的朋友可以参考下,希望能帮助到 ...

  9. 中绘制折线_统计图之折线图的结构和制作过程

    统计图(statisticalgraph)是用点.线.面等各种几何图形来形象化表达统计数据.它将研究对象的特征.内部构成.相互关系.对比情况.频数分布等情况形象而生动地表达出来,更直观地反映出事物间的 ...

最新文章

  1. 华为、小米的新手机用了更多AI,到底谁更胜一筹?
  2. 专访1药网技术副总裁黄哲铿:揭秘技术跨界管理之道
  3. 【Android 系统开发】Android JNI 之 JNIEnv 解析
  4. Cisco PIX防火墙配置命令大全
  5. vue:vue页面刷新vuex数据消失问题
  6. getchar()和EOF
  7. html 按钮 按下 状态_第一次按下是启动,第二次按下是停止,俵哥分享2种接线方法...
  8. LeetCode - Partition List
  9. 教你如何在Python中读,写和解析CSV文
  10. 每天5分钟玩转kubernetes_DNS 访问 Service 每天5分钟玩转 Docker 容器技术(138)
  11. Youki的笔记本配置要求
  12. Codeforces Ilya and Queries
  13. java.lang.integer_java 中 关于java.lang.ArrayStoreException: java.lang.Integer异常,是什么原因?...
  14. Unity3D AABB包围盒效果
  15. NPN和PNP三极管原理以及应用电路设计
  16. Maven的下载安装和配置阿里云镜像教程
  17. ps入门第17天_模糊与锐化 案例:基础磨皮效果_ps修图_ps磨皮_ps高低频修图
  18. 灵魂拷问:为什么5G路由器比2.4G路由器快?
  19. c语言建立文件的时候会飞动,C语言:简单而不易懂的声明(二)
  20. problem: ERROR cluster.YarnClientSchedulerBackend: Yarn application has already exited with state

热门文章

  1. 将x的二进制最后一位置为0
  2. Windows下安装scikit-learn
  3. JavaSE——IO(上)(File、字节流、字符流、转换流、打印流、缓存流)
  4. Django——多个数据库
  5. HTML CSS JS(一)
  6. 快速转 TypeScript 指南
  7. Spring Data JPA 从入门到精通~查询方法的创建
  8. 对超长的文字换行处理:程序和CSS样式
  9. MySQL 字符集相关问题
  10. 【转载】广告系统架构解密