专注于高品质PHP技术等信息服务于一体 [STIEMAP] [RSS]

百度提供的广告:
java
当前位置:首页 > 技术文档 > java >  > 
JavaJfreeChart 生成柱状图 扇形图 折线图

JfreeChart 生成柱状图 扇形图 折线图
1,柱形图
package chart;

import java.awt.Color;
import java.io.File;
import java.io.IOException;

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.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.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;

/**
 * 柱状图
 * @author Administrator
 *
 */
public class ZhuZhuang {

    /**
     * 最基本的柱状图
     */
    public static void tu1(){
       
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(610, "广州", "猪肉");
        dataset.addValue(220, "广州", "牛肉");
        dataset.addValue(530, "广州", "鸡肉");
        dataset.addValue(340, "广州", "鱼肉");
        JFreeChart chart = ChartFactory.createBarChart3D("肉类销量统计图",
                          "肉类",
                          "销量",
                          dataset,
                          PlotOrientation.VERTICAL,
                          false,
                          false,
                          false);
        try {
            ChartUtilities.saveChartAsJPEG(new File("d:/zhuzhuang-1.jpg"), chart, 700, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   
    /**
     * 3D柱状图
     */
    public static void tu2()
    {
        double[][] data = new double[][] {{1310}, {720}, {1130}, {440}};
        String[] rowKeys = {"猪肉", "牛肉","鸡肉", "鱼肉"};
        String[] columnKeys = {""};
        CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
        JFreeChart chart = ChartFactory.createBarChart3D("广州肉类销量统计图", "肉类",
        "销量",
        dataset,
        PlotOrientation.VERTICAL,
        true,
        false,
        false);
       
        try {
            ChartUtilities.saveChartAsJPEG(new File("d:/zhuzhuang-2.jpg"), chart, 700, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
       
    }
   
    /**
     * 3D柱状图-2 带分组
     */
    public static void tu3()
    {
        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);
       
        try {
            ChartUtilities.saveChartAsJPEG(new File("d:/zhuzhuang-3.jpg"), chart, 700, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
       
    }
   
    /**
     * 3D柱状图-3带数值标注
     */
    public static void tu4()
    {
        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,
                              true,
                              false);

            CategoryPlot plot = chart.getCategoryPlot();
            //设置网格背景颜色
            plot.setBackgroundPaint(Color.white);
            //设置网格竖线颜色
            plot.setDomainGridlinePaint(Color.pink);
            //设置网格横线颜色
            plot.setRangeGridlinePaint(Color.pink);
            //显示每个柱的数值,并修改该数值的字体属性

            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.setRenderer(renderer);
            //设置地区、销量的显示位置
            //将下方的“肉类”放到上方
            plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
            //将默认放在左边的“销量”放到右方
            plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
            try {
                ChartUtilities.saveChartAsJPEG(new File("d:/zhuzhuang-4.jpg"), chart, 700, 500);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
   
    //测试函数
    public static void main(String[] args) {
        tu1();
        tu2();
        tu3();
        tu4();
    }
}

2,折线图
package chart;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
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.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;

/**
 * 折线图
 *
 * @author Administrator
 *
 */
public class ZheXian {

    /**
     * 折线图 1
     */
    public static void tu1()
    {
        //访问量统计时间线
        TimeSeries timeSeries = new TimeSeries("阿蜜果blog访问量统计", 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);

        lineDataset.addSeries(timeSeries);
        JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间线", "月份", "访问量", lineDataset, true, true, true);
        //设置子标题
        TextTitle subtitle = new TextTitle("2007年度", new Font("黑体", Font.BOLD, 12));
        chart.addSubtitle(subtitle);
        //设置主标题
        chart.setTitle(new TextTitle("阿蜜果blog访问量统计", new Font("隶书", Font.ITALIC, 15)));
        chart.setAntiAlias(true);
        try {
            ChartUtilities.saveChartAsJPEG(new File("d:/zhexian-1.jpg"), chart, 700, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 双折线
     */
    public static void tu2()
    {
        //访问量统计时间线
        TimeSeries timeSeries2006 = new TimeSeries("2006年度", Month.class);
        TimeSeries timeSeries2007 = new TimeSeries("2007年度", Month.class);

        //时间曲线数据集合
        TimeSeriesCollection lineDataset = new TimeSeriesCollection();
        //构造数据集合
        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(timeSeries2006);
        lineDataset.addSeries(timeSeries2007);

        JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间线", "月份", "访问量", lineDataset, true, true, true);
        //设置子标题
        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);
        try {
            ChartUtilities.saveChartAsJPEG(new File("d:/zhexian-2.jpg"), chart, 700, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
       
    }
   
    public static void main(String[] arg0s) {
    }
}

3,扇形图
package chart;

import java.awt.Font;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

public class BingTu {

    /**
     * 饼图
     */
    public static void tu1()
    {
        //设置数据集
        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.setLabelFont(new Font("宋体", 0, 12));

        //没有数据的时候显示的内容
        pieplot.setNoDataMessage("无数据显示");
        pieplot.setCircular(false);
        pieplot.setLabelGap(0.02D);
        try {
            ChartUtilities.saveChartAsJPEG(new File("d:/bing-1.jpg"), chart, 700, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 饼图 透明度
     */
    public static void tu2()
    {
        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, true, false);
        //获得3D的水晶饼图对象
        PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot();
        //设置开始角度
        pieplot3d.setStartAngle(150D);
        //设置方向为”顺时针方向“
        pieplot3d.setDirection(Rotation.CLOCKWISE);
        //设置透明度,0.5F为半透明,1为不透明,0为全透明
        pieplot3d.setForegroundAlpha(0.5F);
        pieplot3d.setNoDataMessage("无数据显示");   
        try {
            ChartUtilities.saveChartAsJPEG(new File("d:/bing-2.jpg"), chart, 700, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   
    public static void main(String[] args) {
       
    }
}