package com.jmyz.utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.File;
import java.io.IOException;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class LineImages extends ApplicationFrame {
private static final long serialVersionUID = -7167492993366589122L;
public LineImages(String title) {
super(title);
setContentPane(createLinePanel());
}
public static CategoryDataset createLineDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "row1", "2001-01-01");
dataset.addValue(200, "row1", "2001-01-05");
dataset.addValue(300, "row1", "2001-01-12");
dataset.addValue(500, "row1", "2001-01-24");
dataset.addValue(600, "row1", "2001-02-24");
dataset.addValue(700, "row1", "2001-03-24");
dataset.addValue(900, "row1", "2001-04-24");
dataset.addValue(2000, "row1", "2001-05-24");
return dataset;
}
public static JFreeChart createLineChart(CategoryDataset dataset) {
// 定义图表对象
JFreeChart chart = ChartFactory.createLineChart("曲线走势图", "时间", "点击率", dataset, PlotOrientation.VERTICAL, false, false, false);
// 获得图表显示对象
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 1000, 0, Color.red));
chart.setTitle(new TextTitle("车型曲线走势图", new Font("隶书", Font.ITALIC, 15)));
CategoryPlot plot = (CategoryPlot) chart.getPlot();
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
numberAxis .setAutoTickUnitSelection(false);
double unit=100d;//刻度的长度
NumberTickUnit ntu= new NumberTickUnit(unit);
numberAxis.setTickUnit(ntu);
// DateAxis xAxis = new DateAxis();
// xAxis.setRange(new Date(), new Date());
// xAxis.setAutoTickUnitSelection(false);
// xAxis.setTickUnit(new DateTickUnit(DateTickUnit.MINUTE,20));
// 设置图表标签字体
plot.setNoDataMessage("No data available");
plot.setForegroundAlpha(0.8f);
return chart;
}
public static JPanel createLinePanel() {
JFreeChart chart = createLineChart(createLineDataset());
return new ChartPanel(chart);
}
public static void getLineImage() {
// 曲线图标题
String title = "趋势分析";
// 曲线图X轴提示
String domain = "月份走势";
// 曲线图Y轴提示
String range = "应收余额";
// 曲线图自标题
String subtitleStr = "数据分析";
// 创建时间数据源
// 每一个TimeSeries在图上是一条曲线
TimeSeries ts = new TimeSeries("驿站");
for (int i = 1999; i < 2005; i++) {
for (int mon = 0; mon < 12; mon++) {
ts.add(new TimeSeriesDataItem(new Day(1, mon + 1, i), new Double(500 + Math.random() * 100)));
}
}
// 时间曲线数据集合
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(ts);
// 时间曲线元素
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain, range, dataset, true, true, false);
TextTitle subtitle = new TextTitle(subtitleStr, new Font("黑体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.red));
String fileName = "d:/line.jpeg";
try {
ChartUtilities.saveChartAsJPEG(new File(fileName), 1, chart, 800, 600);
// BufferedImage buffer = chart.createBufferedImage(800, 600);
// OutputStream stream = null;
// ChartUtilities.writeBufferedImageAsJPEG(stream, buffer);
} catch (IOException e) {
System.out.print("....Cant't Create image File");
}
}
public static void main(String[] args) {
LineImages line = new LineImages("公司");
line.pack();
RefineryUtilities.centerFrameOnScreen(line);
line.setVisible(true);
getLineImage();
}
}