JFreeChart创建区域图

前言

  只是一个demo,效果:运行程序后保存生成的图表。

过程

  1.需要的jar包可以从官网上下载
   jcommon-1.0.23.jar
   jfreechart-1.0.19-experimental.jar
   jfreechart-1.0.19.jar
   mysql-connector-java-5.1.34.jar,数据库可以取对应数据库的jar包,这里用的是mysql
   2.代码
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 java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.jfree.chart.ChartFactory;  
import org.jfree.chart.ChartUtilities;  
import org.jfree.chart.JFreeChart;  
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;  
import org.jfree.data.category.CategoryDataset;  
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;  

public class CreateJFreeChartArea {
	
	/**    
     * 创建JFreeChart Area Chart(区域图)    
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
     */    
    public static void main(String[] args) throws ClassNotFoundException, SQLException {     
        //步骤1:创建CategoryDataset对象(准备数据)    	
        CategoryDataset dataset = createDataset();     
        //步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置     
        JFreeChart freeChart = createChart(dataset);     
        //步骤3:将JFreeChart对象输出到文件,Servlet输出流等       
        saveAsFile(freeChart, "G:\\jfreechart\\area1.png", 500, 400);
    }     
         
    //保存为文件     
    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     
                }     
            }     
        }     
    }     
    
    //根据CategoryDataset创建JFreeChart对象     
    public static JFreeChart createChart(CategoryDataset categoryDataset) {     
        //创建JFreeChart对象:ChartFactory.createAreaChart     
        JFreeChart jfreechart = ChartFactory.createAreaChart("销售曲线汇总",    //标题     
                "月份",    //categoryAxisLabel (category轴,横轴,X轴标签)     
                "数量",    //valueAxisLabel(value轴,纵轴,Y轴的标签)     
                categoryDataset, // dataset     
                PlotOrientation.VERTICAL,     
                true, // legend     
                false, // tooltips     
                false); // URLs     
             
        //使用CategoryPlot设置各种参数。以下设置可以省略。     
        CategoryPlot  plot = (CategoryPlot) jfreechart.getPlot(); 
        
        //背景色 透明度     
        plot.setBackgroundAlpha(0f);     
        //前景色 透明度     
        plot.setForegroundAlpha(0.5f);     
        //其他设置 参考 CategoryPlot类     
        jfreechart.setBackgroundPaint(Color.white); // 设定背景色为白色
        CategoryPlot categoryplot = jfreechart.getCategoryPlot(); // 获得
                       
        categoryplot.setBackgroundPaint(Color.lightGray); // 设定图表数据显示部分背景色
        categoryplot.setDomainGridlinePaint(Color.white); // 横坐标网格线白色
        categoryplot.setDomainGridlinesVisible(false); // 可见
        categoryplot.setRangeGridlinePaint(Color.white); // 纵坐标网格线白色
        
        // 解决中文乱码问题,共要处理这三部分
        // 1、对标题
        Font font1 = new Font("SansSerif", 10, 20); // 设定字体、类型、字号
        // Font font1 = new Font("SimSun", 10, 20); //也可以
        jfreechart.getTitle().setFont(font1); // 标题
        // 2、对图里面的汉字设定,也就是Plot的设定
        Font font2 = new Font("SansSerif", 10, 16); // 设定字体、类型、字号
        categoryplot.getDomainAxis().setLabelFont(font2);// 相当于横轴或理解为X轴
        categoryplot.getRangeAxis().setLabelFont(font2);// 相当于竖轴理解为Y轴
        // 3、下面的方块区域是 LegendTitle 对象
        Font font3 = new Font("SansSerif", 10, 12); // 设定字体、类型、字号
        jfreechart.getLegend().setItemFont(font3);// 最下方
             
        return jfreechart;     
    }     
    
    /**    
     * 创建CategoryDataset对象    
     * @throws SQLException 
     * @throws ClassNotFoundException 
     *     
     */    
    public static CategoryDataset createDataset() throws SQLException, ClassNotFoundException {     
             
    	/* Create SQL Database Connection */     
        Class.forName( "com.mysql.jdbc.Driver" );
        Connection connect = DriverManager.getConnection( 
        "jdbc:mysql://localhost:3306/charttest" ,     
        "root",     
        "123");
        
        Statement statement = connect.createStatement( );
        ResultSet resultSet = statement.executeQuery("SELECT product.Product_Name,sale.year,sale.month,sale.sales FROM sale INNER JOIN product ON product.Product_ID=sale.product_Id where product.Product_ID='11' AND year='2017' ORDER BY month" );
        DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
        
        while( resultSet.next( ) ) 
        {
        	 categoryDataset.addValue(Double.parseDouble( resultSet.getString( "sales" )), 
        	 resultSet.getString( "Product_Name" ),  resultSet.getString( "month" ));
        }
        return categoryDataset;
    }     

}
  3.效果图

  4.不想要显示坐标轴和刻度可以添加如下代码
//区域框不显示 
        plot.setOutlinePaint(null);
        plot.setDomainGridlinesVisible(false);
        plot.setRangeGridlinesVisible(false);
        
        //坐标轴不显示
        plot.getDomainAxis().setAxisLineVisible(false);
        plot.getRangeAxis().setAxisLineVisible(false);
       
        //设置标尺不显示
        plot.getRangeAxis().setTickMarksVisible(false);
        plot.getDomainAxis().setTickMarksVisible(false);
        //标尺数值不显示
        plot.getDomainAxis().setTickLabelsVisible(false);
        plot.getRangeAxis().setTickLabelsVisible(false);
  5.执行结果

参考

  http://www.jfree.org/jfreechart/api/javadoc/index.html
   http://blog.csdn.net/xxg3053/article/details/7359756
   http://zhoujingxian.iteye.com/blog/563313
   主要还是我看官网的api能力不足,找的慢还不太会使难过,以后还是要多看多练才能更熟悉。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值