日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

折线图 java_java报表折线图

發布時間:2024/7/23 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 折线图 java_java报表折线图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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报表折线图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。