您的位置:首页 > 移动开发 > Objective-C

关于使用jfreechart和cewolf的使用关键

2004-12-02 16:43 447 查看
1.生成一个不同类型的数据集

Charts and datasets
To create a chart using JFreeChart, you must create a Dataset, which you then use to create a JFreeChart. A Dataset contains the data that displays in the chart. JFreeChart features many different Dataset objects, which you can use to create assorted types of charts. Once you create a Dataset, you next create the actual chart. JFreeChart uses an object appropriately named JFreeChart to represent charts. You create JFreeChart objects from Dataset objects with the ChartFactory class. In the following examples, we will create pie, XY, and bar charts along with their corresponding Dataset objects.

Pie chart
A pie chart is created from a PieDataset. The following example creates a PieDataset using the DefaultPieDataset class, adds two values via the setValue() method, and then creates a pie chart with the ChartFactory's createPieChart() method. This example will create a pie chart with the title "Sample Pie Chart," a legend, and two slices: JavaWorld with 75 percent of the pie, and Other with the other 25 percent:

DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("JavaWorld", new Integer(75));
pieDataset.setValue("Other", new Integer(25));

JFreeChart chart = ChartFactory.createPieChart
                     ("Sample Pie Chart",   // Title
                      pieDataset,           // Dataset
                      true                  // Show legend 
                     );

XY chart
An XYDataset can create area, line, and step XY charts. The following example creates an XYDataset from a series of data containing three XY points. Next, ChartFactory's createAreaXYChart() method creates an area XY chart. In addition to parameters for title, dataset, and legend, createAreaXYChart() takes in the labels for the X and Y axes:

XYSeries series = new XYSeries("Average Size");
series.add(20.0, 10.0);
series.add(40.0, 20.0);
series.add(70.0, 50.0);
XYDataset xyDataset = new XYSeriesCollection(series);

JFreeChart chart = ChartFactory.createAreaXYChart
                     ("Sample XY Chart",  // Title
                      "Height",           // X-Axis label
                      "Weight",           // Y-Axis label
                      xyDataset,          // Dataset
                      true                // Show legend
                     );

Bar chart
A CategoryDataset can create numerous different charts, including horizontal and vertical bar charts. The following example creates a CatagoryDataset with two series of data and two categories, and then creates a 3D vertical bar chart from this dataset. This example creates a chart that compares the sales growth in two quarters over two years:

String[] seriesNames = new String[] {"2001", "2002"};
String[] categoryNames = new String[] {"First Quater",
                                       "Second Quater"};
Number[][] categoryData = new Integer[][] {{new Integer(20),
                                            new Integer(35)},
                                           {new Integer(40),
                                            new Integer(60)}
                                           };
CategoryDataset categoryDataset = new DefaultCategoryDataset
                                        (seriesNames,
                                         categoryNames,
                                         categoryData);

JFreeChart chart = ChartFactory.createVerticalBarChart3D
                     ("Sample Category Chart", // Title
                      "Quarters",              // X-Axis label
                      "Sales",                 // Y-Axis label
                      categoryDataset,         // Dataset
                      true                     // Show legend
                     );
                    
使用cewolf标签库进行图形开发,只需要实现produceDataset

http://cewolf.sourceforge.net/

DatasetProducer xyData = new DatasetProducer() {
  public Object produceDataset(Map params) {
   XYSeries xys = new XYSeries("Example XY Dataset");
   double last = 0.0;
   for (int i = -50; i <= 50; i++) {
    double y = last + ((Math.random() * 100) - 50.0);
    xys.add((double) i, y);
    last = y;
   }
   return new XYSeriesCollection(xys);
  }
  public String getProducerId() {
   return "XYDataProducer";
  }
  public boolean hasExpired(Map params, Date since) {
   return false;
  }
 };
 
实现鼠标的点击和提示
 LinkGenerator xyLG = new XYItemLinkGenerator() {
  public String generateLink(Object data, int series, int item) {
   return "#Series " + series;
  }
 };
 pageContext.setAttribute("xyLinkGenerator", xyLG);

 CategoryToolTipGenerator catTG = new CategoryToolTipGenerator() {
  public String generateToolTip(CategoryDataset dataset, int series, int index) {
   return String.valueOf(dataset.getValue(series, index));
  }
 };
 pageContext.setAttribute("categoryToolTipGenerator", catTG);

 PieToolTipGenerator pieTG = new PieToolTipGenerator() {
  public String generateToolTip(PieDataset dataset, Comparable section, int index) {
   return String.valueOf(index);
  }
 };
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息