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();

       }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值