Zbavíme se závislosti na Log4J, který se stejně nepoužívá.
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
4 <title>Cewolf Tutorial(2): Provide a DatasetProducer</title>
5 <meta name="author" content="Guido Laures">
6 <link href="../cewolf.css" rel="stylesheet" type="text/css">
9 <h1>Tutorial: Step 2</h1>
10 <h2>Provide a DatasetProducer</h2>
11 <p> As Cewolf uses a MVC (Model-View-Control) approach the data which
12 are shown in your chart are separated from the view defined in the JSP
13 page. So you can change them separately. To provide the chart with the
14 correct data you must provide an object which implements the interface<tt>de.laures.cewolf.DatasetProducer</tt>.
15 This object is asked to produce data every time a new chart must be
16 rendered. Below you can see an example implementation of a
17 DatasetProducer which could be used to provide data needed for our
18 example scenario. </p>
20 <pre>package de.laures.cewolf.example;
22 import java.io.Serializable;
23 import java.util.Date;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.jfree.data.category.CategoryDataset;
29 import org.jfree.data.category.DefaultCategoryDataset;
31 import de.laures.cewolf.DatasetProduceException;
32 import de.laures.cewolf.DatasetProducer;
33 import de.laures.cewolf.links.CategoryItemLinkGenerator;
34 import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
37 * An example data producer.
38 * @author Guido Laures
40 public class PageViewCountData implements DatasetProducer, CategoryToolTipGenerator, CategoryItemLinkGenerator, Serializable {
42 private static final Log log = LogFactory.getLog(PageViewCountData.class);
44 // These values would normally not be hard coded but produced by
45 // some kind of data source like a database or a file
46 private final String[] categories = {"mon", "tue", "wen", "thu", "fri", "sat", "sun"};
47 private final String[] seriesNames = {"cewolfset.jsp", "tutorial.jsp", "testpage.jsp", "performancetest.jsp"};
50 * Produces some random data.
52 public Object produceDataset(Map params) throws DatasetProduceException {
53 log.debug("producing data.");
54 DefaultCategoryDataset dataset = new DefaultCategoryDataset(){
56 * @see java.lang.Object#finalize()
58 protected void finalize() throws Throwable {
60 log.debug(this +" finalized.");
63 for (int series = 0; series < seriesNames.length; series ++) {
64 int lastY = (int)(Math.random() * 1000 + 1000);
65 for (int i = 0; i < categories.length; i++) {
66 final int y = lastY + (int)(Math.random() * 200 - 100);
68 dataset.addValue(y, seriesNames[series], categories[i]);
75 * This producer's data is invalidated after 5 seconds. By this method the
76 * producer can influence Cewolf's caching behaviour the way it wants to.
78 public boolean hasExpired(Map params, Date since) {
79 log.debug(getClass().getName() + "hasExpired()");
80 return (System.currentTimeMillis() - since.getTime()) > 5000;
84 * Returns a unique ID for this DatasetProducer
86 public String getProducerId() {
87 return "PageViewCountData DatasetProducer";
91 * Returns a link target for a special data item.
93 public String generateLink(Object data, int series, Object category) {
94 return seriesNames[series];
98 * @see java.lang.Object#finalize()
100 protected void finalize() throws Throwable {
102 log.debug(this + " finalized.");
106 * @see org.jfree.chart.tooltips.CategoryToolTipGenerator#generateToolTip(CategoryDataset, int, int)
108 public String generateToolTip(CategoryDataset arg0, int series, int arg2) {
109 return seriesNames[series];
114 <p> As you can see this datasetproducer is not very useful. Normally
115 this class would try to access a datasource (e.g. a database) to access
116 the needed information. But to serve as an example it should do. </p>
117 <p> A DatasetProducer needs to implement three methods. The most
118 important one is the <tt>produceDataset()</tt> method which actually
119 produces the data which should be used to render a chart. This method
120 takes a parameter map which is filled by some special tags of the JSP
121 which will be explained later on. </p>
122 <p> The <tt>hasExpired()</tt> method is called by the Cewolf framework
123 if there already exits a data object produced by this producer in
124 Cewolf's data cache. When returning <tt>true</tt> the producer
125 signalizes that the data formerly used has expired. </p>
126 <p> By providing an unique ID via the <tt>getProducerId()</tt> method
127 the Cewolf framework identifies a producer type. Two producer instances
128 with the same ID are supposed to produce the same data. </p>
129 <p> Compile the class and put it in the correct folder structure under
130 your web application's <tt>/WEB-INF/classes</tt> directory to make it
131 available for your application. </p>
132 <p> <a href="step3.html">Step 3: Install the Cewolf Servlet in your Web
133 Application>></a> </p>
134 <a height="30" target="new" href="http://sourceforge.net"><img
135 alt="SourceForge Logo" border="0" height="30"
136 src="http://sourceforge.net/sflogo.php?group_id=57282&type=5"></a>