JfreeChart画饼图,折线图,柱状图

1:饼图:

package test;

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;

public class PieCharts  extends ApplicationFrame{
public PieCharts(String s){
     super(s);
     setContentPane(createDemoPanel());
   }

// 生成显示图表的面板
   public static JPanel createDemoPanel(){
     JFreeChart jfreechart = createChart(createDataset());
     return new ChartPanel(jfreechart);
   }
 
// 生成饼图数据集对象
   public static PieDataset createDataset(){
     DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
     defaultpiedataset.setValue("管理人员",10.02D);
     defaultpiedataset.setValue("市场人员",20.23D);
     defaultpiedataset.setValue("开发人员",60.02D);
     defaultpiedataset.setValue("OEM人员",10.02D);
     defaultpiedataset.setValue("其他人员",5.11D);
   
     return defaultpiedataset;
   }
 
   //生成图表主对象JFreeChart
   public static JFreeChart createChart(PieDataset piedataset){
     //定义图表对象
     JFreeChart jfreechart = ChartFactory.createPieChart("CityInfoPort公司组织架构图",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;
   }
 
   public static void main(String[] args){
      PieCharts fjc = new PieCharts("CityInfoPort公司组织架构图");
      fjc.pack();
      RefineryUtilities.centerFrameOnScreen(fjc);
      fjc.setVisible(true);
    }



}

2:折线图

package test;

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;

public class LineCharts extends ApplicationFrame {

public LineCharts(String s) {
  super(s);
  setContentPane(createDemoLine());
}

public static void main(String[] args) {
  LineCharts fjc = new LineCharts("折线图");
  fjc.pack();
  RefineryUtilities.centerFrameOnScreen(fjc);
  fjc.setVisible(true);

}

// 生成显示图表的面板
public static JPanel createDemoLine() {
  JFreeChart jfreechart = createChart(createDataset());
  return new ChartPanel(jfreechart);
}

// 生成图表主对象JFreeChart
public static JFreeChart createChart(DefaultCategoryDataset linedataset) {
  //定义图表对象
  JFreeChart chart = ChartFactory.createLineChart("折线图", // chart title
    "时间", // domain axis label
    "销售额(百万)", // range axis label
    linedataset, // data
    PlotOrientation.VERTICAL, // orientation
    true, // include legend
    true, // tooltips
    false // urls
    );
  CategoryPlot plot = chart.getCategoryPlot();
  // customise the range axis...
  NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
  rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  rangeAxis.setAutoRangeIncludesZero(true);
  rangeAxis.setUpperMargin(0.20);
  rangeAxis.setLabelAngle(Math.PI / 2.0);

  return chart;
}

//生成数据
public static DefaultCategoryDataset createDataset() {
  DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
  //  各曲线名称
  String series1 = "冰箱";
  String series2 = "彩电";
  String series3 = "洗衣机";

  //    横轴名称(列名称)
  String type1 = "1月";
  String type2 = "2月";
  String type3 = "3月";

  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:柱状图:

package test;

import java.awt.Color;

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 {

public BarCharts(String s) {
  super(s);
  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("水果销量统计图", //建立3D柱状图
    "水果",//横轴名称
    "销量",//纵轴名称
    dataset,//数据集
    PlotOrientation.VERTICAL,//纵向显示
    true,//显示每个颜色柱子的柱名
    false, false);
  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 = { "苹果", "梨子", "葡萄" };// 行标志
  String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };// 列标志
  CategoryDataset linedataset = DatasetUtilities.createCategoryDataset(
    rowKeys, columnKeys, data); // 建立数据集

  return linedataset;
}

public static void main(String[] args) {
  BarCharts fjc = new BarCharts("柱状图");
  fjc.pack();
  RefineryUtilities.centerFrameOnScreen(fjc);
  fjc.setVisible(true);

}
}

你可能感兴趣的:(UI,swing,jfreechart)