通過JfreeChart畫柱狀圖、時序圖、K線圖
阿新 • • 發佈:2019-02-16
import java.awt.Color;
import java.awt.Paint;
import java.io.*;
import java.text.SimpleDateFormat;
import org.jfree.data.*;
import org.jfree.data.category.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.SegmentedTimeline;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
/**
* 該類用於演示最簡單的柱狀圖生成
* @author Winter Lau
*/
public class TestJFreeChart {
public static void main(String[] args) throws IOException{
genCategory();
genLine();
genPie3D();
genTimeSeries();
KLineCombineChart();
}
/**
* 獲取一個演示用的簡單資料集物件
* @return
*/
private static CategoryDataset getDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, null, "蘋果");
dataset.addValue(200, null, "梨子");
dataset.addValue(300, null, "葡萄");
dataset.addValue(400, null, "香蕉");
dataset.addValue(500, null, "荔枝");
return dataset;
}
/**
* 獲取一個演示用的組合資料集物件
* @return
*/
private static CategoryDataset getDataSet2() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "蘋果");
dataset.addValue(100, "上海", "蘋果");
dataset.addValue(100, "廣州", "蘋果");
dataset.addValue(200, "北京", "梨子");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(200, "廣州", "梨子");
dataset.addValue(300, "北京", "葡萄");
dataset.addValue(300, "上海", "葡萄");
dataset.addValue(300, "廣州", "葡萄");
dataset.addValue(400, "北京", "香蕉");
dataset.addValue(400, "上海", "香蕉");
dataset.addValue(400, "廣州", "香蕉");
dataset.addValue(500, "北京", "荔枝");
dataset.addValue(500, "上海", "荔枝");
dataset.addValue(500, "廣州", "荔枝");
return dataset;
}
private static void genCategory(){
CategoryDataset dataset = getDataSet2();
JFreeChart chart = ChartFactory.createBarChart3D(
"柱狀圖Category", // 圖表標題
"水果", // 目錄軸的顯示標籤
"產量", // 數值軸的顯示標籤
dataset, // 資料集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例(對於簡單的柱狀圖必須是false)
false, // 是否生成工具
false // 是否生成URL連結
);
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("D:\\fruit.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
}catch(Exception e) {}
finally {
try {
fos_jpg.close();
} catch (Exception e) {}
}
}
// 折線圖
private static void genLine(){
CategoryDataset dataset = getDatasetLine();
JFreeChart chart = ChartFactory.createLineChart(
"折線圖Line", // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("D:\\fruitLine.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
}catch(Exception e) {}
finally {
try {
fos_jpg.close();
} catch (Exception e) {}
}
}
private static CategoryDataset getDatasetLine() {
// row keys...
final String series1 = "First";
final String series2 = "Second";
final String series3 = "Third";
// column keys...
final String type1 = "Type 1";
final String type2 = "Type 2";
final String type3 = "Type 3";
final String type4 = "Type 4";
final String type5 = "Type 5";
final String type6 = "Type 6";
final String type7 = "Type 7";
final String type8 = "Type 8";
// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, type1);
dataset.addValue(4.0, series1, type2);
dataset.addValue(3.0, series1, type3);
dataset.addValue(5.0, series1, type4);
dataset.addValue(5.0, series1, type5);
dataset.addValue(7.0, series1, type6);
dataset.addValue(7.0, series1, type7);
dataset.addValue(8.0, series1, type8);
dataset.addValue(5.0, series2, type1);
dataset.addValue(7.0, series2, type2);
dataset.addValue(6.0, series2, type3);
dataset.addValue(8.0, series2, type4);
dataset.addValue(4.0, series2, type5);
dataset.addValue(4.0, series2, type6);
dataset.addValue(2.0, series2, type7);
dataset.addValue(1.0, series2, type8);
dataset.addValue(4.0, series3, type1);
dataset.addValue(3.0, series3, type2);
dataset.addValue(2.0, series3, type3);
dataset.addValue(3.0, series3, type4);
dataset.addValue(6.0, series3, type5);
dataset.addValue(3.0, series3, type6);
dataset.addValue(4.0, series3, type7);
dataset.addValue(3.0, series3, type8);
return dataset;
}
// 餅狀圖
private static void genPie3D() {
DefaultPieDataset data = getDataSetPie3D();
JFreeChart chart = ChartFactory.createPieChart3D("餅圖Pie3D", // 圖表標題
data,
true, // 是否顯示圖例
false,
false
);
FileOutputStream fos_jpg = null;
try{
fos_jpg = new FileOutputStream("D:\\fruit3D.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
}catch (Exception e) {}
finally {
try {
fos_jpg.close();
}catch(Exception e) {}
}
}
private static DefaultPieDataset getDataSetPie3D() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("蘋果",100);
dataset.setValue("梨子",200);
dataset.setValue("葡萄",300);
dataset.setValue("香蕉",400);
dataset.setValue("荔枝",500);
return dataset;
}
// time 時序圖
private static TimeSeriesCollection getDataSetTimeSeries() {
TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
s1.add(new Month(2, 2001), 181.8);
s1.add(new Month(3, 2001), 167.3);
s1.add(new Month(4, 2001), 153.8);
s1.add(new Month(5, 2001), 167.6);
s1.add(new Month(6, 2001), 158.8);
s1.add(new Month(7, 2001), 148.3);
s1.add(new Month(8, 2001), 153.9);
s1.add(new Month(9, 2001), 142.7);
s1.add(new Month(10, 2001), 123.2);
s1.add(new Month(11, 2001), 131.8);
s1.add(new Month(12, 2001), 139.6);
s1.add(new Month(1, 2002), 142.9);
s1.add(new Month(2, 2002), 138.7);
s1.add(new Month(3, 2002), 137.3);
s1.add(new Month(4, 2002), 143.9);
s1.add(new Month(5, 2002), 139.8);
s1.add(new Month(6, 2002), 137.0);
s1.add(new Month(7, 2002), 132.8);
TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
s2.add(new Month(2, 2001), 129.6);
s2.add(new Month(3, 2001), 123.2);
s2.add(new Month(4, 2001), 117.2);
s2.add(new Month(5, 2001), 124.1);
s2.add(new Month(6, 2001), 122.6);
s2.add(new Month(7, 2001), 119.2);
s2.add(new Month(8, 2001), 116.5);
s2.add(new Month(9, 2001), 112.7);
s2.add(new Month(10, 2001), 101.5);
s2.add(new Month(11, 2001), 106.1);
s2.add(new Month(12, 2001), 110.3);
s2.add(new Month(1, 2002), 111.7);
s2.add(new Month(2, 2002), 111.0);
s2.add(new Month(3, 2002), 109.6);
s2.add(new Month(4, 2002), 113.2);
s2.add(new Month(5, 2002), 111.6);
s2.add(new Month(6, 2002), 108.8);
s2.add(new Month(7, 2002), 101.6);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
return dataset;
}
private static void genTimeSeries() {
TimeSeriesCollection dataset = getDataSetTimeSeries();
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"時序圖TimeSeries",
"Date",
"Price Per Unit",
dataset,
true,
true,
false
);
FileOutputStream fos_jpg = null;
try{
fos_jpg = new FileOutputStream("D:\\fruittime.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
FileOutputStream output = new FileOutputStream("D:\\fruittime2.png");
//設定背景色
chart.setBackgroundPaint(Color.WHITE);
//設定時序圖中線條的顏色
XYLineAndShapeRenderer xylinerenderer=(XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
//以下分別給同一個曲線圖中3條曲線設定顏色,0為第一條1為第二條,......
xylinerenderer.setSeriesPaint(0,Color.YELLOW);
//chart.setBorderPaint(Color.BLUE);
ChartUtilities.writeChartAsPNG(output, chart, 800, 600);
}catch (Exception e) {}
finally {
try {
fos_jpg.close();
}catch(Exception e) {}
}
}
/*
private static TimeSeriesCollection genTimeSeries2() {
TimeSeriesCollection data = getDataSetTimeSeries();
XYBarRenderer xyBarRender = new XYBarRenderer();
// volume range
NumberAxis y2Axis = new NumberAxis("");
y2Axis.setAutoRangeIncludesZero(false);
// setVolumnAxis(y2Axis);
XYPlot plot2 = new XYPlot(data, null, y2Axis, xyBarRender);
// combine two plot
CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(x1Axis);
combineddomainxyplot.add(plot1, 2);
combineddomainxyplot.add(plot2, 1);
combineddomainxyplot.setGap(4);
String title = String.format("%s - %s", stockInfo.getCode().getCode()
, stockInfo.getStockName());
JFreeChart chart = new JFreeChart(title,
JFreeChart.DEFAULT_TITLE_FONT, combineddomainxyplot, false);
try{
FileOutputStream output = new FileOutputStream("D:\\fruittime2.png");
ChartUtilities.writeChartAsPNG(output, chart, 800, 600);
}catch (Exception e) {}
finally { }
}
*/
public static void KLineCombineChart() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//設定日期格式
double highValue = Double.MIN_VALUE;//設定K線資料當中的最大值
double minValue = Double.MAX_VALUE;//設定K線資料當中的最小值
double high2Value = Double.MIN_VALUE;//設定成交量的最大值
double min2Value = Double.MAX_VALUE;//設定成交量的最低值
OHLCSeries series = new OHLCSeries("");//高開低收資料序列,股票K線圖的四個資料,依次是開,高,低,收
series.add(new Day(28, 9, 2007), 9.2, 9.58, 9.16, 9.34);
series.add(new Day(27, 9, 2007), 8.9, 9.06, 8.83, 8.96);
series.add(new Day(26, 9, 2007), 9.0, 9.1, 8.82, 9.04);
series.add(new Day(25, 9, 2007), 9.25, 9.33, 8.88, 9.00);
series.add(new Day(24, 9, 2007), 9.05, 9.50, 8.91, 9.25);
series.add(new Day(21, 9, 2007), 8.68, 9.05, 8.40, 9.00);
series.add(new Day(20, 9, 2007), 8.68, 8.95, 8.50, 8.69);
series.add(new Day(19, 9, 2007), 8.80, 8.94, 8.50, 8.66);
series.add(new Day(18, 9, 2007), 8.88, 9.17, 8.69, 8.80);
series.add(new Day(17, 9, 2007), 8.26, 8.98, 8.15, 8.89);
series.add(new Day(14, 9, 2007), 8.44, 8.45, 8.13, 8.33);
series.add(new Day(13, 9, 2007), 8.13, 8.46, 7.97, 8.42);
series.add(new Day(12, 9, 2007), 8.2, 8.4, 7.81, 8.13);
series.add(new Day(11, 9, 2007), 9.0, 9.0, 8.1, 8.24);
series.add(new Day(10, 9, 2007), 8.6, 9.03, 8.40, 8.95);
series.add(new Day(7, 9, 2007), 8.89, 9.04, 8.70, 8.73);
series.add(new Day(6, 9, 2007), 8.4, 9.08, 8.33, 8.88);
series.add(new Day(5, 9, 2007), 8.2, 8.74, 8.17, 8.36);
series.add(new Day(4, 9, 2007), 7.7, 8.46, 7.67, 8.27);
series.add(new Day(3, 9, 2007), 7.5, 7.8, 7.48, 7.69);
series.add(new Day(31, 8, 2007), 7.4, 7.6, 7.28, 7.43);
series.add(new Day(30, 8, 2007), 7.42, 7.56, 7.31, 7.40);
series.add(new Day(29, 8, 2007), 7.42, 7.66, 7.22, 7.33);
series.add(new Day(28, 8, 2007), 7.31, 7.70, 7.15, 7.56);
series.add(new Day(27, 8, 2007), 7.05, 7.46, 7.02, 7.41);
series.add(new Day(24, 8, 2007), 7.05, 7.09, 6.90, 6.99);
series.add(new Day(23, 8, 2007), 7.12, 7.16, 7.00, 7.03);
series.add(new Day(22, 8, 2007), 6.96, 7.15, 6.93, 7.11);
series.add(new Day(21, 8, 2007), 7.10, 7.15, 7.02, 7.07);
series.add(new Day(20, 8, 2007), 7.02, 7.19, 6.94, 7.14);
final OHLCSeriesCollection seriesCollection = new OHLCSeriesCollection();//保留K線資料的資料集,必須申明為final,後面要在匿名內部類裡面用到
seriesCollection.addSeries(series);
TimeSeries series2=new TimeSeries("");//對應時間成交量資料
series2.add(new Day(28, 9, 2007), 260659400/100);
series2.add(new Day(27, 9, 2007), 119701900/100);
series2.add(new Day(26, 9, 2007), 109719000/100);
series2.add(new Day(25, 9, 2007), 178492400/100);
series2.add(new Day(24, 9, 2007), 269978500/100);
series2.add(new Day(21, 9, 2007), 361042300/100);
series2.add(new Day(20, 9, 2007), 173912600/100);
series2.add(new Day(19, 9, 2007), 154622600/100);
series2.add(new Day(18, 9, 2007), 200661600/100);
series2.add(new Day(17, 9, 2007), 312799600/100);
series2.add(new Day(14, 9, 2007), 141652900/100);
series2.add(new Day(13, 9, 2007), 221260400/100);
series2.add(new Day(12, 9, 2007), 274795400/100);
series2.add(new Day(11, 9, 2007), 289287300/100);
series2.add(new Day(10, 9, 2007), 289063600/100);
series2.add(new Day(7, 9, 2007), 351575300/100);
series2.add(new Day(6, 9, 2007), 451357300/100);
series2.add(new Day(5, 9, 2007), 442421200/100);
series2.add(new Day(4, 9, 2007), 671942600/100);
series2.add(new Day(3, 9, 2007), 349647800/100);
series2.add(new Day(31, 8, 2007), 225339300/100);
series2.add(new Day(30, 8, 2007), 160048200/100);
series2.add(new Day(29, 8, 2007), 247341700/100);
series2.add(new Day(28, 8, 2007), 394975400/100);
series2.add(new Day(27, 8, 2007), 475797500/100);
series2.add(new Day(24, 8, 2007), 297679500/100);
series2.add(new Day(23, 8, 2007), 191760600/100);
series2.add(new Day(22, 8, 2007), 232570200/100);
series2.add(new Day(21, 8, 2007), 215693200/100);
series2.add(new Day(20, 8, 2007), 200287500/100);
TimeSeriesCollection timeSeriesCollection=new TimeSeriesCollection();//保留成交量資料的集合
timeSeriesCollection.addSeries(series2);
//獲取K線資料的最高值和最低值
int seriesCount = seriesCollection.getSeriesCount();//一共有多少個序列,目前為一個
for (int i = 0; i < seriesCount; i++) {
int itemCount = seriesCollection.getItemCount(i);//每一個序列有多少個數據項
for (int j = 0; j < itemCount; j++) {
if (highValue < seriesCollection.getHighValue(i, j)) {//取第i個序列中的第j個數據項的最大值
highValue = seriesCollection.getHighValue(i, j);
}
if (minValue > seriesCollection.getLowValue(i, j)) {//取第i個序列中的第j個數據項的最小值
minValue = seriesCollection.getLowValue(i, j);
}
}
}
//獲取最高值和最低值
int seriesCount2 = timeSeriesCollection.getSeriesCount();//一共有多少個序列,目前為一個
for (int i = 0; i < seriesCount2; i++) {
int itemCount = timeSeriesCollection.getItemCount(i);//每一個序列有多少個數據項
for (int j = 0; j < itemCount; j++) {
if (high2Value < timeSeriesCollection.getYValue(i,j)) {//取第i個序列中的第j個數據項的值
high2Value = timeSeriesCollection.getYValue(i,j);
}
if (min2Value > timeSeriesCollection.getYValue(i, j)) {//取第i個序列中的第j個數據項的值
min2Value = timeSeriesCollection.getYValue(i, j);
}
}
}
final CandlestickRenderer candlestickRender=new CandlestickRenderer();//設定K線圖的畫圖器,必須申明為final,後面要在匿名內部類裡面用到
candlestickRender.setUseOutlinePaint(true); //設定是否使用自定義的邊框線,程式自帶的邊框線的顏色不符合中國股票市場的習慣
candlestickRender.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_AVERAGE);//設定如何對K線圖的寬度進行設定
candlestickRender.setAutoWidthGap(0.001);//設定各個K線圖之間的間隔
candlestickRender.setUpPaint(Color.RED);//設定股票上漲的K線圖顏色
candlestickRender.setDownPaint(Color.GREEN);//設定股票下跌的K線圖顏色
DateAxis x1Axis=new DateAxis();//設定x軸,也就是時間軸
x1Axis.setAutoRange(false);//設定不採用自動設定時間範圍
try{
x1Axis.setRange(dateFormat.parse("2007-08-20"),dateFormat.parse("2007-09-29"));//設定時間範圍,注意時間的最大值要比已有的時間最大值要多一天
}catch(Exception e){
e.printStackTrace();
}
x1Axis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());//設定時間線顯示的規則,用這個方法就摒除掉了週六和週日這些沒有交易的日期(很多人都不知道有此方法),使圖形看上去連續
x1Axis.setAutoTickUnitSelection(false);//設定不採用自動選擇刻度值
x1Axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);//設定標記的位置
x1Axis.setStandardTickUnits(DateAxis.createStandardDateTickUnits());//設定標準的時間刻度單位
x1Axis.setTickUnit(new DateTickUnit(DateTickUnit.DAY,7));//設定時間刻度的間隔,一般以周為單位
x1Axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));//設定顯示時間的格式
NumberAxis y1Axis=new NumberAxis();//設定y軸,就是數字軸
y1Axis.setAutoRange(false);//不不使用自動設定範圍
y1Axis.setRange(minValue*0.9, highValue*1.1);//設定y軸值的範圍,比最低值要低一些,比最大值要大一些,這樣圖形看起來會美觀些
y1Axis.setTickUnit(new NumberTickUnit((highValue*1.1-minValue*0.9)/10));//設定刻度顯示的密度
XYPlot plot1=new XYPlot(seriesCollection,x1Axis,y1Axis,candlestickRender);//設定畫圖區域物件
XYBarRenderer xyBarRender=new XYBarRenderer(){
private static final long serialVersionUID = 1L;//為了避免出現警告訊息,特設定此值
public Paint getItemPaint(int i, int j){//匿名內部類用來處理當日的成交量柱形圖的顏色與K線圖的顏色保持一致
if(seriesCollection.getCloseValue(i,j)>seriesCollection.getOpenValue(i,j)){//收盤價高於開盤價,股票上漲,選用股票上漲的顏色
return candlestickRender.getUpPaint();
}else{
return candlestickRender.getDownPaint();
}
}};
xyBarRender.setMargin(0.1);//設定柱形圖之間的間隔
NumberAxis y2Axis=new NumberAxis();//設定Y軸,為數值,後面的設定,參考上面的y軸設定
y2Axis.setAutoRange(false);
y2Axis.setRange(min2Value*0.9, high2Value*1.1);
y2Axis.setTickUnit(new NumberTickUnit((high2Value*1.1-min2Value*0.9)/4));
XYPlot plot2=new XYPlot(timeSeriesCollection,null,y2Axis,xyBarRender);//建立第二個畫圖區域物件,主要此時的x軸設為了null值,因為要與第一個畫圖區域物件共享x軸
CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(x1Axis);//建立一個恰當的聯合圖形區域物件,以x軸為共享軸
combineddomainxyplot.add(plot1, 2);//新增圖形區域物件,後面的數字是計算這個區域物件應該佔據多大的區域2/3
combineddomainxyplot.add(plot2, 1);//新增圖形區域物件,後面的數字是計算這個區域物件應該佔據多大的區域1/3
combineddomainxyplot.setGap(10);//設定兩個圖形區域物件之間的間隔空間
JFreeChart chart = new JFreeChart("中國聯通", JFreeChart.DEFAULT_TITLE_FONT, combineddomainxyplot, false);
ChartFrame frame = new ChartFrame("中國聯通股票", chart);
frame.pack();
frame.setVisible(true);
}
}
如何設定jfreechart時間序列圖曲線顏色
chart = ChartFactory.createTimeSeriesChart(......);
XYPlot xyplot=(XYPlot) chart.getPlot();
XYLineAndShapeRenderer xylinerenderer=(XYLineAndShapeRenderer)xyplot.getRenderer();
//以下分別給同一個曲線圖中3條曲線設定顏色,0為第一條1為第二條,......
xylinerenderer.setSeriesPaint(0, new Color(0, 0 ,255 ));
xylinerenderer.setSeriesPaint(1, new Color(255 ,215, 0 ));
xylinerenderer.setSeriesPaint(2, new Color(0, 238, 118));
import java.awt.Paint;
import java.io.*;
import java.text.SimpleDateFormat;
import org.jfree.data.*;
import org.jfree.data.category.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.SegmentedTimeline;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
/**
* 該類用於演示最簡單的柱狀圖生成
* @author Winter Lau
*/
public class TestJFreeChart {
public static void main(String[] args) throws IOException{
genCategory();
genLine();
genPie3D();
genTimeSeries();
KLineCombineChart();
}
/**
* 獲取一個演示用的簡單資料集物件
* @return
*/
private static CategoryDataset getDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, null, "蘋果");
dataset.addValue(200, null, "梨子");
dataset.addValue(300, null, "葡萄");
dataset.addValue(400, null, "香蕉");
dataset.addValue(500, null, "荔枝");
return dataset;
}
/**
* 獲取一個演示用的組合資料集物件
* @return
*/
private static CategoryDataset getDataSet2() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "蘋果");
dataset.addValue(100, "上海", "蘋果");
dataset.addValue(100, "廣州", "蘋果");
dataset.addValue(200, "北京", "梨子");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(200, "廣州", "梨子");
dataset.addValue(300, "北京", "葡萄");
dataset.addValue(300, "上海", "葡萄");
dataset.addValue(300, "廣州", "葡萄");
dataset.addValue(400, "北京", "香蕉");
dataset.addValue(400, "上海", "香蕉");
dataset.addValue(400, "廣州", "香蕉");
dataset.addValue(500, "北京", "荔枝");
dataset.addValue(500, "上海", "荔枝");
dataset.addValue(500, "廣州", "荔枝");
return dataset;
}
private static void genCategory(){
CategoryDataset dataset = getDataSet2();
JFreeChart chart = ChartFactory.createBarChart3D(
"柱狀圖Category", // 圖表標題
"水果", // 目錄軸的顯示標籤
"產量", // 數值軸的顯示標籤
dataset, // 資料集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例(對於簡單的柱狀圖必須是false)
false, // 是否生成工具
false // 是否生成URL連結
);
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("D:\\fruit.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
}catch(Exception e) {}
finally {
try {
fos_jpg.close();
} catch (Exception e) {}
}
}
// 折線圖
private static void genLine(){
CategoryDataset dataset = getDatasetLine();
JFreeChart chart = ChartFactory.createLineChart(
"折線圖Line", // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("D:\\fruitLine.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
}catch(Exception e) {}
finally {
try {
fos_jpg.close();
} catch (Exception e) {}
}
}
private static CategoryDataset getDatasetLine() {
// row keys...
final String series1 = "First";
final String series2 = "Second";
final String series3 = "Third";
// column keys...
final String type1 = "Type 1";
final String type2 = "Type 2";
final String type3 = "Type 3";
final String type4 = "Type 4";
final String type5 = "Type 5";
final String type6 = "Type 6";
final String type7 = "Type 7";
final String type8 = "Type 8";
// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, type1);
dataset.addValue(4.0, series1, type2);
dataset.addValue(3.0, series1, type3);
dataset.addValue(5.0, series1, type4);
dataset.addValue(5.0, series1, type5);
dataset.addValue(7.0, series1, type6);
dataset.addValue(7.0, series1, type7);
dataset.addValue(8.0, series1, type8);
dataset.addValue(5.0, series2, type1);
dataset.addValue(7.0, series2, type2);
dataset.addValue(6.0, series2, type3);
dataset.addValue(8.0, series2, type4);
dataset.addValue(4.0, series2, type5);
dataset.addValue(4.0, series2, type6);
dataset.addValue(2.0, series2, type7);
dataset.addValue(1.0, series2, type8);
dataset.addValue(4.0, series3, type1);
dataset.addValue(3.0, series3, type2);
dataset.addValue(2.0, series3, type3);
dataset.addValue(3.0, series3, type4);
dataset.addValue(6.0, series3, type5);
dataset.addValue(3.0, series3, type6);
dataset.addValue(4.0, series3, type7);
dataset.addValue(3.0, series3, type8);
return dataset;
}
// 餅狀圖
private static void genPie3D() {
DefaultPieDataset data = getDataSetPie3D();
JFreeChart chart = ChartFactory.createPieChart3D("餅圖Pie3D", // 圖表標題
data,
true, // 是否顯示圖例
false,
false
);
FileOutputStream fos_jpg = null;
try{
fos_jpg = new FileOutputStream("D:\\fruit3D.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
}catch (Exception e) {}
finally {
try {
fos_jpg.close();
}catch(Exception e) {}
}
}
private static DefaultPieDataset getDataSetPie3D() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("蘋果",100);
dataset.setValue("梨子",200);
dataset.setValue("葡萄",300);
dataset.setValue("香蕉",400);
dataset.setValue("荔枝",500);
return dataset;
}
// time 時序圖
private static TimeSeriesCollection getDataSetTimeSeries() {
TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
s1.add(new Month(2, 2001), 181.8);
s1.add(new Month(3, 2001), 167.3);
s1.add(new Month(4, 2001), 153.8);
s1.add(new Month(5, 2001), 167.6);
s1.add(new Month(6, 2001), 158.8);
s1.add(new Month(7, 2001), 148.3);
s1.add(new Month(8, 2001), 153.9);
s1.add(new Month(9, 2001), 142.7);
s1.add(new Month(10, 2001), 123.2);
s1.add(new Month(11, 2001), 131.8);
s1.add(new Month(12, 2001), 139.6);
s1.add(new Month(1, 2002), 142.9);
s1.add(new Month(2, 2002), 138.7);
s1.add(new Month(3, 2002), 137.3);
s1.add(new Month(4, 2002), 143.9);
s1.add(new Month(5, 2002), 139.8);
s1.add(new Month(6, 2002), 137.0);
s1.add(new Month(7, 2002), 132.8);
TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
s2.add(new Month(2, 2001), 129.6);
s2.add(new Month(3, 2001), 123.2);
s2.add(new Month(4, 2001), 117.2);
s2.add(new Month(5, 2001), 124.1);
s2.add(new Month(6, 2001), 122.6);
s2.add(new Month(7, 2001), 119.2);
s2.add(new Month(8, 2001), 116.5);
s2.add(new Month(9, 2001), 112.7);
s2.add(new Month(10, 2001), 101.5);
s2.add(new Month(11, 2001), 106.1);
s2.add(new Month(12, 2001), 110.3);
s2.add(new Month(1, 2002), 111.7);
s2.add(new Month(2, 2002), 111.0);
s2.add(new Month(3, 2002), 109.6);
s2.add(new Month(4, 2002), 113.2);
s2.add(new Month(5, 2002), 111.6);
s2.add(new Month(6, 2002), 108.8);
s2.add(new Month(7, 2002), 101.6);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
return dataset;
}
private static void genTimeSeries() {
TimeSeriesCollection dataset = getDataSetTimeSeries();
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"時序圖TimeSeries",
"Date",
"Price Per Unit",
dataset,
true,
true,
false
);
FileOutputStream fos_jpg = null;
try{
fos_jpg = new FileOutputStream("D:\\fruittime.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
FileOutputStream output = new FileOutputStream("D:\\fruittime2.png");
//設定背景色
chart.setBackgroundPaint(Color.WHITE);
//設定時序圖中線條的顏色
XYLineAndShapeRenderer xylinerenderer=(XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
//以下分別給同一個曲線圖中3條曲線設定顏色,0為第一條1為第二條,......
xylinerenderer.setSeriesPaint(0,Color.YELLOW);
//chart.setBorderPaint(Color.BLUE);
ChartUtilities.writeChartAsPNG(output, chart, 800, 600);
}catch (Exception e) {}
finally {
try {
fos_jpg.close();
}catch(Exception e) {}
}
}
/*
private static TimeSeriesCollection genTimeSeries2() {
TimeSeriesCollection data = getDataSetTimeSeries();
XYBarRenderer xyBarRender = new XYBarRenderer();
// volume range
NumberAxis y2Axis = new NumberAxis("");
y2Axis.setAutoRangeIncludesZero(false);
// setVolumnAxis(y2Axis);
XYPlot plot2 = new XYPlot(data, null, y2Axis, xyBarRender);
// combine two plot
CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(x1Axis);
combineddomainxyplot.add(plot1, 2);
combineddomainxyplot.add(plot2, 1);
combineddomainxyplot.setGap(4);
String title = String.format("%s - %s", stockInfo.getCode().getCode()
, stockInfo.getStockName());
JFreeChart chart = new JFreeChart(title,
JFreeChart.DEFAULT_TITLE_FONT, combineddomainxyplot, false);
try{
FileOutputStream output = new FileOutputStream("D:\\fruittime2.png");
ChartUtilities.writeChartAsPNG(output, chart, 800, 600);
}catch (Exception e) {}
finally { }
}
*/
public static void KLineCombineChart() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//設定日期格式
double highValue = Double.MIN_VALUE;//設定K線資料當中的最大值
double minValue = Double.MAX_VALUE;//設定K線資料當中的最小值
double high2Value = Double.MIN_VALUE;//設定成交量的最大值
double min2Value = Double.MAX_VALUE;//設定成交量的最低值
OHLCSeries series = new OHLCSeries("");//高開低收資料序列,股票K線圖的四個資料,依次是開,高,低,收
series.add(new Day(28, 9, 2007), 9.2, 9.58, 9.16, 9.34);
series.add(new Day(27, 9, 2007), 8.9, 9.06, 8.83, 8.96);
series.add(new Day(26, 9, 2007), 9.0, 9.1, 8.82, 9.04);
series.add(new Day(25, 9, 2007), 9.25, 9.33, 8.88, 9.00);
series.add(new Day(24, 9, 2007), 9.05, 9.50, 8.91, 9.25);
series.add(new Day(21, 9, 2007), 8.68, 9.05, 8.40, 9.00);
series.add(new Day(20, 9, 2007), 8.68, 8.95, 8.50, 8.69);
series.add(new Day(19, 9, 2007), 8.80, 8.94, 8.50, 8.66);
series.add(new Day(18, 9, 2007), 8.88, 9.17, 8.69, 8.80);
series.add(new Day(17, 9, 2007), 8.26, 8.98, 8.15, 8.89);
series.add(new Day(14, 9, 2007), 8.44, 8.45, 8.13, 8.33);
series.add(new Day(13, 9, 2007), 8.13, 8.46, 7.97, 8.42);
series.add(new Day(12, 9, 2007), 8.2, 8.4, 7.81, 8.13);
series.add(new Day(11, 9, 2007), 9.0, 9.0, 8.1, 8.24);
series.add(new Day(10, 9, 2007), 8.6, 9.03, 8.40, 8.95);
series.add(new Day(7, 9, 2007), 8.89, 9.04, 8.70, 8.73);
series.add(new Day(6, 9, 2007), 8.4, 9.08, 8.33, 8.88);
series.add(new Day(5, 9, 2007), 8.2, 8.74, 8.17, 8.36);
series.add(new Day(4, 9, 2007), 7.7, 8.46, 7.67, 8.27);
series.add(new Day(3, 9, 2007), 7.5, 7.8, 7.48, 7.69);
series.add(new Day(31, 8, 2007), 7.4, 7.6, 7.28, 7.43);
series.add(new Day(30, 8, 2007), 7.42, 7.56, 7.31, 7.40);
series.add(new Day(29, 8, 2007), 7.42, 7.66, 7.22, 7.33);
series.add(new Day(28, 8, 2007), 7.31, 7.70, 7.15, 7.56);
series.add(new Day(27, 8, 2007), 7.05, 7.46, 7.02, 7.41);
series.add(new Day(24, 8, 2007), 7.05, 7.09, 6.90, 6.99);
series.add(new Day(23, 8, 2007), 7.12, 7.16, 7.00, 7.03);
series.add(new Day(22, 8, 2007), 6.96, 7.15, 6.93, 7.11);
series.add(new Day(21, 8, 2007), 7.10, 7.15, 7.02, 7.07);
series.add(new Day(20, 8, 2007), 7.02, 7.19, 6.94, 7.14);
final OHLCSeriesCollection seriesCollection = new OHLCSeriesCollection();//保留K線資料的資料集,必須申明為final,後面要在匿名內部類裡面用到
seriesCollection.addSeries(series);
TimeSeries series2=new TimeSeries("");//對應時間成交量資料
series2.add(new Day(28, 9, 2007), 260659400/100);
series2.add(new Day(27, 9, 2007), 119701900/100);
series2.add(new Day(26, 9, 2007), 109719000/100);
series2.add(new Day(25, 9, 2007), 178492400/100);
series2.add(new Day(24, 9, 2007), 269978500/100);
series2.add(new Day(21, 9, 2007), 361042300/100);
series2.add(new Day(20, 9, 2007), 173912600/100);
series2.add(new Day(19, 9, 2007), 154622600/100);
series2.add(new Day(18, 9, 2007), 200661600/100);
series2.add(new Day(17, 9, 2007), 312799600/100);
series2.add(new Day(14, 9, 2007), 141652900/100);
series2.add(new Day(13, 9, 2007), 221260400/100);
series2.add(new Day(12, 9, 2007), 274795400/100);
series2.add(new Day(11, 9, 2007), 289287300/100);
series2.add(new Day(10, 9, 2007), 289063600/100);
series2.add(new Day(7, 9, 2007), 351575300/100);
series2.add(new Day(6, 9, 2007), 451357300/100);
series2.add(new Day(5, 9, 2007), 442421200/100);
series2.add(new Day(4, 9, 2007), 671942600/100);
series2.add(new Day(3, 9, 2007), 349647800/100);
series2.add(new Day(31, 8, 2007), 225339300/100);
series2.add(new Day(30, 8, 2007), 160048200/100);
series2.add(new Day(29, 8, 2007), 247341700/100);
series2.add(new Day(28, 8, 2007), 394975400/100);
series2.add(new Day(27, 8, 2007), 475797500/100);
series2.add(new Day(24, 8, 2007), 297679500/100);
series2.add(new Day(23, 8, 2007), 191760600/100);
series2.add(new Day(22, 8, 2007), 232570200/100);
series2.add(new Day(21, 8, 2007), 215693200/100);
series2.add(new Day(20, 8, 2007), 200287500/100);
TimeSeriesCollection timeSeriesCollection=new TimeSeriesCollection();//保留成交量資料的集合
timeSeriesCollection.addSeries(series2);
//獲取K線資料的最高值和最低值
int seriesCount = seriesCollection.getSeriesCount();//一共有多少個序列,目前為一個
for (int i = 0; i < seriesCount; i++) {
int itemCount = seriesCollection.getItemCount(i);//每一個序列有多少個數據項
for (int j = 0; j < itemCount; j++) {
if (highValue < seriesCollection.getHighValue(i, j)) {//取第i個序列中的第j個數據項的最大值
highValue = seriesCollection.getHighValue(i, j);
}
if (minValue > seriesCollection.getLowValue(i, j)) {//取第i個序列中的第j個數據項的最小值
minValue = seriesCollection.getLowValue(i, j);
}
}
}
//獲取最高值和最低值
int seriesCount2 = timeSeriesCollection.getSeriesCount();//一共有多少個序列,目前為一個
for (int i = 0; i < seriesCount2; i++) {
int itemCount = timeSeriesCollection.getItemCount(i);//每一個序列有多少個數據項
for (int j = 0; j < itemCount; j++) {
if (high2Value < timeSeriesCollection.getYValue(i,j)) {//取第i個序列中的第j個數據項的值
high2Value = timeSeriesCollection.getYValue(i,j);
}
if (min2Value > timeSeriesCollection.getYValue(i, j)) {//取第i個序列中的第j個數據項的值
min2Value = timeSeriesCollection.getYValue(i, j);
}
}
}
final CandlestickRenderer candlestickRender=new CandlestickRenderer();//設定K線圖的畫圖器,必須申明為final,後面要在匿名內部類裡面用到
candlestickRender.setUseOutlinePaint(true); //設定是否使用自定義的邊框線,程式自帶的邊框線的顏色不符合中國股票市場的習慣
candlestickRender.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_AVERAGE);//設定如何對K線圖的寬度進行設定
candlestickRender.setAutoWidthGap(0.001);//設定各個K線圖之間的間隔
candlestickRender.setUpPaint(Color.RED);//設定股票上漲的K線圖顏色
candlestickRender.setDownPaint(Color.GREEN);//設定股票下跌的K線圖顏色
DateAxis x1Axis=new DateAxis();//設定x軸,也就是時間軸
x1Axis.setAutoRange(false);//設定不採用自動設定時間範圍
try{
x1Axis.setRange(dateFormat.parse("2007-08-20"),dateFormat.parse("2007-09-29"));//設定時間範圍,注意時間的最大值要比已有的時間最大值要多一天
}catch(Exception e){
e.printStackTrace();
}
x1Axis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());//設定時間線顯示的規則,用這個方法就摒除掉了週六和週日這些沒有交易的日期(很多人都不知道有此方法),使圖形看上去連續
x1Axis.setAutoTickUnitSelection(false);//設定不採用自動選擇刻度值
x1Axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);//設定標記的位置
x1Axis.setStandardTickUnits(DateAxis.createStandardDateTickUnits());//設定標準的時間刻度單位
x1Axis.setTickUnit(new DateTickUnit(DateTickUnit.DAY,7));//設定時間刻度的間隔,一般以周為單位
x1Axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));//設定顯示時間的格式
NumberAxis y1Axis=new NumberAxis();//設定y軸,就是數字軸
y1Axis.setAutoRange(false);//不不使用自動設定範圍
y1Axis.setRange(minValue*0.9, highValue*1.1);//設定y軸值的範圍,比最低值要低一些,比最大值要大一些,這樣圖形看起來會美觀些
y1Axis.setTickUnit(new NumberTickUnit((highValue*1.1-minValue*0.9)/10));//設定刻度顯示的密度
XYPlot plot1=new XYPlot(seriesCollection,x1Axis,y1Axis,candlestickRender);//設定畫圖區域物件
XYBarRenderer xyBarRender=new XYBarRenderer(){
private static final long serialVersionUID = 1L;//為了避免出現警告訊息,特設定此值
public Paint getItemPaint(int i, int j){//匿名內部類用來處理當日的成交量柱形圖的顏色與K線圖的顏色保持一致
if(seriesCollection.getCloseValue(i,j)>seriesCollection.getOpenValue(i,j)){//收盤價高於開盤價,股票上漲,選用股票上漲的顏色
return candlestickRender.getUpPaint();
}else{
return candlestickRender.getDownPaint();
}
}};
xyBarRender.setMargin(0.1);//設定柱形圖之間的間隔
NumberAxis y2Axis=new NumberAxis();//設定Y軸,為數值,後面的設定,參考上面的y軸設定
y2Axis.setAutoRange(false);
y2Axis.setRange(min2Value*0.9, high2Value*1.1);
y2Axis.setTickUnit(new NumberTickUnit((high2Value*1.1-min2Value*0.9)/4));
XYPlot plot2=new XYPlot(timeSeriesCollection,null,y2Axis,xyBarRender);//建立第二個畫圖區域物件,主要此時的x軸設為了null值,因為要與第一個畫圖區域物件共享x軸
CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(x1Axis);//建立一個恰當的聯合圖形區域物件,以x軸為共享軸
combineddomainxyplot.add(plot1, 2);//新增圖形區域物件,後面的數字是計算這個區域物件應該佔據多大的區域2/3
combineddomainxyplot.add(plot2, 1);//新增圖形區域物件,後面的數字是計算這個區域物件應該佔據多大的區域1/3
combineddomainxyplot.setGap(10);//設定兩個圖形區域物件之間的間隔空間
JFreeChart chart = new JFreeChart("中國聯通", JFreeChart.DEFAULT_TITLE_FONT, combineddomainxyplot, false);
ChartFrame frame = new ChartFrame("中國聯通股票", chart);
frame.pack();
frame.setVisible(true);
}
}
如何設定jfreechart時間序列圖曲線顏色
chart = ChartFactory.createTimeSeriesChart(......);
XYPlot xyplot=(XYPlot) chart.getPlot();
XYLineAndShapeRenderer xylinerenderer=(XYLineAndShapeRenderer)xyplot.getRenderer();
//以下分別給同一個曲線圖中3條曲線設定顏色,0為第一條1為第二條,......
xylinerenderer.setSeriesPaint(0, new Color(0, 0 ,255 ));
xylinerenderer.setSeriesPaint(1, new Color(255 ,215, 0 ));
xylinerenderer.setSeriesPaint(2, new Color(0, 238, 118));