Trying to understand

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
gchrt
Posts: 19
Joined: Sat Jun 19, 2004 12:56 am

Trying to understand

Post by gchrt » Mon Jun 28, 2004 7:46 pm

Here is the servlet:

Code: Select all

package com.dunlopconsult.clayforeman.servlets;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.awt.Font;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.CategoryItemRenderer;
import org.jfree.chart.renderer.StatisticalBarRenderer;
import org.jfree.data.DefaultCategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.data.statistics.StatisticalCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class CF_ChartGeneratorServlet extends HttpServlet {
    
    /**
     * Default constructor.
     */
    public CF_ChartGeneratorServlet() {
        // nothing required
    }    

    /**
     * Process a GET request.
     *
     * @param request  the request.
     * @param response  the response.
     *
     * @throws ServletException if there is a servlet related problem.
     * @throws IOException if there is an I/O problem.
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        response.setContentType("image/png");
        OutputStream out = response.getOutputStream();
        StatisticalCategoryDataset dataset = createDataset();

        CategoryAxis xAxis = new CategoryAxis("Type");
        xAxis.setLowerMargin(0.01d); // percentage of space before first bar
        xAxis.setUpperMargin(0.01d); // percentage of space after last bar
        xAxis.setCategoryMargin(0.05d); // percentage of space between categories
        ValueAxis yAxis = new NumberAxis("Value");

        // define the plot
        CategoryItemRenderer renderer = new StatisticalBarRenderer();
        CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);

        JFreeChart chart = new JFreeChart("Statistical Bar Chart Demo",
                                          new Font("Helvetica", Font.BOLD, 14),
                                          plot,
                                          true);
        ChartUtilities.writeChartAsPNG(out, chart, 600, 500);
        out.close();
    }

    /**
     * Creates a sample dataset.
     *
     * @return The dataset.
     */
    private StatisticalCategoryDataset createDataset() {

        final DefaultStatisticalCategoryDataset result = new DefaultStatisticalCategoryDataset();

        result.add(32.5, 17.9, "Series 1", "Type 1");
        result.add(27.8, 11.4, "Series 1", "Type 2");
        result.add(29.3, 14.4, "Series 1", "Type 3");
        result.add(37.9, 10.3, "Series 1", "Type 4");

        result.add(22.9,  7.9, "Series 2", "Type 1");
        result.add(21.8, 18.4, "Series 2", "Type 2");
        result.add(19.3, 12.4, "Series 2", "Type 3");
        result.add(30.3, 20.7, "Series 2", "Type 4");

        result.add(12.5, 10.9, "Series 3", "Type 1");
        result.add(24.8,  7.4, "Series 3", "Type 2");
        result.add(19.3, 13.4, "Series 3", "Type 3");
        result.add(17.1, 10.6, "Series 3", "Type 4");

        return result;

    }
  
Here is the jsp call:

Code: Select all

img src="http://localhost:8081/com/dunlopconsult/clayforeman/servlets/CF_ChartGeneratorServlet" /
I am not getting a chart image.

Any ideas?

richard_atkinson
Posts: 115
Joined: Fri Mar 14, 2003 3:13 pm
Location: London, England
Contact:

Servlet Problem

Post by richard_atkinson » Mon Jun 28, 2004 8:20 pm

Do you have a servlet definition and mapping setup in your web.xml file?

Code: Select all

    <servlet>
        <servlet-name>DisplayChart</servlet-name>
        <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DisplayChart</servlet-name>
        <url-pattern>/servlet/DisplayChart</url-pattern>
     </servlet-mapping>
Then you can reference your servlet using

Code: Select all

http://localhost:8081/servlet/DisplayChart
Check out the sample WAR file to see a complete example.

Regards,
Richard...

gchrt
Posts: 19
Joined: Sat Jun 19, 2004 12:56 am

Post by gchrt » Mon Jun 28, 2004 8:45 pm

Richard,

Thank you for replying.

Please excuse my inexperience in both jfreeChart and java.

In reference to your war file, when clicking on it I get a document of code.
Is this saved then used somehow?

I have my web.xml file mapped for DisplayChart.
I tried referencing the servlet as suggested but still no image.

I am using NetBeans IDE and with a breakpoint in the chart building servlet the process never gets further than the default constructor.

again thank you for taking the time to help me solve my dilemma. It has been a frustrating few days.

richard_atkinson
Posts: 115
Joined: Fri Mar 14, 2003 3:13 pm
Location: London, England
Contact:

WAR file

Post by richard_atkinson » Mon Jun 28, 2004 9:02 pm

Try right clicking on the WAR file link and clicking "Save target as" or whatever looks a likely option in your browser. In a default Tomcat installation you can drop it in the webapps directory and then start Tomcat and it will deploy automatically to /jfreechart-sample.

Check you servlet engine log files for additional errors that might be coming from your servlet.

Regards,
Richard...

joelwyland
Posts: 15
Joined: Wed Jun 02, 2004 10:26 pm

Post by joelwyland » Mon Jun 28, 2004 9:33 pm

gchrt wrote: I have my web.xml file mapped for DisplayChart.
I tried referencing the servlet as suggested but still no image.
Can you post the section of your web.xml that configures the DisplayChart servlet?

gchrt
Posts: 19
Joined: Sat Jun 19, 2004 12:56 am

Post by gchrt » Mon Jun 28, 2004 9:46 pm

Here are the two sections in my web.xml file referring to DisplayChart:

Code: Select all

  
  <servlet>
  <servlet-name>DisplayChart</servlet-name>
  <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>DisplayChart</servlet-name>
  <url-pattern>/servlet/org/jfree/chart/servlet/DisplayChart</url-pattern>
  </servlet-mapping>

joelwyland
Posts: 15
Joined: Wed Jun 02, 2004 10:26 pm

Post by joelwyland » Mon Jun 28, 2004 10:36 pm

gchrt wrote:Here are the two sections in my web.xml file referring to DisplayChart:

Code: Select all

  
  <servlet>
  <servlet-name>DisplayChart</servlet-name>
  <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>DisplayChart</servlet-name>
  <url-pattern>/servlet/org/jfree/chart/servlet/DisplayChart</url-pattern>
  </servlet-mapping>
In your first post, you said the JSP was trying to load an image from:
img src="http://localhost:8081/com/dunlopconsult ... torServlet" /

Which means that you need to have a servlet-mapping that references /com/dunlopconsult/clayforeman/servlets/CF_ChartGeneratorServlet

Like so:

Code: Select all

  <servlet>
  <servlet-name>DisplayChart</servlet-name>
  <servlet-class>com.dunlopconsult.clayforeman.servlets.CF_ChartGeneratorServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>DisplayChart</servlet-name>
  <url-pattern>/com/dunlopconsult/clayforeman/servlets/CF_ChartGeneratorServlet</url-pattern>
  </servlet-mapping>
Kenny[/code]

gchrt
Posts: 19
Joined: Sat Jun 19, 2004 12:56 am

Post by gchrt » Mon Jun 28, 2004 10:52 pm

Thank you for taking the time to assist me in this endeavor.

I followed your advise and remapped my web.xml but to no avail.

Again the servlet is not being run when called upon.

My initial call to the chart building servlet is through another servlet I am using to parse user entered data. Could this be the culprit:

Code: Select all

CF_ChartGeneratorServlet chart = new CF_ChartGeneratorServlet();
Again I really appreciate any insight I can get.

gchrt
Posts: 19
Joined: Sat Jun 19, 2004 12:56 am

Post by gchrt » Tue Jun 29, 2004 12:38 am

I routed the call to the chart builder servlet through my main controller forwarding the req/resp.
Now I get a run down through the servlet to:

Code: Select all

JFreeChart chart = new JFreeChart("", new Font("Helvetica" Font.BOLD, 14), plot, true);
Where I now get a HTTP Status 500 - Internal Server Error:

Code: Select all

java.lang.ClassCastException
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:246)
Something in JFreeChart that I have sent it would cause this?

tonghz
Posts: 3
Joined: Sat Jun 10, 2006 6:58 am

Re: Trying to understand

Post by tonghz » Sat Jun 10, 2006 7:42 am

Can anyone tell me what use of the bool urls param? thx in advance.

final JFreeChart chart = ChartFactory.createBarChart3D(
"3D Bar Chart Demo", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Re: Trying to understand

Post by angel » Mon Jun 12, 2006 7:55 am

tonghz wrote:Can anyone tell me what use of the bool urls param? thx in advance.

final JFreeChart chart = ChartFactory.createBarChart3D(
"3D Bar Chart Demo", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
http://www.jfree.org/jfreechart/javadoc ... BarChart3D

Locked