/**
* 图表测试
*
* @time 12:38:27 PM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class TestChart {
public static void main(String[] args) throws IOException {
// pieTest();
barTest();
}
/**
* 饼状图
*
* @param dataset
*
* @throws IOException
*/
public static void pieTest() throws IOException {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("吃饭", 0.5);
dataset.setValue("睡觉", 0.4);
dataset.setValue("coder", 0.12);
dataset.setValue("打弹弹", 0.08);
dataset.setValue("看电视", 0.48);
// 三维
// JFreeChart chart = ChartFactory.createPieChart3D("活动点用你每天的时间比",
// dataset, true, true, true);
// 二维
JFreeChart chart = ChartFactory.createPieChart("活动点用你每天的时间比", dataset, true, true, true);
// 中文乱码问题
Font font = new Font("宋体", Font.BOLD, 20);
chart.getTitle().setFont(font);
// 描述条字体
chart.getLegend().setItemFont(font);
// 图形中的图标
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(font);
// 设置背景透明度
plot.setBackgroundAlpha(.5f);
// 前景透明
plot.setForegroundAlpha(0.5f);
// 设置开始角度
plot.setStartAngle(.6f);
// 设置分裂效果
plot.setExplodePercent("ibm", 0.1);
// 添加子标题
/*
* TextTitle textTitle1 = new TextTitle(); textTitle1.setText("测试标题一");
* TextTitle textTitle2 = new TextTitle(); textTitle2.setText("测试标题二");
* chart.getSubtitles().add(textTitle1);
* chart.getSubtitles().add(textTitle2);
*/
// 保存在文件中
File file = new File("e:/jfreechar_test/test.jpg");
ChartUtilities.saveChartAsJPEG(file, chart, 800, 400);
// 保存到流中
// FileOutputStream fos = new
// FileOutputStream("e:/jfreechar_test/test2.jpg");
// ChartUtilities.writeChartAsJPEG(fos, chart, 800, 600);
// fos.close();
}
/**
* 柱状图
*
* @throws IOException
*/
public static void barTest() throws IOException {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(5000, "张三", "1月份");
dataset.addValue(6000, "张三", "2月份");
dataset.addValue(7000, "张三", "3月份");
dataset.addValue(1000, "李四", "1月份");
dataset.addValue(2000, "李四", "2月份");
dataset.addValue(3000, "李四", "3月份");
dataset.addValue(3000, "王五", "1月份");
dataset.addValue(4000, "王五", "2月份");
dataset.addValue(5000, "王五", "3月份");
JFreeChart chart = ChartFactory.createBarChart("季度工资表", "categoryAxis", "valueAxis", dataset, PlotOrientation.VERTICAL, true, true, true);
// 中文乱码问题
Font font = new Font("宋体", Font.BOLD, 20);
chart.getTitle().setFont(font);
chart.getLegend().setItemFont(font);
// 图形中的图标
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//plot.get
// 设置背景透明度
// plot.setBackgroundAlpha(.5f);
// 前景透明
// plot.setForegroundAlpha(0.5f);
// 保存到流中
FileOutputStream fos = new FileOutputStream("e:/jfreechar_test/test2.jpg");
ChartUtilities.writeChartAsJPEG(fos, chart, 800, 600);
fos.close();
}
}
* 图表测试
*
* @time 12:38:27 PM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class TestChart {
public static void main(String[] args) throws IOException {
// pieTest();
barTest();
}
/**
* 饼状图
*
* @param dataset
*
* @throws IOException
*/
public static void pieTest() throws IOException {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("吃饭", 0.5);
dataset.setValue("睡觉", 0.4);
dataset.setValue("coder", 0.12);
dataset.setValue("打弹弹", 0.08);
dataset.setValue("看电视", 0.48);
// 三维
// JFreeChart chart = ChartFactory.createPieChart3D("活动点用你每天的时间比",
// dataset, true, true, true);
// 二维
JFreeChart chart = ChartFactory.createPieChart("活动点用你每天的时间比", dataset, true, true, true);
// 中文乱码问题
Font font = new Font("宋体", Font.BOLD, 20);
chart.getTitle().setFont(font);
// 描述条字体
chart.getLegend().setItemFont(font);
// 图形中的图标
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(font);
// 设置背景透明度
plot.setBackgroundAlpha(.5f);
// 前景透明
plot.setForegroundAlpha(0.5f);
// 设置开始角度
plot.setStartAngle(.6f);
// 设置分裂效果
plot.setExplodePercent("ibm", 0.1);
// 添加子标题
/*
* TextTitle textTitle1 = new TextTitle(); textTitle1.setText("测试标题一");
* TextTitle textTitle2 = new TextTitle(); textTitle2.setText("测试标题二");
* chart.getSubtitles().add(textTitle1);
* chart.getSubtitles().add(textTitle2);
*/
// 保存在文件中
File file = new File("e:/jfreechar_test/test.jpg");
ChartUtilities.saveChartAsJPEG(file, chart, 800, 400);
// 保存到流中
// FileOutputStream fos = new
// FileOutputStream("e:/jfreechar_test/test2.jpg");
// ChartUtilities.writeChartAsJPEG(fos, chart, 800, 600);
// fos.close();
}
/**
* 柱状图
*
* @throws IOException
*/
public static void barTest() throws IOException {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(5000, "张三", "1月份");
dataset.addValue(6000, "张三", "2月份");
dataset.addValue(7000, "张三", "3月份");
dataset.addValue(1000, "李四", "1月份");
dataset.addValue(2000, "李四", "2月份");
dataset.addValue(3000, "李四", "3月份");
dataset.addValue(3000, "王五", "1月份");
dataset.addValue(4000, "王五", "2月份");
dataset.addValue(5000, "王五", "3月份");
JFreeChart chart = ChartFactory.createBarChart("季度工资表", "categoryAxis", "valueAxis", dataset, PlotOrientation.VERTICAL, true, true, true);
// 中文乱码问题
Font font = new Font("宋体", Font.BOLD, 20);
chart.getTitle().setFont(font);
chart.getLegend().setItemFont(font);
// 图形中的图标
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//plot.get
// 设置背景透明度
// plot.setBackgroundAlpha(.5f);
// 前景透明
// plot.setForegroundAlpha(0.5f);
// 保存到流中
FileOutputStream fos = new FileOutputStream("e:/jfreechar_test/test2.jpg");
ChartUtilities.writeChartAsJPEG(fos, chart, 800, 600);
fos.close();
}
}