![JFreeChart实现饼状图、柱状图和折线图以及各类报表 - lishirong - The CTO of LiShirong JFreeChart实现饼状图、柱状图和折线图以及各类报表 - lishirong - The CTO of LiShirong](http://img1.ph.126.net/ApCU3YA2SEbFsTaspLU25Q==/2528208240832235701.jpg)
实现代码如下:
package servlet;
import java.awt.Container;
import java.awt.Font;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* @Title: 饼状图的CS架构实现
* @Package servlet
* @Description:
* @author lzh
* @date Dec 5, 2011 3:17:25 PM
* @version V1.0
*/
public class PieCharts extends ApplicationFrame {
* @Title: 饼状图的CS架构实现
* @Package servlet
* @Description:
* @author lzh
* @date Dec 5, 2011 3:17:25 PM
* @version V1.0
*/
public class PieCharts extends ApplicationFrame {
public PieCharts(String s) {
super(s);
setContentPane(createDemoPanel());
// TODO Auto-generated constructor stub
}
super(s);
setContentPane(createDemoPanel());
// TODO Auto-generated constructor stub
}
public static JPanel createDemoPanel() {
// 生成图表面板
JFreeChart jfreechart=createChart(createDataset());
return new ChartPanel(jfreechart);
}
// 生成图表面板
JFreeChart jfreechart=createChart(createDataset());
return new ChartPanel(jfreechart);
}
private static JFreeChart createChart(Object createDataset) {
// TODO Auto-generated method stub
return null;
}
// TODO Auto-generated method stub
return null;
}
//生成饼图数据集对象
public static PieDataset createDataset(){
DefaultPieDataset defaultpiedataset=new DefaultPieDataset();
defaultpiedataset.setValue("MANAGER", 10.02D);
defaultpiedataset.setValue("MARKET", 20.23D);
defaultpiedataset.setValue("DEVELOP", 60.02D);
defaultpiedataset.setValue("OEM", 10.02D);
defaultpiedataset.setValue("OTHERS", 5.11D);
return defaultpiedataset;
}
public static PieDataset createDataset(){
DefaultPieDataset defaultpiedataset=new DefaultPieDataset();
defaultpiedataset.setValue("MANAGER", 10.02D);
defaultpiedataset.setValue("MARKET", 20.23D);
defaultpiedataset.setValue("DEVELOP", 60.02D);
defaultpiedataset.setValue("OEM", 10.02D);
defaultpiedataset.setValue("OTHERS", 5.11D);
return defaultpiedataset;
}
//生成图表主对象JFreeChart
public static JFreeChart createChart(PieDataset piedataset){
JFreeChart jfreechart=ChartFactory.createPieChart("CityInfoPort Company",piedataset,true,true,false);
//获得图表显示对象
PiePlot pieplot=(PiePlot)jfreechart.getPlot();
//设置图表标签字体
pieplot.setLabelFont(new Font("SansSerif",Font.BOLD,12));
pieplot.setNoDataMessage("No data available");
pieplot.setCircular(true);
pieplot.setLabelGap(0.01D);//间距
return jfreechart;
//定义图表对象
}
/**
* @Fields serialVersionUID :
* @author lzh
* @date Dec 5, 2011 3:18:05 PM
*/
private static final long serialVersionUID = 1L;
public static JFreeChart createChart(PieDataset piedataset){
JFreeChart jfreechart=ChartFactory.createPieChart("CityInfoPort Company",piedataset,true,true,false);
//获得图表显示对象
PiePlot pieplot=(PiePlot)jfreechart.getPlot();
//设置图表标签字体
pieplot.setLabelFont(new Font("SansSerif",Font.BOLD,12));
pieplot.setNoDataMessage("No data available");
pieplot.setCircular(true);
pieplot.setLabelGap(0.01D);//间距
return jfreechart;
//定义图表对象
}
/**
* @Fields serialVersionUID :
* @author lzh
* @date Dec 5, 2011 3:18:05 PM
*/
private static final long serialVersionUID = 1L;
/**
* @Title: main
* @Description:
* @param args
* @return void
* @author lzh
* @date Dec 5, 2011 3:17:25 PM
* @throws
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PieCharts fjc=new PieCharts("CityInfoPort Company");
fjc.pack();
RefineryUtilities.centerFrameOnScreen(fjc);
fjc.setVisible(true);
}
* @Title: main
* @Description:
* @param args
* @return void
* @author lzh
* @date Dec 5, 2011 3:17:25 PM
* @throws
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PieCharts fjc=new PieCharts("CityInfoPort Company");
fjc.pack();
RefineryUtilities.centerFrameOnScreen(fjc);
fjc.setVisible(true);
}
}
2.折线图的CS架构实现
![JFreeChart实现饼状图、柱状图和折线图以及各类报表 - lishirong - The CTO of LiShirong JFreeChart实现饼状图、柱状图和折线图以及各类报表 - lishirong - The CTO of LiShirong](http://img7.ph.126.net/BUD2uHnC2c5u99mTmn3ktQ==/1571193320016003667.jpg)
实现代码如下:
package servlet;
import java.awt.Container;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* @Title: 折线图的CS架构实现
* @Package servlet
* @Description:
* @author lzh
* @date Dec 5, 2011 3:39:41 PM
* @version V1.0
*/
public class LineCharts extends ApplicationFrame{
* @Title: 折线图的CS架构实现
* @Package servlet
* @Description:
* @author lzh
* @date Dec 5, 2011 3:39:41 PM
* @version V1.0
*/
public class LineCharts extends ApplicationFrame{
private static final long serialVersionUID = 0L;
public LineCharts(String s) {
super(s);
setContentPane(createDemoLine());
}
public LineCharts(String s) {
super(s);
setContentPane(createDemoLine());
}
public static void main(String args[]){
LineCharts fjc=new LineCharts("Line");
fjc.pack();
RefineryUtilities.centerFrameOnScreen(fjc);
fjc.setVisible(true);
}
//生成显示图表的面板
public static JPanel createDemoLine(){
JFreeChart jfreechart=createChart(createDataset());
return new ChartPanel(jfreechart);
}
LineCharts fjc=new LineCharts("Line");
fjc.pack();
RefineryUtilities.centerFrameOnScreen(fjc);
fjc.setVisible(true);
}
//生成显示图表的面板
public static JPanel createDemoLine(){
JFreeChart jfreechart=createChart(createDataset());
return new ChartPanel(jfreechart);
}
//生成图表主对象JFreeChart
private static JFreeChart createChart(DefaultCategoryDataset linedataset) {
//定义图表对象
JFreeChart chart=ChartFactory.createLineChart("Line","Time","Sale(million)",linedataset,PlotOrientation.VERTICAL,true,true,false);
CategoryPlot plot=chart.getCategoryPlot();
NumberAxis rangeAxis=(NumberAxis)plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI/2.0);
return chart;
}
private static JFreeChart createChart(DefaultCategoryDataset linedataset) {
//定义图表对象
JFreeChart chart=ChartFactory.createLineChart("Line","Time","Sale(million)",linedataset,PlotOrientation.VERTICAL,true,true,false);
CategoryPlot plot=chart.getCategoryPlot();
NumberAxis rangeAxis=(NumberAxis)plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI/2.0);
return chart;
}
//生成数据
private static DefaultCategoryDataset createDataset() {
DefaultCategoryDataset linedataset=new DefaultCategoryDataset();
//各曲线名称
String series1="TV";
String series2="PC";
String series3="WASH";
//横轴名称(列名称)
String type1="JAN";
String type2="FEB";
String type3="MAR";
linedataset.addValue(0.0,series1,type1);
linedataset.addValue(4.2,series1,type2);
linedataset.addValue(3.9,series1,type3);
linedataset.addValue(1.0,series2,type1);
linedataset.addValue(5.2,series2,type2);
linedataset.addValue(7.9,series2,type3);
linedataset.addValue(2.0,series3,type1);
linedataset.addValue(9.2,series3,type2);
linedataset.addValue(8.9,series3,type3);
return linedataset;
}
private static DefaultCategoryDataset createDataset() {
DefaultCategoryDataset linedataset=new DefaultCategoryDataset();
//各曲线名称
String series1="TV";
String series2="PC";
String series3="WASH";
//横轴名称(列名称)
String type1="JAN";
String type2="FEB";
String type3="MAR";
linedataset.addValue(0.0,series1,type1);
linedataset.addValue(4.2,series1,type2);
linedataset.addValue(3.9,series1,type3);
linedataset.addValue(1.0,series2,type1);
linedataset.addValue(5.2,series2,type2);
linedataset.addValue(7.9,series2,type3);
linedataset.addValue(2.0,series3,type1);
linedataset.addValue(9.2,series3,type2);
linedataset.addValue(8.9,series3,type3);
return linedataset;
}
}
3.柱状图的CS架构实现
3.柱状图的CS架构实现
![JFreeChart实现饼状图、柱状图和折线图以及各类报表 - lishirong - The CTO of LiShirong JFreeChart实现饼状图、柱状图和折线图以及各类报表 - lishirong - The CTO of LiShirong](http://img7.ph.126.net/y6cGf2ZFfTPMcE6cbvXuiQ==/2599421409940030797.jpg)
package servlet;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
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.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class BarCharts extends ApplicationFrame{
private static final long serialVersionUID = 1L;
public BarCharts(String title) {
super(title);
setContentPane(createDemoBar());
}
//生成显示图表的面板
public static JPanel createDemoBar() {
JFreeChart jfreechart=createChart(createDataset());
return new ChartPanel(jfreechart);
}
//生成图表主对象JFreeChart
public static JFreeChart createChart(CategoryDataset dataset) {
JFreeChart chart=ChartFactory.createBarChart3D("Fruit sales","Fruit","Sales",dataset,PlotOrientation.VERTICAL,true,false,false);//第三个true表示显示每个颜色柱子的柱名
CategoryPlot plot=chart.getCategoryPlot();//设置图的高级属性
BarRenderer3D renderer=new BarRenderer3D();//3D属性修改
renderer.setBaseOutlinePaint(Color.BLACK);//设置边框颜色为black
renderer.setWallPaint(Color.gray);//设置wall的颜色为gray
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());//设置柱顶数据,API中居然没有StandardCategoryItemLabelGenerator这个类
renderer.setItemLabelFont(new Font("黑体",Font.PLAIN,12));//设置柱顶数据字体
renderer.setItemLabelsVisible(true);//打开ItemLabel开关
plot.setRenderer(renderer);//将修改后的属性值保存到图中
plot.setForegroundAlpha(0.6f);
return chart;
}
//生成数据
public static CategoryDataset createDataset(){
double[][] data=new double[][]{{672,766,223,540,126},{325,521,210,340,106},{332,256,523,240,526}};//设置数据
String[] rowKeys={"Apple","Pear","BANANA"};//行标志
String[] columnKeys={"Peking","ShangHai","ChengDu","ChongQing","GuangZhou"};//列标志
CategoryDataset linedataset=DatasetUtilities.createCategoryDataset(rowKeys,columnKeys,data);//建立数据集
return linedataset;
}
public static void main(String args[]){
BarCharts fjc=new BarCharts("BarCharts");
fjc.pack();
RefineryUtilities.centerFrameOnScreen(fjc);
fjc.setVisible(true);
}
}