package cn.zhangao.jfreechart;
import java.awt.Font;
import java.io.File;
import javax.imageio.ImageIO;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class AppLine {
public static void main(String[] args) {
try {
//种类数据集
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.setValue(100, "ibm", "第一季度");
ds.setValue(200, "ibm", "第二季度");
ds.setValue(600, "ibm", "第三季度");
ds.setValue(500, "google", "第一季度");
ds.setValue(333, "google", "第二季度");
ds.setValue(780, "google", "第三季度");
ds.setValue(600, "用友", "第一季度");
ds.setValue(1500, "用友", "第二季度");
ds.setValue(300, "用友", "第三季度");
Font font = new Font("宋体", Font.BOLD, 20);
//创建柱状图
JFreeChart chart = ChartFactory.createLineChart("前三季度各大公司JEEAS市场销售情况", "季度", "销量(万台)", ds, PlotOrientation.VERTICAL, true, true, true);
//设置整个图片的标题字体
chart.getTitle().setFont(font);
//设置提示条字体
font = new Font("宋体", Font.BOLD, 15);
chart.getLegend().setItemFont(font);
//得到绘图区
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//得到绘图区的域轴(横轴),设置标签的字体
plot.getDomainAxis().setLabelFont(font);
//设置横轴标签项字体
plot.getDomainAxis().setTickLabelFont(font);
//设置范围轴(纵轴)字体
plot.getRangeAxis().setLabelFont(font);
//存储成图片
//设置chart的背景图片
chart.setBackgroundImage(ImageIO.read(new File("d:/sunset.bmp")));
plot.setBackgroundImage(ImageIO.read(new File("d:/Water.jpg")));
plot.setForegroundAlpha(1.0f);
ChartUtilities.saveChartAsJPEG(new File("d:/line.jpg"), chart, 600, 400);
} catch (Exception e) {
e.printStackTrace();
}
}
}