Syntax problem

Rachel
2006-12-22
2013-01-13
  • Rachel
    Rachel
    2006-12-22

    The following code does not work.  I am new to this and I got an error: IncompatibleDataSetException raised: Charts of type line need datasets of type
    org.jfree.data.category.Category.

    Basically what happened is, I got the example to work and now I am trying to change the example which was a line chart to a pie chart.  How can I fix this?  Please let me know of any other syntax errors I made.  If anyone has examples of Bar charts I need that too.

    Rachel (code found below)

    package de.laures.cewolf.example;

    import java.io.Serializable;
    import java.util.Date;
    import java.util.Map;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartFrame;
    import org.jfree.chart.JFreeChart;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.data.general.DefaultPieDataset;

    import de.laures.cewolf.DatasetProduceException;
    import de.laures.cewolf.DatasetProducer;
    import de.laures.cewolf.links.CategoryItemLinkGenerator;
    import de.laures.cewolf.links.*;
    import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
    import de.laures.cewolf.tooltips.PieToolTipGenerator;

    /**
    * An example data producer. @author  Rachel Kozlowski
    */
    public class PageViewCountData1 implements DatasetProducer, PieSectionLinkGenerator, Serializable {

        private static final long serialVersionUID = 1L;
        private static final Log log = LogFactory.getLog(PageViewCountData.class);

        // 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 =    {"Category 1", "Category 2", "Category 3"};
       //private final String[] seriesNames =    {"Rachel", "Ken", "Lynne", "Michael"};

        /**
         *  Produces some random data.
         */
        public Object produceDataset(Map params) throws DatasetProduceException {
            log.debug("producing data.");
            DefaultPieDataset dataset = new DefaultPieDataset();
            dataset.setValue("Category 1",43.2);
            dataset.setValue("Category 2",27.9);
            dataset.setValue("Category 3",79.5);
            return dataset;
        }

        /**
         * This producer's data is invalidated after 5 seconds. By this method the
         * producer can influence Cewolf's caching behaviour the way it wants to.
         */
        public boolean hasExpired(Map params, Date since) {
            log.debug(getClass().getName() + "hasExpired()");
            return (System.currentTimeMillis() - since.getTime())  > 5000;
        }

        /**
         * Returns a unique ID for this DatasetProducer
         */
        public String getProducerId() {
            return "PageViewCountData1 DatasetProducer";
        }

        /**
         * Returns a link target for a special data item.
         */
        /*public String generateLink(Object data, int series, Object category) {
            return seriesNames[series];
        }*/
        public String generateLink(Object data, Object category){
            return categories[1];
        }
        //PieSectionLinkGenerator

        /**
         * @see java.lang.Object#finalize()
         */
        protected void finalize() throws Throwable {
            super.finalize();
            log.debug(this + " finalized.");
        }

        /*
          @see org.jfree.chart.tooltips.CategoryToolTipGenerator#generateToolTip(CategoryDataset, int, int)

        public String generateToolTip(CategoryDataset arg0, int series, int arg2) {
            return seriesNames[series];
        }*/

        //String generateToolTip(PieDataset data, Comparable key, int pieIndex);

    }

     
    • Rachel
      Rachel
      2006-12-22

      I got part of the answer:  I had to change the id in the cewolf:chart tag to pie it was set at line.  This is what it should be:
      <cewolf:chart     id="pie"     title="Employee View Statistics"  type="pie">

      The jsp page should work now.  However, I am still having problems, I am getting a nullPointerException raised.  Remember I am new to this, so where would my log file be found?

      This is how the jsp page should look:
      <%@page contentType="text/html"%>
      <%@taglib uri='/WEB-INF/cewolf.tld' prefix='cewolf' %>
      <HTML>
      <BODY>
      <H1>Employee Statistics</H1>
      <HR>
      <jsp:useBean id="pageViews" class="de.laures.cewolf.example.PageViewCountData1"/>
      <cewolf:chart     id="pie"     title="Employee View Statistics"  type="pie">   
      <cewolf:data>       
      <cewolf:producer id="pageViews"/>   
      </cewolf:data>
      </cewolf:chart>
      <p>
      <cewolf:img chartid="line" renderer="cewolf" width="400" height="300"/>
      <P>
      </BODY>
      </HTML>

       
    • Rachel
      Rachel
      2006-12-22

      Ok folks, I found the answer.  It was not my java code at all, but it was my jsp code, I needed to change another line. The chartid need to be changed from pie to pieChart and from line to pieChart.  These were the lines affected:
      <cewolf:chart     id="pieChart"     title="Employee View Statistics"  type="pie">   
      <cewolf:img chartid="pieChart" renderer="cewolf" width="400" height="300"/>

      This is a simple example of pie Chart code in CEWOLF:

      This is the jsp code that works:
      <%@page contentType="text/html"%>
      <%@taglib uri='/WEB-INF/cewolf.tld' prefix='cewolf' %>
      <HTML>
      <BODY>
      <H1>Employee Statistics</H1>
      <HR>
      <jsp:useBean id="pageViews" class="de.laures.cewolf.example.PageViewCountData1"/>
      <cewolf:chart     id="pieChart"     title="Employee View Statistics"  type="pie">   
      <cewolf:data>       
      <cewolf:producer id="pageViews"/>   
      </cewolf:data>
      </cewolf:chart>
      <p>
      <cewolf:img chartid="pieChart" renderer="cewolf" width="400" height="300"/>
      <P>
      </BODY>
      </HTML>

      This is the java code that works:
      package de.laures.cewolf.example;

      import java.io.Serializable;
      import java.util.Date;
      import java.util.Map;

      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import org.jfree.chart.ChartFactory;
      import org.jfree.chart.ChartFrame;
      import org.jfree.chart.JFreeChart;
      import org.jfree.data.category.CategoryDataset;
      import org.jfree.data.category.DefaultCategoryDataset;
      import org.jfree.data.general.DefaultPieDataset;

      import de.laures.cewolf.DatasetProduceException;
      import de.laures.cewolf.DatasetProducer;
      import de.laures.cewolf.links.CategoryItemLinkGenerator;
      import de.laures.cewolf.links.*;
      import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
      import de.laures.cewolf.tooltips.PieToolTipGenerator;

      /**
      * An example data producer. @author  Rachel Kozlowski
      */
      public class PageViewCountData1 implements DatasetProducer, PieSectionLinkGenerator, Serializable {

          private static final long serialVersionUID = 1L;
          private static final Log log = LogFactory.getLog(PageViewCountData.class);

          private final String[] categories =    {"Category 1", "Category 2", "Category 3"};

          public Object produceDataset(Map params) throws DatasetProduceException {
              log.debug("producing data.");
              DefaultPieDataset dataset = new DefaultPieDataset();
              dataset.setValue("Category 1",43.2);
              dataset.setValue("Category 2",27.9);
              dataset.setValue("Category 3",79.5);
              return dataset;
          }

          /**
           * This producer's data is invalidated after 5 seconds. By this method the
           * producer can influence Cewolf's caching behaviour the way it wants to.
           */
          public boolean hasExpired(Map params, Date since) {
              log.debug(getClass().getName() + "hasExpired()");
              return (System.currentTimeMillis() - since.getTime())  > 5000;
          }

          /**
           * Returns a unique ID for this DatasetProducer
           */
          public String getProducerId() {
              return "PageViewCountData1 DatasetProducer";
          }

          /**
           * Returns a link target for a special data item.
           */

          public String generateLink(Object data, Object category){
              return categories[1];
          }

          /**
           * @see java.lang.Object#finalize()
           */
          protected void finalize() throws Throwable {
              super.finalize();
              log.debug(this + " finalized.");
          }

      }