本文共 7023 字,大约阅读时间需要 23 分钟。
用Jfree实现条形柱状图表,java代码实现。可经常用于报表的制作,代码自动生成后可以自由查看。可以自由配置图表的各个属性,用来达到自己的要求和目的。本文给大家介绍使用java实现各种数据统计图(柱形图,饼图,折线图),需要的朋友可以参考下
最近在做数据挖掘的课程设计,需要将数据分析的结果很直观的展现给用户,这就要用到数据统计图,要实现这个功能就需要几个第三方包了:
1. jfreechart-1.0.13.jar2. jcommon-1.0.16.jar3. gnujaxp.jar先来看一下,最终效果图:(三张图是一起的)效果图如下:
效果图如下:
三 实现饼状图的java代码:
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("苹果",100); dataset.setValue("梨子",200); dataset.setValue("葡萄",300); dataset.setValue("香蕉",400); dataset.setValue("荔枝",500); return dataset; } public ChartPanel getChartPanel(){ return frame1; } }
效果图如下:
四 实现折线图的java代码:
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; } }
效果图如下:
五 总结
以上都是一个简单的例子去实现了,想了解更深的同学可自行查询资料,其实以上代码都通俗易懂,只要结合自己的实际情况,便可开发出属于自己的Application,大家可以看出我这里是在Application上实现的,其实更多情况数据统计图在javaweb上应用更多,大家也可自行了解。