JFreeChart的基本應用

生成餅狀圖

public class App {

    public static void main(String[] args) {

       try {

           //構造餅圖數據集

           DefaultPieDataset pds = new DefaultPieDataset();

           pds.setValue("weblogic", 500);

           pds.setValue("sun", 200);

           pds.setValue("oracle", 1000);

           pds.setValue("ibm", 800);

           pds.setValue("用友", 2000);

          

           Font font = new Font("宋體",Font.BOLD,20);

           //

           JFreeChart chart = ChartFactory.createPieChart3D(

                  "jee應用服務器所佔市場份額調查", pds, true, false, false);

           //設定標題的字體

           chart.getTitle().setFont(font);

           PiePlot plot = (PiePlot) chart.getPlot();

           //設置繪圖區的字體

           plot.setLabelFont(font);

          

           //設置開始角

           plot.setStartAngle(3.14 / 2);

          

           //產生分裂效果

           plot.setExplodePercent("weblogic", 0.1);

          

           //設置前景色透明度

           plot.setForegroundAlpha(0.7f);

          

           //設置背景色爲圖片

           Image image = ImageIO.read(new File("d:/sunset.jpg"));

           plot.setBackgroundImage(image);

          

           //設置背景透明度

           plot.setBackgroundAlpha(0f);

          

           //設置提示條的字體

           chart.getLegend().setItemFont(font);

          

           //創建標籤生成器

           StandardPieSectionLabelGenerator gor

= new StandardPieSectionLabelGenerator("{0}({2})");

           //設置標籤生成器

           plot.setLabelGenerator(gor);

           ChartUtilities.saveChartAsJPEG(new File("d:/kk.jpg"),

                  chart, 600, 300);

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

}

生成柱狀圖

public class AppBar {

    public static void main(String[] args) {

       try {

           DefaultCategoryDataset ds = new DefaultCategoryDataset();

           ds.addValue(100, "ibm", "1");

           ds.addValue(200, "google", "1");

           ds.addValue(500, "sun", "1");

           ds.addValue(800, "oracle", "1");

          

           ds.addValue(34, "ibm", "2");

           ds.addValue(256, "google", "2");

           ds.addValue(435, "sun", "2");

           ds.addValue(667, "oracle", "2");

          

           ds.addValue(123, "ibm", "3");

           ds.addValue(346, "google", "3");

           ds.addValue(732, "sun", "3");

           ds.addValue(245, "oracle", "3");

          

           Font font = new Font("宋體",Font.BOLD,20);

           //

           JFreeChart chart = ChartFactory.createBarChart3D("bar demo",

"月份", "銷售額", ds,PlotOrientation.VERTICAL, true, false, false);

           //設定標題的字體

           chart.getTitle().setFont(font);

           CategoryPlot plot = (CategoryPlot) chart.getPlot();

           //設置域軸(種類軸)標籤

           plot.getDomainAxis().setLabelFont(font);

           //設置範圍軸(值軸)標籤

           plot.getRangeAxis().setLabelFont(font);

           //設置前景色透明度

           plot.setForegroundAlpha(0.7f);

           //設置背景色爲圖片

           //設置背景透明度

           plot.setBackgroundAlpha(1f);

          

           //設置提示條的字體

           chart.getLegend().setItemFont(font);

           ChartUtilities.saveChartAsJPEG(new File("d:/barDemo.jpg"),

                                          chart, 600, 300);

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

}

生成線形圖

public class AppLine {

    public static void main(String[] args) {

       try {

           DefaultCategoryDataset ds = new DefaultCategoryDataset();

           ds.addValue(100, "ibm", "1");

           ds.addValue(200, "google", "1");

           ds.addValue(500, "sun", "1");

           ds.addValue(800, "oracle", "1");

          

           ds.addValue(34, "ibm", "2");

           ds.addValue(256, "google", "2");

           ds.addValue(435, "sun", "2");

           ds.addValue(667, "oracle", "2");

          

           ds.addValue(123, "ibm", "3");

           ds.addValue(346, "google", "3");

           ds.addValue(732, "sun", "3");

           ds.addValue(245, "oracle", "3");

          

           Font font = new Font("宋體",Font.BOLD,20);

           //

           JFreeChart chart = ChartFactory.createLineChart3D("lie demo", "月份",

                  "銷售額", ds,PlotOrientation.VERTICAL, true, false, false);

           //設定標題的字體

           chart.getTitle().setFont(font);

           CategoryPlot plot = (CategoryPlot) chart.getPlot();

           //設置域軸(種類軸)標籤

           plot.getDomainAxis().setLabelFont(font);

           //設置範圍軸(值軸)標籤

           plot.getRangeAxis().setLabelFont(font);

           //設置前景色透明度

           plot.setForegroundAlpha(0.7f);

           //設置背景色爲圖片

           //設置背景透明度

           plot.setBackgroundAlpha(1f);

          

           //設置提示條的字體

           chart.getLegend().setItemFont(font);

           ChartUtilities.saveChartAsJPEG(new File("d:/lineDemo.jpg"),

                  chart, 600, 300);

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章