http://www.java2s.com/Code/Java/Chart/JFreeChartTimeSeriesDemo12.htm
위 샘플을 약간 변형한 샘플
package admin;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class chartMaker {
/**
* @param title
* @param sample 레이블용 스트링 어레이
* @param list 데이터 ArrayList<Object[DateX[],IntegerY[]] 형태
* @param width
* @param height
*/
public chartMaker( String title,String[] sample, ArrayList<Object[]> list, int width, int height) {
// super(title);
// XYDataset dataset = createDataset();
XYDataset dataset = createDataset(sample, list);
JFreeChart chart = createChart(dataset);
Font labelFont = null;
labelFont = chart.getTitle().getFont();
chart.getTitle().setFont(new Font("Malgun Gothic", labelFont.getStyle(), labelFont.getSize()));
chart.getLegend().setItemFont(new Font("Malgun Gothic", Font.PLAIN, 12));
XYPlot plot = chart.getXYPlot();
//가로축 제목
labelFont = plot.getDomainAxis().getLabelFont();
plot.getDomainAxis().setLabelFont(new Font("Malgun Gothic", labelFont.getStyle(), labelFont.getSize()));
//가로축 값에 대한 레이블
labelFont = plot.getDomainAxis().getTickLabelFont();
plot.getDomainAxis().setTickLabelFont(new Font("Arial", labelFont.getStyle(), 12));
//세로축 제목
labelFont = plot.getRangeAxis().getLabelFont();
plot.getRangeAxis().setLabelFont(new Font("Malgun Gothic", labelFont.getStyle(), labelFont.getSize()));
//세로축 값에 대한 레이블
labelFont = plot.getRangeAxis().getTickLabelFont();
plot.getRangeAxis().setTickLabelFont(new Font("Arial", labelFont.getStyle(), labelFont.getSize()));
FileOutputStream fos = null;
try{
fos = new FileOutputStream(new File(Util.getNicePath(admin.intf.AdminConstVars.DOC_ROOT + "/chart/chart.png")));
// 함수만 변경하면 gif ,png ... 등등 다른 이미지 형식으로 저장 가능
ChartUtilities.writeChartAsPNG(fos, chart, width, height, true, 100);
}catch(Exception ig){
System.out.println(ig.getMessage());
ig.printStackTrace();
}finally{
if(fos != null) try{fos.close();}catch(Exception ig){}
chart = null;
dataset = null;
}
}
private TimeSeriesCollection createDataset(String[] indexes, ArrayList<Object[]> list) {
final TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.setDomainIsPointsInTime(true);
for(int i=0;i<list.size();i++){
Object[] obj = (Object[])list.get(i);
Date[] x = (Date[])obj[0];
Integer[] y = (Integer[])obj[1];
final TimeSeries series = new TimeSeries (indexes[i], Day.class);
// SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
for(int j=0; j<x.length; j++){
series.add(new Day( x[j].getDate(), x[j].getMonth()+1, x[j].getYear()+1900), y[j]);
}
dataset.addSeries(series);
}
return dataset;
}
/**
* Creates a chart.
*
* @param dataset the data for the chart.
*
* @return a chart.
*/
private JFreeChart createChart(final XYDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"", // chart title
"조회일", // x axis label
"발생건수", // y axis label
dataset, // data
true, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(1, false);
plot.setRenderer(renderer);
// change the auto tick unit selection to integer units only...
final DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MM/dd"));
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
}