JFreeChat四種圖表的繪製例程
阿新 • • 發佈:2019-01-23
{
request.setCharacterEncoding("UTF-8");
// 曲線圖標題
String title ="趨勢分析";
// 曲線圖X軸提示
String domain ="年份";
// 曲線圖Y軸提示
String range ="GDP";
// 建立時間資料來源,每一個TimeSeries在圖上是一條曲線
TimeSeries chinaTs =new TimeSeries("中國");
Map<Integer, Integer> chinaht =new LinkedHashMap<Integer, Integer>();
chinaht.put(2002, 22100);
chinaht.put(2003, 14100);
chinaht.put(2004, 27100);
chinaht.put(2005, 38100);
chinaht.put(2006, 29000);
chinaht.put(2007, 90100);
for (Integer i:chinaht.keySet()) {
chinaTs.add( new TimeSeriesDataItem(new Day(1, 1, i),new Double(chinaht.get(i))));
}
// 建立時間資料來源,每一個TimeSeries在圖上是一條曲線
TimeSeries japanTs =new TimeSeries("日本");
Map<Integer, Integer> japanht =new LinkedHashMap<Integer, Integer>();
japanht.put(2000, 43900);
japanht.put( 2003, 63500);
japanht.put(2004, 43400);
japanht.put(2005, 63200);
japanht.put(2006, 53100);
japanht.put(2007, 32900);
for (Integer i:japanht.keySet()) {
japanTs.add(new TimeSeriesDataItem(new Day(1, 1, i),new Double(japanht.get(i))));
}
// 建立時間資料來源,每一個TimeSeries在圖上是一條曲線
TimeSeries usaTs =new TimeSeries("美國");
Map<Integer, Integer> usaht =new LinkedHashMap<Integer, Integer>();
usaht.put(2001, 100000);
usaht.put(2003, 159800);
usaht.put(2004, 109700);
usaht.put(2005, 129600);
usaht.put(2006, 9800);
usaht.put(2007, 149800);
for (Integer i:usaht.keySet()) {
usaTs.add(new TimeSeriesDataItem(new Day(1, 1, i),new Double(usaht.get(i))));
}
// 時間曲線資料集合
TimeSeriesCollection dataset =new TimeSeriesCollection();
dataset.addSeries(chinaTs);
dataset.addSeries(japanTs);
dataset.addSeries(usaTs);
// 時間曲線元素
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain,
range, dataset, true, true, false);
// 建立檔案並將圖片地址傳遞到頁面
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, request.getSession());
String graphURL = request.getContextPath() +"/displayChart?filename="
+ filename;
request.setAttribute("graphURL", graphURL);
// 標識
request.setAttribute("msg", "折線圖效果");
// 頁面轉向
RequestDispatcher dispatcher = request
.getRequestDispatcher("/web/page/chart.jsp");
dispatcher.forward(request, response);
return;
}
request.setCharacterEncoding("UTF-8");
// 曲線圖標題
String title ="趨勢分析";
// 曲線圖X軸提示
String domain ="年份";
// 曲線圖Y軸提示
String range ="GDP";
// 建立時間資料來源,每一個TimeSeries在圖上是一條曲線
TimeSeries chinaTs =new TimeSeries("中國");
Map<Integer, Integer>
chinaht.put(2002, 22100);
chinaht.put(2003, 14100);
chinaht.put(2004, 27100);
chinaht.put(2005, 38100);
chinaht.put(2006, 29000);
chinaht.put(2007, 90100);
for (Integer i:chinaht.keySet()) {
chinaTs.add(
}
// 建立時間資料來源,每一個TimeSeries在圖上是一條曲線
TimeSeries japanTs =new TimeSeries("日本");
Map<Integer, Integer> japanht =new LinkedHashMap<Integer, Integer>();
japanht.put(2000, 43900);
japanht.put(
japanht.put(2004, 43400);
japanht.put(2005, 63200);
japanht.put(2006, 53100);
japanht.put(2007, 32900);
for (Integer i:japanht.keySet()) {
japanTs.add(new TimeSeriesDataItem(new Day(1, 1, i),new Double(japanht.get(i))));
}
// 建立時間資料來源,每一個TimeSeries在圖上是一條曲線
TimeSeries usaTs =new TimeSeries("美國");
Map<Integer, Integer> usaht =new LinkedHashMap<Integer, Integer>();
usaht.put(2001, 100000);
usaht.put(2003, 159800);
usaht.put(2004, 109700);
usaht.put(2005, 129600);
usaht.put(2006, 9800);
usaht.put(2007, 149800);
for (Integer i:usaht.keySet()) {
usaTs.add(new TimeSeriesDataItem(new Day(1, 1, i),new Double(usaht.get(i))));
}
// 時間曲線資料集合
TimeSeriesCollection dataset =new TimeSeriesCollection();
dataset.addSeries(chinaTs);
dataset.addSeries(japanTs);
dataset.addSeries(usaTs);
// 時間曲線元素
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain,
range, dataset, true, true, false);
// 建立檔案並將圖片地址傳遞到頁面
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, request.getSession());
String graphURL = request.getContextPath() +"/displayChart?filename="
+ filename;
request.setAttribute("graphURL", graphURL);
// 標識
request.setAttribute("msg", "折線圖效果");
// 頁面轉向
RequestDispatcher dispatcher = request
.getRequestDispatcher("/web/page/chart.jsp");
dispatcher.forward(request, response);
return;
}