Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203156
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: Java

2008-03-29 00:15:36

  创建一个Chart的三步:
  Creating charts with JFreeChart is a three step process. You need to:
    • create a dataset containing the data to be displayed in the chart;
    • create a JFreeChart object that will be responsible for drawing the chart;
    • draw the chart to some output target (often, but not always, a panel on the    screen);
 
 
Step one can be done easily using the DefaultPieDataset class, as follows:
// create a dataset...DefaultPieDataset dataset = new DefaultPieDataset();dataset.setValue("Category 1", 43.2);dataset.setValue("Category 2", 27.9);dataset.setValue("Category 3", 79.5);
/*Note that JFreeChart can create pie charts using data from any class that implements the PieDataset
interface.*/

Step two concerns how we will present the dataset created in the previous section. We need to
create a JFreeChart object that can draw a chart using the data from our pie dataset. We will use
the ChartFactory class, as follows:
// create a chart...
JFreeChart chart = ChartFactory.createPieChart(
"Sample Pie Chart",
dataset,
true, // legend?
true, // tooltips?
false // URLs?
);
#JFreeChart在绘制时有很多默认的选项,这里我们接受大多数的默认值:
The chart that we have created uses default settings for most attributes. There are many ways
to customise the appearance of charts created with JFreeChart, but in this example we will just
accept the defaults.

The final step is to display the chart somewhere. JFreeChart is very flexible about where it draws charts, thanks to its use of the Graphics2D class.
For now, let’s display the chart in a frame on the screen. The ChartFrame class contains the
machinery (a ChartPanel) required to display charts:
// create and display a frame...
ChartFrame frame = new ChartFrame("Test", chart);
frame.pack();
frame.setVisible(true);

#完整的程序
package com.biaoflying;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.data.general.DefaultPieDataset;

public class First {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DefaultPieDataset dataset=new DefaultPieDataset();
        dataset.setValue("Category 1", 43.2);
        dataset.setValue("Category 2", 27.9);
        dataset.setValue("Category 3", 79.5);
       
        JFreeChart chart=ChartFactory.createPieChart("Simple Pie Chart",
                dataset, true, true, false);
       
        ChartFrame frame=new ChartFrame("First",chart);
        frame.pack();
        frame.setVisible(true);
    }
}

-----------------------------------------------------------------------------------

  接下来主要介绍Pie Chart:
This chapter provides information about using some of the standard features of the pie charts in JFreeChart, including:
• controlling the color and outline of pie sections;
• handling of null and zero values;
• pie section labels (customising the text, altering the space allocated);
• “exploded” sections;
• multiple pie charts.
• displaying charts with a 3D effect;

1,改变扇区颜色
  import java.awt.Color;
  import org.jfree.chart.plot.PiePlot;
加上以下的代码:
  PiePlot plot=(PiePlot)chart.getPlot();
  plot.setSectionPaint("Category 1", new Color(1, 1, 1));
  plot.setSectionPaint("Category 2", new Color(200, 200, 255));
      
2,改变扇区的轮廓
  Section outlines are drawn, by default, as a thin grey line around each pie section. The PiePlot class provides options to:
• switch off the outlines completely;
eg:plot.setSectionOutlinesVisible(false);
• change the outlines for all sections by changing the default values;
• control the outline for particular pie sections independently;
 
3,Outline Appearance轮廓的外形
 
At the base layer, a default setting is defined—this is used when no higher level settings have been made. You can change the base settings with these methods in the PiePlot class:
public void setBaseSectionOutlinePaint(Paint paint);
public void setBaseSectionOutlineStroke(Stroke stroke);
  Sometimes, you may prefer to set the outline paint and stroke on a “per series” basis, perhaps to highlight a particular section in the chart. For this, you can use the series layer settings, defined via these methods:
public void setSectionOutlinePaint(Comparable key, Paint paint);
public void setSectionOutlineStroke(Comparable key, Stroke stroke);
The first argument for each method is the section key from the dataset. If you set the value for a section to null, the base layer setting will be used instead.

4,Null,非正数的处理
 
If a zero value is found in the dataset, the PiePlot class, by default, will place a label at the position
where the pie section would be displayed.If you prefer zero values to be ignored, you can set a flag for this, as follows:
  #忽略百分比为0的section.
  PiePlot plot = (PiePlot) chart.getPlot();
  plot.setIgnoreZeroValues(true);
 
  #null的处理
  A similar approach is taken for null values, which represent a missing or unknown value in the dataset. The default handling is the same as for zero values, and if you prefer null values to be ignored, you can set a flag as follows:

  PiePlot plot = (PiePlot) chart.getPlot();
  plot.setIgnoreNullValues(true);

 
#负值的处理
 
There does not seem to be a sensible way to represent negative values in a pie chart, and JFreeChart
will always ignore them.

  5,扇区和legend标签
 
The text used for the section labels, both on the chart and in the chart’s legend, is fully customisable.Default label generators are installed automatically, but if you need to you can change these withthe following methods:
  public void setLabelGenerator(PieSectionLabelGenerator generator);
  public void setLegendLabelGenerator(PieSectionLabelGenerator
generator);

 
The StandardPieSectionLabelGenerator class is typically used as the generator, and provides enough
flexibility to handle most custom labelling requirements.

  6,Exploded Sections
  The PiePlot class supports the display of “exploded” sections, in which a pie section is offset from the centre of the chart to highlight it.
  The amount by which a section is offset from the chart is specified as a percentage of the radius of the pie chart, for example 0.30 (30 percent) is used in the example.
  PiePlot plot = (PiePlot) chart.getPlot();
  plot.setExplodePercent("Section A", 0.30);

  7,实现3d饼状图
 
使用ChartFactory.createPieChart3D方法实现3d饼状图。若要进行扇区的设置,可以采用以下的步骤:
  PiePlot3D plot3d=(PiePlot3D)chart.getPlot();
  plot3d.setSectionPaint("Category 1", new Color(1, 1, 1));
  plot3d.setSectionPaint("Category 2", new Color(200, 200, 255));
  //3d饼状图不支持exploded section
  plot3d.setExplodePercent("Category 3",0.4);

  8,Multiple Pie Charts
  MultiplePiePlot,CategoryDataset
  ?如何绘制一个多重饼状图。

阅读(2587) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~