JFreeChart统计图

package com.ue.jeebase.modules.ysj.test;

import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class Test {
	
	public static void main(String[] args) throws IOException {
		
		String CHART_PATH = "D:\\";
		FileOutputStream fosChart = null;
		try {
		    //文件夹不存在则创建
		    File file = new File(CHART_PATH);
		    if (!file.exists()) {
		        file.mkdirs();
		    }
		    String chartName = CHART_PATH + "jfx.jpg";
		    fosChart = new FileOutputStream(chartName);
		    //高宽的设置影响椭圆饼图的形状
//		    JFreeChart chart = createChart("标题", "x轴", "y轴", createDataset());
 		    JFreeChart chart = createLineChart("标题", "x轴", "y轴", createDataset());

// 		    JFreeChart chart = createPieChart3D("标题", getDataPieSetByUtil1(new double[]{2.5,4,3.5}, new String[]{"大","数","据"}), new String[]{"",""});
		    
//			ChartFrame chartFrame=new ChartFrame("某公司人员组织数据图",chart); 
		    ChartUtilities.writeChartAsPNG(fosChart, chart, 500, 500);
//		    return chartName;
		} catch (Exception e) {
		    e.printStackTrace();
		} finally {
		    try {
		        fosChart.close();
		    } catch (Exception e) {
		        e.printStackTrace();
		    }
		}
	        //chart要放在Java容器组件中,ChartFrame继承自java的Jframe类。该第一个参数的数据是放在窗口左上角的,不是正中间的标题。
//	        chartFrame.pack(); //以合适的大小展现图形
//	        chartFrame.setVisible(true);//图形是否可见
	}
	
	/**
	 * 创建柱状图
	 * @param chartTitle 图表标题
	 * @param xName      x轴标题
	 * @param yName      y轴标题
	 * @param dataset    数据集
	 * @return
	 */
	public static JFreeChart createChart(String chartTitle, String xName,
	        String yName, CategoryDataset dataset) {
	    /**
	     * createBarChart的参数分别为:
	     * 标题,横坐标标题,纵坐标标题,数据集,图标方向(水平、垂直)
	     * ,是否显示图例,是否显示tooltips,是否urls
	     */
	    JFreeChart chart = ChartFactory.createBarChart(
	            chartTitle, xName, yName,
	            dataset, PlotOrientation.VERTICAL,
	            true, true, false);
	    /**
	     * VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭,
	     * 使用的关闭抗锯齿后,字体尽量选择12到14号的宋体字,这样文字最清晰好看
	     */
	    chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
	            RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
	    // 背景色
	    chart.setBackgroundPaint(Color.white);
	    // 设置标题字体
	    chart.getTitle().setFont(new Font("宋体", Font.BOLD, 14));
	    // 图例背景色
	    chart.getLegend().setBackgroundPaint(new Color(110, 182, 229));
	    // 图例字体
	    chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
	 
	    CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
	    // 设置纵虚线可见
	    //categoryPlot.setDomainGridlinesVisible(true);
	    // 虚线色彩
	    //categoryPlot.setDomainGridlinePaint(Color.black);
	    // 设置横虚线可见
	    categoryPlot.setRangeGridlinesVisible(true);
	    // 虚线色彩
	    categoryPlot.setRangeGridlinePaint(Color.black);
	    // 设置柱的透明度
	    categoryPlot.setForegroundAlpha(1.0f);
	    //设置柱图背景色(注意,系统取色的时候要使用
	    //16位的模式来查看颜色编码,这样比较准确)
	    categoryPlot.setBackgroundPaint(new Color(110, 182, 229));
	 
	    /*
	     * categoryPlot.setRangeCrosshairVisible(true);
	     * categoryPlot.setRangeCrosshairPaint(Color.blue);
	     */
	 
	    // 纵坐标--范围轴
	    NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
	    // 纵坐标y轴坐标字体
	    numberAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
	    // 纵坐标y轴标题字体
	    numberAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
	    // 设置最高的一个 Item 与图片顶端的距离
	    // numberAxis.setUpperMargin(0.5);
	    // 设置最低的一个 Item 与图片底端的距离
	    // numberAxis.setLowerMargin(0.5);
	    // 设置刻度单位 为Integer
	    numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	 
	    // 横坐标--类别轴、域
	    CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
	    // 横坐标x轴坐标字体
	    categoryAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
	    // 横坐标x轴标题字体
	    categoryAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
	    // 类别轴的位置,倾斜度
	    categoryAxis.setCategoryLabelPositions(
	            CategoryLabelPositions.createUpRotationLabelPositions(
	                    0.5235987755982988D));
	    //横轴上的 Lable
	    //categoryAxis.setMaximumCategoryLabelWidthRatio(0.6f);
	    //是否完整显示
	    //设置距离图片左端距离
	    categoryAxis.setLowerMargin(0.1D);
	    // 设置距离图片右端距离
	    categoryAxis.setUpperMargin(0.1D);
	 
	    // 渲染 - 中间的部分
	    BarRenderer barRenderer = (BarRenderer) categoryPlot.getRenderer();
	    // 设置柱子宽度
	    barRenderer.setMaximumBarWidth(0.05);
	    // 设置柱子高度
	    barRenderer.setMinimumBarLength(0.2);
	    // 设置柱子边框颜色
	    barRenderer.setBaseOutlinePaint(Color.BLACK);
	    // 设置柱子边框可见
	    barRenderer.setDrawBarOutline(true);
	    // 设置柱的颜色
	    barRenderer.setSeriesPaint(0, new Color(0, 255, 0));
	    barRenderer.setSeriesPaint(1, new Color(0, 0, 255));
	    barRenderer.setSeriesPaint(2, new Color(255, 0, 0));
	    // 设置每个柱之间距离
	    barRenderer.setItemMargin(0.2D);
	    // 显示每个柱的数值,并修改该数值的字体属性
	    barRenderer.setIncludeBaseInRange(true);
	    barRenderer.setBaseItemLabelGenerator(  new StandardCategoryItemLabelGenerator());
	    barRenderer.setBaseItemLabelsVisible(true);
	 
	    return chart;
	}
		
	/**
	 * 柱状图数据集
	 *
	 * @return
	 */
	public static CategoryDataset createDataset() {
	    String str1 = "Java EE开发";
	    String str2 = "IOS开发";
	    String str3 = "Android开发";
	    String str4 = "1月";
	    String str5 = "2月";
	    String str6 = "3月";
	    String str7 = "4月";
	    String str8 = "5月";
	 
	    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	 
	    dataset.addValue(1.0D, str1, str4);
	    dataset.addValue(4.0D, str1, str5);
	    dataset.addValue(3.0D, str1, str6);
	    dataset.addValue(5.0D, str1, str7);
	    dataset.addValue(5.0D, str1, str8);
	 
	    dataset.addValue(5.0D, str2, str4);
	    dataset.addValue(7.0D, str2, str5);
	    dataset.addValue(6.0D, str2, str6);
	    dataset.addValue(8.0D, str2, str7);
	    dataset.addValue(4.0D, str2, str8);
	 
	    dataset.addValue(4.0D, str3, str4);
	    dataset.addValue(3.0D, str3, str5);
	    dataset.addValue(2.0D, str3, str6);
	    dataset.addValue(3.0D, str3, str7);
	    dataset.addValue(6.0D, str3, str8);
	    return dataset;
	}

	
	// 饼状图 数据集
	public static PieDataset getDataPieSetByUtil1(double[] data,
	        String[] datadescription) {
	 
	    if (data != null && datadescription != null) {
	        if (data.length == datadescription.length) {
	            DefaultPieDataset dataset = new DefaultPieDataset();
	            for (int i = 0; i < data.length; i++) {
	                dataset.setValue(datadescription[i], data[i]);
	            }
	            return dataset;
	        }
	    }
	    return null;
	}
	
	
	/**
	 * 生成折线图
	 * @param chartTitle 图的标题
	 * @param x          横轴标题
	 * @param y          纵轴标题
	 * @param dataset    数据集
	 * @return
	 */
	public static JFreeChart createLineChart(
	        String chartTitle, String x,
	        String y, CategoryDataset dataset) {
	 
	    // 构建一个chart
	    JFreeChart chart = ChartFactory.createLineChart(
	            chartTitle,
	            x,
	            y,
	            dataset,
	            PlotOrientation.VERTICAL,
	            true,
	            true,
	            false);
	    //字体清晰
	    chart.setTextAntiAlias(false);
	    // 设置背景颜色
	    chart.setBackgroundPaint(Color.WHITE);
	 
	    // 设置图标题的字体
	    Font font = new Font("隶书", Font.BOLD, 25);
	    chart.getTitle().setFont(font);
	 
	    // 设置面板字体
	    Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
	    // 设置图示的字体
	    chart.getLegend().setItemFont(labelFont);
	 
	    CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
	    // x轴 // 分类轴网格是否可见
	    categoryplot.setDomainGridlinesVisible(true);
	    // y轴 //数据轴网格是否可见
	    categoryplot.setRangeGridlinesVisible(true);
	    categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩
	    categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩
	    categoryplot.setBackgroundPaint(Color.lightGray);// 折线图的背景颜色
	 
	    // 设置轴和面板之间的距离
	    // categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
	 
	    // 横轴 x
	    CategoryAxis domainAxis = categoryplot.getDomainAxis();
	    domainAxis.setLabelFont(labelFont);// 轴标题
	    domainAxis.setTickLabelFont(labelFont);// 轴数值
	    // domainAxis.setLabelPaint(Color.BLUE);//轴标题的颜色
	    // domainAxis.setTickLabelPaint(Color.BLUE);//轴数值的颜色
	 
	    // 横轴 lable 的位置 横轴上的 Lable 45度倾斜 DOWN_45
	    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
	 
	    // 设置距离图片左端距离
	    domainAxis.setLowerMargin(0.0);
	    // 设置距离图片右端距离
	    domainAxis.setUpperMargin(0.0);
	 
	    // 纵轴 y
	    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
	    numberaxis.setLabelFont(labelFont);
	    numberaxis.setTickLabelFont(labelFont);
	    numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	    numberaxis.setAutoRangeIncludesZero(true);
        // 是否由系统定义数据轴  
	    numberaxis.setAutoTickUnitSelection(false);  
        // 数据轴数值单位大小  
        double unit = 2d;  
        // 一个数值单位  
        NumberTickUnit numberTickUnit = new NumberTickUnit(unit);  
        // 设置轴的数值单位并发送一个AxisChangeEvent所有注册的侦听器  
        numberaxis.setTickUnit(numberTickUnit);  
        
	    // 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!
	    LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
	            .getRenderer();
	    lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见
	    lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见
	 
	    // 显示折点数据
	    lineandshaperenderer
	            .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
	    lineandshaperenderer.setBaseItemLabelsVisible(true);
	 
	    return chart;
	}
	
	/**
	 * 生成饼图
	 * @param chartTitle 图的标题
	 * @param dataset 数据集
	 * @param pieKeys 分饼的名字集
	 * @return
	 */
	public static JFreeChart createPieChart3D(
	        String chartTitle,
	        PieDataset dataset,
	        String[] pieKeys) {
	 
	    JFreeChart chart = ChartFactory.createPieChart3D(
	            chartTitle,
	            dataset,
	            true,//显示图例
	            true,
	            false);
	 
	    //关闭抗锯齿,是字体清晰
	    chart.getRenderingHints().put(
	            RenderingHints.KEY_TEXT_ANTIALIASING,
	            RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
	    chart.setTextAntiAlias(false);
	    //图片背景色
	    chart.setBackgroundPaint(Color.white);
	    //设置图标题的字体重新设置title
	    Font font = new Font("隶书", Font.BOLD, 25);
	    chart.getTitle().setFont(font);
	    /*TextTitle title = new TextTitle(chartTitle);
	    title.setFont(font);
	    chart.setTitle(title);*/
	    //设置图例字体
	    chart.getLegend().setItemFont(new Font("宋体",Font.PLAIN,14));
	 
	    PiePlot3D plot = (PiePlot3D) chart.getPlot();
	    // 图片中显示百分比:默认方式
	 
	    // 指定饼图轮廓线的颜色
	    // plot.setBaseSectionOutlinePaint(Color.BLACK);
	    // plot.setBaseSectionPaint(Color.BLACK);
	 
	    // 设置无数据时的信息
	    plot.setNoDataMessage("无对应的数据,请重新查询。");
	 
	    // 设置无数据时的信息显示颜色
	    plot.setNoDataMessagePaint(Color.red);
	 
	    // 图片中显示百分比:自定义方式,{0} 表示选项,
	    //{1} 表示数值, {2} 表示所占比例 ,小数点后两位
	    plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
	            "{0}={1}({2})", NumberFormat.getNumberInstance(),
	            new DecimalFormat("0.00%")));
	    //图片显示字体
	    plot.setLabelFont(new Font("宋体", Font.TRUETYPE_FONT, 12));
	 
	    // 图例显示百分比:自定义方式, {0} 表示选项,
	    //{1} 表示数值, {2} 表示所占比例
	    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
	            "{0}={1}({2})"));
	 
	    // 指定图片的透明度(0.0-1.0)
	    plot.setForegroundAlpha(0.65f);
	    // 指定显示的饼图上圆形(false)还椭圆形(true)
	    plot.setCircular(false, true);
	 
	    // 设置第一个 饼块section 的开始位置,默认是12点钟方向
	    plot.setStartAngle(90);
	 
	    // // 设置分饼颜色
	    plot.setSectionPaint(pieKeys[0], new Color(244, 194, 144));
	    plot.setSectionPaint(pieKeys[1], new Color(144, 233, 144));
	 
	    return chart;
	}
	// 饼状图 数据集
	public static PieDataset getDataPieSetByUtil(double[] data,
	        String[] datadescription) {
	 
	    if (data != null && datadescription != null) {
	        if (data.length == datadescription.length) {
	            DefaultPieDataset dataset = new DefaultPieDataset();
	            for (int i = 0; i < data.length; i++) {
	                dataset.setValue(datadescription[i], data[i]);
	            }
	            return dataset;
	        }
	    }
	    return null;
	}
	
}
	
	
转自:http://www.huosen.net/archives/156.html

	

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值