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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值