jfreeChar 柱状图

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

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
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.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

public class CeateBarChartNew {
	private static JFreeChart chart;
	private static CategoryDataset dataset;
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//步骤1:创建XYDataset对象(准备数据)      
		createDataset();
    	//步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置      
    	createChart();      
    	//步骤3:将JFreeChart对象输出到文件,Servlet输出流等      
    	//saveAsFile(freeChart, "F:\\jfreechart\\lineXY.png", 600, 400);      
      
        //显示  
        ChartFrame frame = new ChartFrame("BarChart Tesing!", chart);
        frame.pack();
        frame.setVisible(true);
	}
	/**
	 * Create DataSet
	 */
	private static void createDataset() {
		double[][] data = new double[][] {{24, 32, 48, 54, 65},{7, 9, 14, 21, 31}};
		String[] rowKeys = {"合格","不合格"};
		String[] columnKeys = {"类别一","类别二","类别三","类别四","类别五"};
		dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
	}
	
	/**
	 * create chart
	 * @return
	 */
	@SuppressWarnings("deprecation")
	private static void createChart() {		
		chart = ChartFactory.createBarChart3D("柱状统计图",
                "X轴",
                "Y轴",
                dataset,
                PlotOrientation.VERTICAL//PlotOrientation.VERTICAL 让平行柱垂直显示,而 PlotOrientation.HORIZONTAL 则让平行柱水平显示。                 
                true,
                false,
                false);
		chart.setBackgroundPaint(Color.WHITE);
		setChartFont();
		CategoryPlot plot = chart.getCategoryPlot();
		
		ValueAxis rangeAxis = plot.getRangeAxis();
		//设置最高的一个 Item 与图片顶端的距离
		rangeAxis.setUpperMargin(0.15);
		//设置最低的一个 Item 与图片底端的距离
		rangeAxis.setLowerMargin(0.15);
		plot.setRangeAxis(rangeAxis);
		
		BarRenderer3D renderer = new BarRenderer3D();
		//设置平行柱之间距离
		renderer.setItemMargin(0.1);
		//显示每个柱的数值,并修改该数值的字体属性
		renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setItemLabelFont(new Font("黑体",Font.PLAIN,12));
		renderer.setItemLabelsVisible(true);
		plot.setRenderer(renderer);
	}
	
	/**
	 * Save chart to File    
	 * @param chart
	 * @param outputPath
	 * @param weight
	 * @param height
	 */
    public static void saveAsFile(JFreeChart chart, String outputPath,      
    								int weight, int height) {      
    	
    	FileOutputStream out = null;      
    	try {      
    		File outFile = new File(outputPath);      
    		if (!outFile.getParentFile().exists()) {      
    			outFile.getParentFile().mkdirs();      
    		}      
    		out = new FileOutputStream(outputPath);      
    		//保存为PNG      
    		ChartUtilities.writeChartAsPNG(out, chart, weight, height);      
    		// 保存为JPEG      
    		// ChartUtilities.writeChartAsJPEG(out, chart, weight, height);      
    		out.flush();      
    	} catch (FileNotFoundException e) {      
    		e.printStackTrace();      
    	} catch (IOException e) {      
    		e.printStackTrace();      
    	} finally {      
    		if (out != null) {      
    			try {      
    				out.close();      
    			} catch (IOException e) {      
    			// do nothing      
    			}      
    		}      
    	}      
    }  
    
    /**
	 * 图标乱码解决
	 */
	private static void setChartFont() {	
		Font title = new Font("楷体", Font.PLAIN, 15);
		Font f = new Font("楷体", Font.PLAIN, 12);
		//title(标题),
		TextTitle textTitle = chart.getTitle();   
		textTitle.setFont(title); 
			CategoryPlot plot = chart.getCategoryPlot();   
			CategoryAxis domainAxis = plot.getDomainAxis();     
			//设置X轴坐标上的文字     
			domainAxis.setTickLabelFont(f); 
			//设置X轴的标题文字     
			domainAxis.setLabelFont(f); 
			
			ValueAxis numberaxis = plot.getRangeAxis(); 
			//设置Y轴坐标上的文字     
			numberaxis.setTickLabelFont(f);	
			//设置Y轴的标题文字     
			numberaxis.setLabelFont(f);  
		chart.getLegend().setItemFont(f);
	}
}

jar包是jfreeCahr-1.0.13.jar

jfreeChar 柱状图

你可能感兴趣的:(servlet,F#,jfreechart)