Cannot generate an image map. What gives???

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
hardwickj
Posts: 7
Joined: Thu Jan 11, 2007 5:27 pm

Cannot generate an image map. What gives???

Post by hardwickj » Tue Jan 16, 2007 4:35 pm

I have been working on this servlet the past day and a half, and for whatever reason, I cannot get this to generate an image map. When I view the html source of its output, the <map id=etc..></map> tags are there, but there is no further information within them. Any ideas on what I am doing wrong??

I broke my code up into three sections, the main method run by the servlet, a method to generate the dataset from results from a database, and a method generate the chart from the dataset. The final image is being generated OK, just not the image map.

EDIT: I forgot to say, my goal is to create a horizontal bar chart, with a single bar for every "track" I have. There are no categories, and I use DefaultKeyedValues because that is the only one I could find that does a sortByValues method. I want a person to be able to click on any of the bars, and to be re-directed to a page with additional details on that "track". The image is being generated fine, just no image map to handle the redirecting.

Code: Select all

public class ThreatAsmnt extends HttpServlet {
    
    private int spacer;
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        // Create a chart
        CategoryDataset dataset = createDatasets();
        JFreeChart chart = createChart(dataset);        
        ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        
        // Stores imagemap html for JSP, and stores chart information for image generation
        String imageMap = ImageMapUtilities.getImageMap("imageMap", info);
        HttpSession session = request.getSession();
        session.setAttribute("chart", chart);
        session.setAttribute("chartInfo", info);
        session.setAttribute("chartSize", new Integer(120 + spacer));
        request.setAttribute("imageMap", imageMap);
        RequestDispatcher dispatcher = request.getRequestDispatcher("threatAssess.jsp");
        dispatcher.forward(request, response);
    }
    
    /** Receives tracks from the TrackInfoBean and generically process them
     * to assign some sort of risk analysis for demonstration.
     * @returns CategoryDataset
     */
    private CategoryDataset createDatasets() {
        TrackInfoLocal trackInfoBean = new TrackInfoBean();
        StoredResultSet r = trackInfoBean.getTracksAllDetails();
        Random generator = new Random();
        Long key;
        String ident = "";
        DefaultKeyedValues data = new DefaultKeyedValues();
        spacer = 0;
        while(r.next()) {
            key = r.getLong("T_IMONUMBER");
            if(!r.wasNull()) {
                spacer += 13;
                ident = r.getString("T_IDENTITY");
                if(ident.equals("UNKNOWN"))      {
                    data.addValue(key.toString(), generator.nextInt(349) + 500);  
                }
                else if(ident.equals("NEUTRAL")) {
                    data.addValue(key.toString(), generator.nextInt(499));
                }
                else if(ident.equals("FRIEND"))  {
                    data.addValue(key.toString(), generator.nextInt(499));
                }
                else if(ident.equals("HOSTILE")) {
                    data.addValue(key.toString(), generator.nextInt(150) + 850);
                }
                else if(ident.equals("PENDING")) {
                    data.addValue(key.toString(), generator.nextInt(349) + 500);
                }
                else if(ident.equals("SUSPECT")) {
                    data.addValue(key.toString(), generator.nextInt(150) + 850);
                }
                else {
                    data.addValue(key.toString(), generator.nextInt(349) + 500);
                }
            }
        }
        data.sortByValues(SortOrder.DESCENDING);
        CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Tracks", data);
        return dataset;
    }
    
    /** Creates a horizontal bar chart from a dataset.
     * @params dataset CategoryDataset to generate chart from
     * @returns JFreeChart
     */
    private JFreeChart createChart(CategoryDataset dataset) {
        // create the chart...
        JFreeChart chart = ChartFactory.createBarChart(
            "Risk Assessment",          // chart title
            "IMO#",                     // domain axis label
            "Risk",                     // range axis label
            dataset,                    // data
            PlotOrientation.HORIZONTAL,
            false,                      // legend?
            true,                       // tooltips?
            false                        // url's?
        );

        CustomBarRenderer renderer = new CustomBarRenderer();
        renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
        renderer.setItemURLGenerator(new StandardCategoryURLGenerator("TrackDetails"));
        
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setRenderer(renderer);
        
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setLowerMargin(0.01);
        domainAxis.setUpperMargin(0.01);

        return chart;    
    }
Any hints/tips are welcome!!!

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jan 16, 2007 4:49 pm

You are passing an empty 'info' object to the getImageMap() method. You need to first pass the new 'info' instance to JFreeChart.draw() (this populates the 'info' as the chart is being drawn), then call getImageMap() with the populated 'info' instance.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

hardwickj
Posts: 7
Joined: Thu Jan 11, 2007 5:27 pm

Post by hardwickj » Tue Jan 16, 2007 4:58 pm

K, seems simple enough. I was looking at all your sample ImageMapDemo code, and never saw anything do this.

Or let me guess, something such as ChartUtilities.writeChartAsPNG() hit's up the draw method?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jan 16, 2007 5:05 pm

Indirectly it happens, because:

Code: Select all

ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
...passes 'info' to the JFreeChart.draw() method.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

hardwickj
Posts: 7
Joined: Thu Jan 11, 2007 5:27 pm

Post by hardwickj » Tue Jan 16, 2007 5:28 pm

Excellent. Well this makes sense. I'll have to re-arrange the flow of my servlets a bit but this should be an easy fix.

Thanks for pointing out the obvious to me David :)

hardwickj
Posts: 7
Joined: Thu Jan 11, 2007 5:27 pm

Post by hardwickj » Tue Jan 16, 2007 5:57 pm

I changed the code somewhat like the following:

Code: Select all

response.setContentType("image/png");
        OutputStream out = response.getOutputStream();
        ChartUtilities.writeChartAsPNG(out,chart,600,spacer);
        out.close();
        
        // Stores imagemap html for JSP
        String imageMap = ChartUtilities.getImageMap("imageMap", info);
and the image map HTML is still coming up empty. I get the <map></map> tags, but no further data in them.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jan 16, 2007 6:01 pm

You need the writeChartAsPNG method that has a ChartRenderingInfo argument, so that the 'info' object gets populated.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

hardwickj
Posts: 7
Joined: Thu Jan 11, 2007 5:27 pm

Post by hardwickj » Tue Jan 16, 2007 6:03 pm

Geesh.....didn't we just get done talking about that?

Your doing pretty well at making me feel like a putz...

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

Post by angel » Wed Jan 17, 2007 12:41 pm

Something like that

Code: Select all

ChartRenderingInfo cri = new ChartRenderingInfo();
String filename = ServletUtilities.saveChartAsPNG(chart, 790, 430, cri, null);
String imageMap = ChartUtilities.getImageMap("imagemap", cri);

jfreeuser2006
Posts: 59
Joined: Mon Nov 20, 2006 1:00 pm

Post by jfreeuser2006 » Mon Jan 22, 2007 6:15 am

I have this in my servlet

Code: Select all

     ChartRenderingInfo info = new ChartRenderingInfo(new    StandardEntityCollection());
     ChartUtilities.writeChartAsPNG(out, chart, width, height, info); 
     String imageMap = ImageMapUtilities.getImageMap("imageMap", info);
            
     session.setAttribute("chart", chart);
     session.setAttribute("chartInfo", info);
     session.setAttribute("imageMap", imageMap); 
and this in my jsp

Code: Select all

<%=session.getAttribute("imageMap")%>
                <img src="/SPC/spcchart?width=700&height=500" usemap="#map">
what shows is this

Code: Select all

    <map id="imagemap" name="imagemap"></map>
    <img src="/SPC/spcchart?width=700&height=600" usemap="#map">

i'm clueless to what i'm doing wrong.
any help would be great.
thanks!

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

Post by angel » Mon Jan 22, 2007 4:47 pm

You use a differnent name for the map and the name of the map that the image is using:

<map id="imagemap" name="imagemap"></map>
<img src="/SPC/spcchart?width=700&height=600" usemap="#map">

Locked