风为裳

导航

在action里面创建pie图和柱状图

一、创建柱状图

 

首先必须在action中定义一个private JFreeChart chart;

然后set和get一下 ,在get中写如下方法

// 准备数据集   

DefaultCategoryDataset dcd = new DefaultCategoryDataset();   

dcd.setValue();

//方法介绍

* @param value  the value (<code>null</code> permitted).  

* @param rowKey  the row key (<code>null</code> not permitted).     

* @param columnKey  the column key (<code>null</code> not permitted).

public void setValue(Number value, Comparable rowKey,Comparable columnKey) {     

   this.data.setValue(value, rowKey, columnKey);    

   fireDatasetChanged();   

  }

JFreeChart chart = ChartFactory.createBarChart3D("客户投诉", "意见",  "意见数量", dcd, PlotOrientation.VERTICAL, true, true, false); 

CategoryPlot plot = (CategoryPlot) chart.getPlot();

  //设置图表中标签的字体   

CategoryAxis domainAxis = plot.getDomainAxis();//(柱状图的x轴)   

domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,15));//设置x轴坐标上的字体    

domainAxis.setLabelFont(new Font("宋体",Font.BOLD,15));//设置x轴上的标题的字体       

ValueAxis valueAxis = plot.getRangeAxis();//(柱状图的y轴)     

valueAxis.setTickLabelFont(new Font("宋体",Font.BOLD,15));//设置y轴坐标上的字体     

valueAxis.setLabelFont(new Font("宋体",Font.BOLD,15));//设置y轴坐标上的标题的字体        

//取得图表的下部分的字体   LegendTitle lt = chart.getLegend();   

//修改图例的字体   lt.setItemFont(new Font("黑体",Font.BOLD,15));  

 // 设置图例标题            

Font font = new java.awt.Font("黑体", java.awt.Font.CENTER_BASELINE, 50);   

TextTitle title = new TextTitle("人员级别分布");           

title.getBackgroundPaint();          

title.setFont(font);          

// 设置标题的字体颜色        

 title.setPaint(Color.RED);          

chart.setTitle(title);   

return chart;

 

二、定义Pie图

还是先在action中定义一个private JFreeChart chart;

然后set和get一下

// 准备数据集   DefaultPieDataset dpd = new DefaultPieDataset();

dpd.setValue();//方法介绍

 /**     

* Sets the data value for a key and sends a {@link DatasetChangeEvent} to     

* all registered listeners.   

* @param key  the key (<code>null</code> not permitted).     

* @param value  the value.     

* @throws IllegalArgumentException if <code>key</code> is     

*  <code>null</code>.     

*/    

public void setValue(Comparable key, double value) {        

  setValue(key, new Double(value));   

 }

// 使用工厂类创建饼图   

JFreeChart chart = ChartFactory.createPieChart3D("客户满意统计",  dpd,     true,     true,     false);

//获得图表的Plot对象   

PiePlot piePlot = (PiePlot) chart.getPlot();      

//设置图表中标签的字体  

piePlot.setLabelFont(new Font("微软雅黑",Font.ITALIC,15));      

//取得图表的图例   LegendTitle lt = chart.getLegend();   

//修改图例的字体   lt.setItemFont(new Font("黑体",Font.BOLD,15));

  // 设置图例标题            

Font font = new java.awt.Font("黑体", java.awt.Font.CENTER_BASELINE, 50);   

TextTitle title = new TextTitle("客户满意统计");          

 title.getBackgroundPaint();          

 title.setFont(font);         

 // 设置标题的字体颜色         

title.setPaint(Color.RED);         

 chart.setTitle(title);       

return chart;

 

 

 

 

posted on 2012-01-18 09:54  风为裳  阅读(213)  评论(0编辑  收藏  举报