使用Java来进行数据分析,画图

在平时涉及到数据分析以及画图一般都是用MATLAB或者Python,在这两个之中都有封装好的库,可以直接使用造好的轮子。而在Java中自带jar包并没有相关功能,需要导入外部包

  1. jfreechart-1.0.13.jar
  2. jcommon-1.0.16.jar
  3. gnujaxp.jar
    运行结果如下图
    使用Java来进行数据分析,画图_第1张图片
    柱形图1代码段
package com.njue.testJFreeChart;

import java.awt.Font;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class BarChart {
	ChartPanel frame1;
	public  BarChart(){
		CategoryDataset dataset = getDataSet();
        JFreeChart chart = ChartFactory.createBarChart3D(
       		                 "共享单车", // 图表标题
                            "单车种类", // 目录轴的显示标签
                            "数量", // 数值轴的显示标签
                            dataset, // 数据集
                            PlotOrientation.VERTICAL, // 图表方向:水平、垂直
                            true,           // 是否显示图例(对于简单的柱状图必须是false)
                            false,          // 是否生成工具
                            false           // 是否生成URL链接
                            );
        
        //从这里开始
        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
        CategoryAxis domainAxis=plot.getDomainAxis();         //水平底部列表
         domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
         domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
         ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
         rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
          chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
          chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
          
          //到这里结束,虽然代码有点多,但只为一个目的,解决汉字乱码问题
          
         frame1=new ChartPanel(chart,true);        //这里也可以用chartFrame,可以直接生成一个独立的Frame
         
	}
	   private static CategoryDataset getDataSet() {
           DefaultCategoryDataset dataset = new DefaultCategoryDataset();
           dataset.addValue(100, "北京", "ofo");
           dataset.addValue(100, "上海", "ofo");
           dataset.addValue(100, "广州", "ofo");
           dataset.addValue(200, "北京", "酷奇");
           dataset.addValue(200, "上海", "酷奇");
           dataset.addValue(200, "广州", "酷奇");
           dataset.addValue(300, "北京", "青桔");
           dataset.addValue(300, "上海", "青桔");
           dataset.addValue(300, "广州", "青桔");
           dataset.addValue(400, "北京", "摩拜");
           dataset.addValue(400, "上海", "摩拜");
           dataset.addValue(400, "广州", "摩拜");
           dataset.addValue(500, "北京", "二八大杠");
           dataset.addValue(500, "上海", "二八大杠");
           dataset.addValue(500, "广州", "二八大杠");
           return dataset;
}
public ChartPanel getChartPanel(){
	return frame1;
	
}
}

柱形图2代码段

package com.njue.testJFreeChart;

import java.awt.Font;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
//这是柱形图的另一种效果,其实跟第一种相比都只有数据集发生了变化,再无其他变化
public class BarChart1 {
	ChartPanel frame1;
	public  BarChart1(){
		CategoryDataset dataset = getDataSet();
        JFreeChart chart = ChartFactory.createBarChart3D(
       		                 "共享单车", // 图表标题
                            "共享单车种类", // 目录轴的显示标签
                            "数量", // 数值轴的显示标签
                            dataset, // 数据集
                            PlotOrientation.VERTICAL, // 图表方向:水平、垂直
                            true,           // 是否显示图例(对于简单的柱状图必须是false)
                            false,          // 是否生成工具
                            false           // 是否生成URL链接
                            );
        
        //从这里开始
        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
        CategoryAxis domainAxis=plot.getDomainAxis();         //水平底部列表
         domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
         domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
         ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
         rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
          chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
          chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
          
          //到这里结束,虽然代码有点多,但只为一个目的,解决汉字乱码问题
          
         frame1=new ChartPanel(chart,true);        //这里也可以用chartFrame,可以直接生成一个独立的Frame
         
	}
	   private static CategoryDataset getDataSet() {
           DefaultCategoryDataset dataset = new DefaultCategoryDataset();
           dataset.addValue(100, "ofo", "ofo");
           dataset.addValue(200, "酷奇", "酷奇");
           dataset.addValue(300, "青桔", "青桔");
           dataset.addValue(400, "摩拜", "摩拜");
           dataset.addValue(500, "二八大杠", "二八大杠");
           return dataset;
}

public ChartPanel getChartPanel(){
	return frame1;
	
}
}

饼形图代码段

package com.njue.testJFreeChart;

import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

public class PieChart {
	ChartPanel frame1;
	public PieChart(){
		  DefaultPieDataset data = getDataSet();
	      JFreeChart chart = ChartFactory.createPieChart3D("单车数量",data,true,false,false);
	    //设置百分比
	      PiePlot pieplot = (PiePlot) chart.getPlot();
	      DecimalFormat df = new DecimalFormat("0.00%");//获得一个DecimalFormat对象,主要是设置小数问题
	      NumberFormat nf = NumberFormat.getNumberInstance();//获得一个NumberFormat对象
	      StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0}  {2}", nf, df);//获得StandardPieSectionLabelGenerator对象
	      pieplot.setLabelGenerator(sp1);//设置饼图显示百分比
	  
	  //没有数据的时候显示的内容
	      pieplot.setNoDataMessage("无数据显示");
	      pieplot.setCircular(false);
	      pieplot.setLabelGap(0.02D);
	  
	      pieplot.setIgnoreNullValues(true);//设置不显示空值
	      pieplot.setIgnoreZeroValues(true);//设置不显示负值
	     frame1=new ChartPanel (chart,true);
	      chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
	      PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
	      piePlot.setLabelFont(new Font("宋体",Font.BOLD,10));//解决乱码
	      chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10));
	}
    private static DefaultPieDataset getDataSet() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("ofo",100);
        dataset.setValue("酷奇",200);
        dataset.setValue("摩拜",300);
        dataset.setValue("青桔",400);
        dataset.setValue("二八大杠",500);
        return dataset;
}
    public ChartPanel getChartPanel(){
    	return frame1;
    	
    }
}

还有折线图代码段

package com.njue.testJFreeChart;

import java.awt.Font;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

public class TimeSeriesChart {
	ChartPanel frame1;
	public TimeSeriesChart(){
		XYDataset xydataset = createDataset();
		JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General单位信托基金价格", "日期", "价格",xydataset, true, true, true);
		XYPlot xyplot = (XYPlot) jfreechart.getPlot();
		DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
        dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
        frame1=new ChartPanel(jfreechart,true);
        dateaxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
        dateaxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
        ValueAxis rangeAxis=xyplot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
        jfreechart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
        jfreechart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体

	} 
	 private static XYDataset createDataset() {  //这个数据集有点多,但都不难理解
	        TimeSeries timeseries = new TimeSeries("legal & general欧洲指数信任",
	                org.jfree.data.time.Month.class);
	        timeseries.add(new Month(2, 2001), 181.80000000000001D);
	        timeseries.add(new Month(3, 2001), 167.30000000000001D);
	        timeseries.add(new Month(4, 2001), 153.80000000000001D);
	        timeseries.add(new Month(5, 2001), 167.59999999999999D);
	        timeseries.add(new Month(6, 2001), 158.80000000000001D);
	        timeseries.add(new Month(7, 2001), 148.30000000000001D);
	        timeseries.add(new Month(8, 2001), 153.90000000000001D);
	        timeseries.add(new Month(9, 2001), 142.69999999999999D);
	        timeseries.add(new Month(10, 2001), 123.2D);
	        timeseries.add(new Month(11, 2001), 131.80000000000001D);
	        timeseries.add(new Month(12, 2001), 139.59999999999999D);
	        timeseries.add(new Month(1, 2002), 142.90000000000001D);
	        timeseries.add(new Month(2, 2002), 138.69999999999999D);
	        timeseries.add(new Month(3, 2002), 137.30000000000001D);
	        timeseries.add(new Month(4, 2002), 143.90000000000001D);
	        timeseries.add(new Month(5, 2002), 139.80000000000001D);
	        timeseries.add(new Month(6, 2002), 137D);
	        timeseries.add(new Month(7, 2002), 132.80000000000001D);
	        TimeSeries timeseries1 = new TimeSeries("legal & general英国指数信任",
	                org.jfree.data.time.Month.class);
	        timeseries1.add(new Month(2, 2001), 129.59999999999999D);
	        timeseries1.add(new Month(3, 2001), 123.2D);
	        timeseries1.add(new Month(4, 2001), 117.2D);
	        timeseries1.add(new Month(5, 2001), 124.09999999999999D);
	        timeseries1.add(new Month(6, 2001), 122.59999999999999D);
	        timeseries1.add(new Month(7, 2001), 119.2D);
	        timeseries1.add(new Month(8, 2001), 116.5D);
	        timeseries1.add(new Month(9, 2001), 112.7D);
	        timeseries1.add(new Month(10, 2001), 101.5D);
	        timeseries1.add(new Month(11, 2001), 106.09999999999999D);
	        timeseries1.add(new Month(12, 2001), 110.3D);
	        timeseries1.add(new Month(1, 2002), 111.7D);
	        timeseries1.add(new Month(2, 2002), 111D);
	        timeseries1.add(new Month(3, 2002), 109.59999999999999D);
	        timeseries1.add(new Month(4, 2002), 113.2D);
	        timeseries1.add(new Month(5, 2002), 111.59999999999999D);
	        timeseries1.add(new Month(6, 2002), 108.8D);
	        timeseries1.add(new Month(7, 2002), 101.59999999999999D);
	        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
	        timeseriescollection.addSeries(timeseries);
	        timeseriescollection.addSeries(timeseries1);
	        return timeseriescollection;
	    }
	  public ChartPanel getChartPanel(){
	    	return frame1;
	    	
	    }
}

最后在主方法中将其调用

package com.njue.testJFreeChart;

import java.awt.GridLayout;

import javax.swing.JFrame;

public class mainClass {
public static void main(String args[]){
	JFrame frame=new JFrame("共享单车数据统计图");
	frame.setLayout(new GridLayout(2,2,10,10));
	frame.add(new BarChart().getChartPanel());           //添加柱形图
	frame.add(new BarChart1().getChartPanel());          //添加柱形图的另一种效果
	frame.add(new PieChart().getChartPanel());           //添加饼状图
	frame.add(new TimeSeriesChart().getChartPanel());    //添加折线图
	frame.setBounds(50, 50, 800, 600);
	frame.setVisible(true);
}
}

本文大部分来自于转载,很多技术细节已经标记在注释之中

你可能感兴趣的:(自学记录)