Tutorial: Step 2

Provide a DatasetProducer

As Cewolf uses a MVC (Model-View-Control) approach the data which are shown in your chart are separated from the view defined in the JSP page. So you can change them separately. To provide the chart with the correct data you must provide an object which implements the interfacede.laures.cewolf.DatasetProducer. This object is asked to produce data every time a new chart must be rendered. Below you can see an example implementation of a DatasetProducer which could be used to provide data needed for our example scenario.

package de.laures.cewolf.example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.io.Serializable;
import org.jfree.data.CategoryDataset;
import org.jfree.data.DefaultCategoryDataset;
import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.DatasetProducer;
import de.laures.cewolf.CategoryItemLinkGenerator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.chart.entity.CategoryItemEntity;
import org.jfree.chart.tooltips.CategoryToolTipGenerator;

public class PageViewCountData implements DatasetProducer, Serializable {

// These values would normally not be hard coded but produced by
// some kind of data source like a database or a file
private final String[] categories = {"mon", "tue", "wen", "thu", "fri", "sat", "sun"};
private final String[] seriesNames = {"cewolfset.jsp", "tutorial.jsp", "testpage.jsp", "performancetest.jsp"};
private final Integer[] [] values = new Integer[seriesNames.length] [categories.length];

public Object produceDataset(Map params) throws DatasetProduceException {
log.debug("producing data.");
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int series = 0; series < seriesNames.length; series ++) {
int lastY = (int)(Math.random() * 1000 + 1000);
for (int i = 0; i < categories.length; i++) {
final int y = lastY + (int)(Math.random() * 200 - 100);
lastY = y;
dataset.addValue((double)y, seriesNames[series], categories[i]);
}
}
return dataset;
}

public boolean hasExpired(Map params, Date since) {
log.debug(getClass().getName() + "hasExpired()");
return (System.currentTimeMillis() - since.getTime()) > 5000;
}

public String getProducerId() {
return "PageViewCountData DatasetProducer";
}
}

As you can see this datasetproducer is not very useful. Normally this class would try to access a datasource (e.g. a database) to access the needed information. But to serve as an example it should do.

A DatasetProducer needs to implement three methods. The most important one is the produceDataset() method which actually produces the data which should be used to render a chart. This method takes a parameter map which is filled by some special tags of the JSP which will be explained later on.

The hasExpired() method is called by the Cewolf framework if there already exits a data object produced by this producer in Cewolf's data cache. When returning true the producer signalizes that the data formerly used has expired.

By providing an unique ID via the getProducerId() method the Cewolf framework identifies a producer type. Two producer instances with the same ID are supposed to produce the same data.

Compile the class and put it in the correct folder structure under your web application's /WEB-INF/classes directory to make it available for your application.

Step 3: Install the Cewolf Servlet in your Web Application>>

SourceForge Logo