JFreeChart 实现各种数据统计图(柱形图,饼状图,折线图)

        最近在做数据分析相关的工作,需要将数据分析的结果直观的展示出来,以便进行微调。这里就需要用到柱形图,饼图,折线图等,Java里有JFreeChart 库来实现数据统计图,JFreeChart 官网:http://www.jfree.org/jfreechart/。


一 jar配置

当前使用的是官方最新的版本 1.0.19,使用时需导入 jcommon-1.0.23.jar和jfreechart-1.0.19.jar



二 示例

1.柱形图

示例代码:

package com.ricky.java.chart;

import java.awt.Font;

import javax.swing.JFrame;

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

public class BarChartDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		JFreeChart chart = ChartFactory.createBarChart("统计", "水果", "水果种类",
				getDataSet());

		// 从这里开始
		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));// 设置标题字体

		ChartPanel chartPanel = new ChartPanel(chart, true); // 这里也可以用chartFrame,可以直接生成一个独立的Frame
		JFrame frame=new JFrame("Java数据统计图");  
	    frame.add(chartPanel);           //添加柱形图  
	    frame.setBounds(50, 50, 900, 600);  
	    frame.setVisible(true);  
	}

	private static CategoryDataset getDataSet() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(100, "北京", "苹果");
		dataset.addValue(100, "上海", "苹果");
		dataset.addValue(100, "广州", "苹果");
		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;
	}

}

运行效果



2. 饼状图

示例代码:

package com.ricky.java.chart;

import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JFrame;
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 PieChartDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		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);// 设置不显示负值

		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));

		ChartPanel chartPanel = new ChartPanel(chart, true);
		JFrame frame = new JFrame("Java数据统计图");
		frame.add(chartPanel); // 添加柱形图
		frame.setBounds(0, 0, 900, 600);
		frame.setVisible(true);
	}

	private static DefaultPieDataset getDataSet() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("苹果", 100);
		dataset.setValue("梨子", 200);
		dataset.setValue("葡萄", 300);
		dataset.setValue("香蕉", 400);
		dataset.setValue("荔枝", 500);
		return dataset;
	}
}

运行结果



3. 折线图

示例代码:

package com.ricky.java.chart;

import java.awt.Font;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
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 TimeSeriesChartDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		XYDataset xydataset = createDataset();  
        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("天气变化数据", "日期", "价格",xydataset, true, true, true);  
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();  
        DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();  
        dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));  
        
        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));//设置标题字体  
        
        ChartPanel chartPanel = new ChartPanel(jfreechart,true);  
        JFrame frame = new JFrame("Java数据统计图");  
	    frame.add(chartPanel);           //添加柱形图  
	    frame.setBounds(50, 50, 900, 600);  
	    frame.setVisible(true);  
	}
	
	private static XYDataset createDataset() {  //这个数据集有点多,但都不难理解  
        TimeSeries timeseries = new TimeSeries("2012年 全年天气变化数据");  
        timeseries.add(new Month(1, 2012), 12.6d);  
        timeseries.add(new Month(2, 2012), 15.6d);  
        timeseries.add(new Month(3, 2012), 19.6d);  
        timeseries.add(new Month(4, 2012), 25.6d);  
        timeseries.add(new Month(5, 2012), 35.6d);  
        timeseries.add(new Month(6, 2012), 36.6d);  
        timeseries.add(new Month(7, 2012), 41.9d);  
        timeseries.add(new Month(8, 2012), 42.9d);  
        timeseries.add(new Month(9, 2012), 34.2d);  
        timeseries.add(new Month(10, 2012), 22.2d);  
        timeseries.add(new Month(11, 2012), 19.2d);  
        timeseries.add(new Month(12, 2012), 16.2d);  
        
        TimeSeries timeseries1 = new TimeSeries("2013年 全年天气变化数据");  
        timeseries1.add(new Month(1, 2013), 11.99d);  
        timeseries1.add(new Month(2, 2013), 13.99d);  
        timeseries1.add(new Month(3, 2013), 17.99d);  
        timeseries1.add(new Month(4, 2013), 19.99d);  
        timeseries1.add(new Month(5, 2013), 22.99d);  
        timeseries1.add(new Month(6, 2013), 29.99d);  
        timeseries1.add(new Month(7, 2013), 37.99d);  
        timeseries1.add(new Month(8, 2013), 39.99d);  
        timeseries1.add(new Month(9, 2013), 32.99d);  
        timeseries1.add(new Month(10, 2013), 21.99d);  
        timeseries1.add(new Month(11, 2013), 17.99d);  
        timeseries1.add(new Month(12, 2013), 12.99d);  
        
        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();  
        timeseriescollection.addSeries(timeseries);  
        timeseriescollection.addSeries(timeseries1);  
        return timeseriescollection;  
    }  

}

运行结果




源码下载地址:





  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用ApplicationFrame实现jfreechart动态折线图,需要遵循以下步骤: 1. 导入jfreechart库。 2. 创建一个ApplicationFrame实例,它将包含动态折线图。 3. 创建一个TimeSeries对象,它将存储图表中的数据。 4. 创建一个TimeSeriesCollection对象,它将包含所有TimeSeries对象。 5. 创建一个ChartPanel对象,它将显示动态折线图。 6. 创建一个JFreeChart对象,它将与ChartPanel关联。 7. 设置折线图的样式和属性。 8. 创建一个Timer对象,它将定期更新折线图。 9. 在Timer对象的ActionListener方法中,添加新数据到TimeSeries对象中,并刷新折线图。 下面是一个简单的示例代码,演示如何使用ApplicationFrame实现jfreechart动态折线图: ```java import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.data.time.Millisecond; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; public class DynamicLineChart extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private TimeSeries series; private Timer timer; public DynamicLineChart(final String title) { super(title); this.series = new TimeSeries("Random Data"); final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series); final JFreeChart chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(500, 270)); final JPanel content = new JPanel(new BorderLayout()); content.add(chartPanel); setContentPane(content); this.timer = new Timer(1000, this); this.timer.start(); } private JFreeChart createChart(final XYDataset dataset) { final JFreeChart result = ChartFactory.createTimeSeriesChart( "Dynamic Line Chart", "Time", "Value", dataset, true, true, false ); return result; } public void actionPerformed(final ActionEvent e) { final double factor = Math.random() * 0.1 + 0.95; final Millisecond now = new Millisecond(); this.series.add(new Millisecond(), Math.random() * factor); } public static void main(final String[] args) { final DynamicLineChart demo = new DynamicLineChart("Dynamic Line Chart"); demo.pack(); demo.setVisible(true); } } ``` 在这个示例中,我们创建了一个名为DynamicLineChart的类,它继承自JFrame,并实现了ActionListener接口,用于更新折线图。在构造函数中,我们创建了一个TimeSeries对象,将其添加到TimeSeriesCollection中,并将其与ChartPanel关联。我们还创建了一个定时器,每秒钟更新一次折线图。在ActionListener方法中,我们向TimeSeries对象中添加新数据,并使用ChartPanel对象的repaint()方法刷新折线图。最后,我们创建了一个DynamicLineChart对象,并调用它的pack()和setVisible()方法显示动态折线图

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值