1,柱状图
package xu.chart.demo;
import java.awt.Font;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;
public class BarSimple1 {
public static void main( String[] args ){
// double[][] data = new double[][] {{1310}, {720}, {1130}, {440}};
// String[] rowKeys = {"猪肉", "牛肉","鸡肉", "鱼肉"};
// String[] columnKeys = {""};
double[][] data = new double[][] {{1310, 1220, 1110, 1000},
{720, 700, 680, 640},
{1130, 1020, 980, 800},
{440, 400, 360, 300}};
String[] rowKeys = {"猪肉", "牛肉","鸡肉", "鱼肉"};
String[] columnKeys = {"广州", "深圳", "东莞", "佛山"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("广州肉类销量统计图",
"肉类",
"销量",
dataset,
PlotOrientation.VERTICAL,
true,
false,
false
);
/*---------------------------设置字体:防口字乱码----------------------------------*/
//设置字体:线条或者柱状的说明....................栏
Font font1 = new Font("SimSun",10,20);
// chart.getLegend().setItemFont(font1);
//设置字体:标题
TextTitle title = new TextTitle("广州肉类销量统计图");
title.setFont(font1);
chart.setTitle(title);
CategoryPlot plot = chart.getCategoryPlot();//获得图表区域对象
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(true);
plot.setDomainAxis(domainAxis);
ValueAxis rAxis = plot.getRangeAxis();
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置Y轴坐标上的文字-----------*/
rAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置Y轴的标题文字------------*/
rAxis.setLabelFont(new Font("黑体",Font.PLAIN,15));
/*---------------------------设置字体:防口字乱码----------------------------------*/
//显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D); //将显示的数值位置抬高一点
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.3);
//设置地区、销量的显示位置
//将下方的“肉类”放到上方
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
//将默认放在左边的“销量”放到右方
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setRenderer(renderer);
FileOutputStream fos_jpg = null;
try {
fos_jpg=new FileOutputStream("D:\\simple1.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,640,480,null);
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2,饼状图
package xu.chart.demo;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;
public class PieSimple1 {
public static void main( String[] args ){
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("初中高级程序员", 0.55);
dataset.setValue("项目经理", 0.1);
dataset.setValue("系统分析师", 0.1);
dataset.setValue("软件架构师", 0.1);
dataset.setValue("其他", 0.2);
//通过工厂类生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图", dataset, true, false, false);
PiePlot pieplot = (PiePlot) chart.getPlot();
//没有数据的时候显示的内容
pieplot.setNoDataMessage("无数据显示");
pieplot.setCircular(false);
/*------------------------透明处理---------------------------*/
//设置开始角度
pieplot.setStartAngle(150D);
// //设置方向为”顺时针方向“
pieplot.setDirection(Rotation.CLOCKWISE);
pieplot.setLabelGap(0.02D);
//设置透明度,0.5F为半透明,1为不透明,0为全透明
pieplot.setForegroundAlpha(0.5F);
/*------------------------透明处理-----------------------*/
//显示:饼图的数值
//其中new StandardPieSectionLabelGenerator(
//("{0}: ({1},{2})"), NumberFormat.getNumberInstance(),
//new DecimalFormat("0.00%"))
// 的("{0}: ({1},{2})")是生成的格式,{0}表示section名,{1}表示section的值,{2}表示百分比。可以自定义。而new DecimalFormat("0.00%")表示小数点后保留两位。
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
("{0}: ({2})"), NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
/*---------------------------设置字体:防口字乱码----------------------------------*/
//设置字体:图形中的字体
pieplot.setLabelFont(new Font("宋体", 0, 12));
//设置字体:最底下一栏
Font font1 = new Font("宋体", 0, 12);
chart.getLegend().setItemFont(font1);
//设置字体:标题
TextTitle title = new TextTitle("IT行业职业分布图");
title.setFont(font1);
chart.setTitle(title);
/*---------------------------设置字体:防口字乱码----------------------------------*/
FileOutputStream fos_jpg = null;
try {
fos_jpg=new FileOutputStream("D:\\PIEsimple1.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,500,300,null);
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3,时序图
//设置X轴时间显示方式
dateaxis.setTickUnit(new DateTickUnit(DateTickUnitType.MONTH, 1, new SimpleDateFormat("MM-yyyy")));
package xu.chart.demo;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;
public class LineSimple {
public static void main( String[] args ){
//访问量统计时间线
TimeSeries timeSeries = new TimeSeries("阿蜜果blog访问量统计", Day.class);
TimeSeries timeSeries2006 = new TimeSeries("2006年度", Month.class);
TimeSeries timeSeries2007 = new TimeSeries("2007年度", Month.class);
//时间曲线数据集合
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
//构造数据集合
timeSeries.add(new Month(1, 2007), 11200);
timeSeries.add(new Month(2, 2007), 9000);
timeSeries.add(new Month(3, 2007), 6200);
timeSeries.add(new Month(4, 2007), 8200);
timeSeries.add(new Month(5, 2007), 8200);
timeSeries.add(new Month(6, 2007), 12200);
timeSeries.add(new Month(7, 2007), 13200);
timeSeries.add(new Month(8, 2007), 8300);
timeSeries.add(new Month(9, 2007), 12400);
timeSeries.add(new Month(10, 2007), 12500);
timeSeries.add(new Month(11, 2007), 13600);
timeSeries.add(new Month(12, 2007), 12500);
//构造数据集合
timeSeries2006.add(new Month(1, 2007), 7200);
timeSeries2006.add(new Month(2, 2007), 7000);
timeSeries2006.add(new Month(3, 2007), 4200);
timeSeries2006.add(new Month(4, 2007), 8200);
timeSeries2006.add(new Month(5, 2007), 7300);
timeSeries2006.add(new Month(6, 2007), 8200);
timeSeries2006.add(new Month(7, 2007), 9200);
timeSeries2006.add(new Month(8, 2007), 7300);
timeSeries2006.add(new Month(9, 2007), 9400);
timeSeries2006.add(new Month(10, 2007), 7500);
timeSeries2006.add(new Month(11, 2007), 6600);
timeSeries2006.add(new Month(12, 2007), 3500);
timeSeries2007.add(new Month(1, 2007), 10200);
timeSeries2007.add(new Month(2, 2007), 9000);
timeSeries2007.add(new Month(3, 2007), 6200);
timeSeries2007.add(new Month(4, 2007), 8200);
timeSeries2007.add(new Month(5, 2007), 8200);
timeSeries2007.add(new Month(6, 2007), 11200);
timeSeries2007.add(new Month(7, 2007), 13200);
timeSeries2007.add(new Month(8, 2007), 8300);
timeSeries2007.add(new Month(9, 2007), 10400);
timeSeries2007.add(new Month(10, 2007), 12500);
timeSeries2007.add(new Month(11, 2007), 10600);
timeSeries2007.add(new Month(12, 2007), 10500);
lineDataset.addSeries(timeSeries);
lineDataset.addSeries(timeSeries2006);
lineDataset.addSeries(timeSeries2007);
JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间线", "月份", "访问量", lineDataset, true, true, true);
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
//设置网格背景颜色
plot.setBackgroundPaint(Color.white);
//设置网格竖线颜色
plot.setDomainGridlinePaint(Color.pink);
//设置网格横线颜色
plot.setRangeGridlinePaint(Color.pink);
//设置曲线图与xy轴的距离
plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
//设置曲线是否显示数据点
xylineandshaperenderer.setBaseShapesVisible(true);
//设置曲线显示各数据点的值
XYItemRenderer xyitem = plot.getRenderer();
xyitem.setBaseItemLabelsVisible(true);
xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));
plot.setRenderer(xyitem);
/*---------------------------设置字体:防口字乱码----------------------------------*/
ValueAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(true);
plot.setDomainAxis(domainAxis);
ValueAxis rAxis = plot.getRangeAxis();
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置Y轴坐标上的文字-----------*/
rAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置Y轴的标题文字------------*/
rAxis.setLabelFont(new Font("黑体",Font.PLAIN,15));
//设置字体:线条或者柱状的说明....................
Font font1 = new Font("宋体",Font.PLAIN,15);
chart.getLegend().setItemFont(font1);
//设置子标题
TextTitle subtitle = new TextTitle("2006/2007年度访问量对比", new Font("黑体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
//设置主标题
chart.setTitle(new TextTitle("阿蜜果blog访问量统计", new Font("隶书", Font.ITALIC, 15)));
chart.setAntiAlias(true);
/*---------------------------设置字体:防口字乱码----------------------------------*/
FileOutputStream fos_jpg = null;
try {
fos_jpg=new FileOutputStream("D:\\Linesimple1.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,640,480,null);
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}