JFreeChart 折線圖

jfreechart採用TimeSeriesChart並更改熱點內容 

/**
*
*/
package com.huaxia.bank.test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
/**
* @author cuiran
*
*/
public class TimeSeriesTest  {
public final static String MONTH = "MONTH";
public final static String DAY = "DAY";
public final static String HOUR = "HOUR";
private JFreeChart rChart = null;     //圖表對象
public String chartTitle = "";        //圖表標題
public String chartXdesc = "";        //X軸標題
public String chartYdesc = "";        //Y軸標題
public String chartSeriesDesc = "";   //曲線說明
public String chartSeriesDesc1 = "";   //曲線說明
public int graphWidth = 600;          //默認寬度
public int graphHigh = 400;           //默認高度
public String timeFormat = "MM/yyyy"; // 按日:MM-dd ,按小時:hh:mm
// 用於標誌用戶選擇的是按哪種查詢統計週期類型(年、月、天、小時).
// 年:YEAR, 月:MONTH, 天:DAY, 小時:HOUR
public String periodType = "";
// 用於確定時間間隔
public int dateInterval = 1;
//統計結果數據集
TimeSeriesCollection statDataset = new TimeSeriesCollection();
TimeSeries monthSeries = null;  //月份統計圖數據集合
TimeSeries monthSeries1 = null;  //月份統計圖數據集合
TimeSeries daySeries = null;    //天數統計圖數據集合
TimeSeries hourSeries = null;   //小時統計圖數據集合
public void createTread(){
setTimeSeriesStatType();
}
/**
* 創建趨勢圖表
* @return JFreeChart 圖表對象JFreeChart
*/
private JFreeChart createTrendChart(){
JFreeChart _freeChart = ChartFactory.createTimeSeriesChart(chartTitle, chartXdesc, chartYdesc,
getTimeSeriesStatDataSet(), true, true, true);
_freeChart.setBackgroundPaint(Color.white);
XYPlot _xyplot = _freeChart.getXYPlot();
_xyplot.setOrientation(PlotOrientation.VERTICAL);
_xyplot.setBackgroundPaint(Color.lightGray);
_xyplot.setDomainGridlinePaint(Color.white);
_xyplot.setRangeGridlinePaint(Color.white);
_xyplot.setAxisOffset(new RectangleInsets(1.0, 2.0, 2.0, 10.0));
XYItemRenderer renderer= _xyplot.getRenderer();
DateFormat df=new SimpleDateFormat("yyyy年MM月");
NumberFormat nf= NumberFormat.getNumberInstance();
StandardXYToolTipGenerator toolg=new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,df,nf);
renderer.setToolTipGenerator(toolg);
//
DateAxis dateaxis = (DateAxis) _xyplot.getDomainAxis();
if (periodType.equalsIgnoreCase("MONTH")){
if (dateInterval > 0) {
dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, dateInterval));
}
}else if (periodType.equalsIgnoreCase("DAY")){
if (dateInterval > 0) {
dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, dateInterval));
}
}else if (periodType.equalsIgnoreCase("HOUR")){
if (dateInterval > 0) {
dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, dateInterval));
}
}
dateaxis.setDateFormatOverride(new SimpleDateFormat(timeFormat));
return _freeChart;
}
/**
* 增加走勢圖數據
* @param periodType 區間類型
* @param year  年份
* @param month 月份
* @param day   日期
* @param hour  時間
* @param statData 統計數據
*/
public void addTimeSeriesUnitData(int year, int month,int statData) {
if (periodType.equalsIgnoreCase("MONTH")){
if (monthSeries == null){
monthSeries = new TimeSeries(chartSeriesDesc,Month.class);
}
monthSeries.add(new Month(month, year), statData);
System.out.println("月");
}else if (periodType.equalsIgnoreCase("DAY")){
if (daySeries == null){
daySeries = new TimeSeries(chartSeriesDesc, Day.class);
}
//        daySeries.add(new Day(day, month, year), statData);
}else if (periodType.equalsIgnoreCase("HOUR")){
if (hourSeries == null){
hourSeries = new TimeSeries(chartSeriesDesc, Hour.class);
}
//        hourSeries.add(new Hour(hour, day, month, year), statData);
}
}
/**
* 增加走勢圖數據
* @param periodType 區間類型
* @param year  年份
* @param month 月份
* @param day   日期
* @param hour  時間
* @param statData 統計數據
*/
public void addTimeSeriesUnitDataAll(int year, int month,int statData) {
if (periodType.equalsIgnoreCase("MONTH")){
if (monthSeries1 == null){
monthSeries1 = new TimeSeries(chartSeriesDesc1,Month.class);
}
monthSeries1.add(new Month(month, year), statData);
System.out.println("月");
}else if (periodType.equalsIgnoreCase("DAY")){
if (daySeries == null){
daySeries = new TimeSeries(chartSeriesDesc1, Day.class);
}
//        daySeries.add(new Day(day, month, year), statData);
}else if (periodType.equalsIgnoreCase("HOUR")){
if (hourSeries == null){
hourSeries = new TimeSeries(chartSeriesDesc1, Hour.class);
}
//        hourSeries.add(new Hour(hour, day, month, year), statData);
}
}
/**
* 設置走勢圖統計的區間類型
* @param periodType 區間類型
*/
private void setTimeSeriesStatType() {
if (periodType.equalsIgnoreCase("MONTH")){
statDataset.addSeries(monthSeries);
statDataset.addSeries(monthSeries1);
}else if (periodType.equalsIgnoreCase("DAY")){
statDataset.addSeries(daySeries);
}else if (periodType.equalsIgnoreCase("HOUR")){
statDataset.addSeries(hourSeries);
}
}
/**
* 獲得時序圖的統計數據
* @return XYDataset 統計數據
*/
private XYDataset getTimeSeriesStatDataSet() {
statDataset.setDomainIsPointsInTime(true);
return statDataset;
}
public int getDateInterval() {
return dateInterval;
}
//字體配置方法(解決中文問題)
private static void configFont(JFreeChart chart) {
// 配置字體
Font xfont = new Font("宋體", Font.PLAIN, 12);// X軸
Font yfont = new Font("宋體", Font.PLAIN, 12);// Y軸
Font kfont = new Font("宋體", Font.PLAIN, 12);// 底部
Font titleFont = new Font("宋體", Font.BOLD, 25); // 圖片標題
XYPlot plot =  chart.getXYPlot();// 圖形的繪製結構對象
// 圖片標題
chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
// 底部
//          chart.getLegend().setItemFont(kfont);
// 橫軸框裏的標題字體
chart.getLegend().setItemFont(kfont);
// 橫軸列表字體
plot.getDomainAxis().setTickLabelFont(kfont);
// 橫軸小標題字體
plot.getDomainAxis().setLabelFont(kfont);
// Y 軸
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(yfont);
rangeAxis.setLabelPaint(Color.BLUE); // 字體顏色
rangeAxis.setTickLabelFont(yfont);
}
public void setDateInterval(int dateInterval) {
this.dateInterval = dateInterval;
}
public static void main(String arhs[]){
TimeSeriesTest trendChart = new TimeSeriesTest();
trendChart.chartTitle = "一年走勢圖";
trendChart.chartSeriesDesc = "確認數量";
trendChart.chartSeriesDesc1 = "風險數量";
trendChart.chartXdesc = "月份";
trendChart.chartYdesc = "數量";
trendChart.graphHigh = 400;
trendChart.graphWidth = 600;
trendChart.timeFormat = "yyyy年MM月";
trendChart.periodType = TimeSeriesTest.MONTH;
double baseData = 100.0;
double rData = baseData;
trendChart.addTimeSeriesUnitData(2011, 11,25);
trendChart.addTimeSeriesUnitData(2011, 12,45);
trendChart.addTimeSeriesUnitData(2012, 1, 50);
trendChart.addTimeSeriesUnitData(2012, 2,  80);
trendChart.addTimeSeriesUnitData(2012, 3, 30);
trendChart.addTimeSeriesUnitData(2012, 4,  10);
trendChart.addTimeSeriesUnitDataAll(2011, 11,45);
trendChart.addTimeSeriesUnitDataAll(2011, 12,65);
trendChart.addTimeSeriesUnitDataAll(2012, 1, 70);
trendChart.addTimeSeriesUnitDataAll(2012, 2,  90);
trendChart.addTimeSeriesUnitDataAll(2012, 3, 50);
trendChart.addTimeSeriesUnitDataAll(2012, 4, 40);
trendChart.createTread();
JFreeChart chart =trendChart.createTrendChart();
trendChart.configFont(chart);
final ChartFrame preview = new ChartFrame("一年走勢圖",chart);
preview.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent event) {
preview.dispose();
}
});
preview.pack();
//調整預覽窗口的大小和位置,適合屏幕,並且居中
//             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//
//             preview.setSize(screenSize.width,screenSize.height-50);//適合屏幕,50表示把工具欄要考慮在內
//
//             Dimension frameSize = preview.getSize();
//
//             if (frameSize.height > screenSize.height) {
//
//               frameSize.height = screenSize.height;
//
//             }
//
//             if (frameSize.width > screenSize.width) {
//
//               frameSize.width = screenSize.width;
//
//             }
//
//             preview.setLocation( (screenSize.width - frameSize.width) / 2,
//
//                        (screenSize.height - frameSize.height-50) / 2);
//顯示報表預覽窗口
preview.setVisible(true);
}
}

 

XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)_xyplot.getRenderer();
//設置網格背景顏色
_xyplot.setBackgroundPaint(Color.white);
//設置網格豎線顏色
_xyplot.setDomainGridlinePaint(Color.pink);
//設置網格橫線顏色
_xyplot.setRangeGridlinePaint(Color.pink);
//設置曲線圖與xy軸的距離
_xyplot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
//設置曲線是否顯示數據點
xylineandshaperenderer.setBaseShapesVisible(true);
//設置曲線顯示各數據點的值



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章