Creating a ImageMap from An Image

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
TheHaas
Posts: 21
Joined: Sat Feb 07, 2004 9:14 pm
Contact:

Creating a ImageMap from An Image

Post by TheHaas » Tue Apr 13, 2004 5:45 pm

I have a servlet that creates the chart image directly to the output stream. I want this to be an image map, so I need to render the HTML. I have the ToolTipGenerator and UrlGenerator set in the chart, but the only example I can find of making the image map consists of this:

Code: Select all

	ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		
		return ChartUtilities.getImageMap("chart",info);
Which doesn't give me the desired results:

Code: Select all

<MAP NAME="chart">
</MAP>
I know I need to somehow get the generators into the getImageMap, but if I do this:

Code: Select all

return ChartUtilities.getImageMap("chart", info,
			  			(ToolTipTagFragmentGenerator) plot.getToolTipGenerator(), 
									 (URLTagFragmentGenerator) plot.getURLGenerator());
I get a ClassCastException.

Anyone have any ideas?

tfritsch
Posts: 2
Joined: Tue Apr 13, 2004 2:39 pm

Use a bean to generate Chart and ImageMap?

Post by tfritsch » Tue Apr 13, 2004 6:35 pm

I'm trying to do something similar - and also having problems...

Is it possible to have a bean (stored in the Request object) that creates the chart one time and stores it, and then provides methods to retrieve the Chart or Image Map information? This bean could be referenced from within a JSP to get the chart, and then a servlet could be used to actually stream the chart image. Something like:

Java Bean:

Code: Select all

public class ChartBean
{
   private JFreeChart myChart = null;

   public JFreeChart getMyChart()
   {
      if (myChart == null)
      {
         createMyChart();
      } // end if
		
      return myChart ;
   } 
 	
   public void createMyChart()
   {
      // Do some stuff to build the chart...
      
      // Create tooltip and URL generators
      StandardXYItemLabelGenerator ttg = new StandardXYItemLabelGenerator(blah blah);
      TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(blah blah blah blah);

      //  Create the axes
      ValueAxis timeAxis = new DateAxis(timeAxisLabel);
      NumberAxis valueAxis = new NumberAxis(valueAxisLabel);
      valueAxis.setAutoRangeIncludesZero(false);  // override default

      // Create the renderer
      StandardXYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.LINES, ttg, urlg);
      renderer.setShapesFilled(true);

      // Create the plot
      XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer);

      // Create the chart	
      myChart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
	
   } // end createNumberOfUsersChartTitle()

   public String getMyChartImageMap()
   {
      // Not sure what to do here...
      
      String map = "";
      String name = "myChart";
				
      // Create the chart rendering Info
      ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		
      // Create the image map
      map = ChartUtilities.getImageMap(name, info);

      return map;

   }

} 
JSP:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ page 
	language="java"
	contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"
%>

<jsp:useBean 
	id="chartBean" 
	class="com.hewitt.ispsoft.chart.TestChartBean"
	scope="request"/>
	
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>TestPage.jsp</TITLE>
</HEAD>
<BODY>
<P>My Chart</P>

<img name="chartNumberOfUsers" src="http://localhost:9080/JFree/TestServlet"/> 

<!-- Begin Image Map Info -->
<%= chartBean.getMyChartImageMap() %>
<!-- End Image Map Info -->
</BODY>
</HTML>
Servlet:

Code: Select all

public class TestServlet extends HttpServlet implements Servlet 
{
   public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException 
   {
				
      // Create the Chart Bean
      ChartBean chartBean = new ChartBean();
		
      // Get the chart object
      JFreeChart jfc = chartBean.getMyChart();
		
      // Add the bean to the request object
      request.setAttribute("chartBean", chartBean);
		
      // set the reponse content type 
      response.setContentType("text/png"); 

      // send the response 
      ChartUtilities.writeChartAsPNG( response.getOutputStream(), jfc,  640, 480); 

      // close the output stream 
      response.getOutputStream().close(); 

   } // end doPost()

   public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException 
   {
      doPost(req, resp);
   } // end doGet()

}
It seems like there should be a way to do this, but I can't figure out how the Image Map information gets generated by the ChartUtilities class - when you call getImageMap() or writeImageMap(). You pass in a ChartRenderingInfo object, but its usually created using an empty default constructor:

Code: Select all

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
So, I don't see how it "knows" anything about the specific chart in order to generate the ToolTips...

Does the ChartRenderingInfo somehow get magically populated with some information when the chart gets rendered when you call the method writeChartAsPNG()?

I also tried using a method similar to the examples by essentially generating the chart twice (and saving it to a temp file), but I'd like to avoid that if possible... It seems like there should be a better way...

Am I missing something?

Thanks in advance...

Ted

TheHaas
Posts: 21
Joined: Sat Feb 07, 2004 9:14 pm
Contact:

Post by TheHaas » Tue Apr 13, 2004 7:50 pm

Does the ChartRenderingInfo somehow get magically populated with some information when the chart gets rendered when you call the method writeChartAsPNG()?
I've tried looking at the code, but was no use.

The secret is using the ToolTipTagFragmentGenerator and UrlTagFragmentGenerator options in getImageMap, but there are no examples or way to cast plot.getToolTips() and plot.BaseItemURLGenerator() to those, that I can find.

TheHaas
Posts: 21
Joined: Sat Feb 07, 2004 9:14 pm
Contact:

Post by TheHaas » Wed Apr 14, 2004 8:24 pm

Okay, after a day of working on it, I think I have something . . .

When you do this:

Code: Select all

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
You create a blank ChartRenderingInfo object. When you do this:

Code: Select all

ChartUtilities.writeChartAsPNG(out,chart,600,400,info);
The "info" object gets populated. ("Use the Source, Luke")

Once upon a time, Richard Akinson said to render the chart twice -- once for the image and once for the map. That's what you have to do.

My class has a HashMap called "chartTable" that keeps track of the JFreeChart object. The "getImageMap" method in my servlet looks like this.

Code: Select all

public static String getImageMap(String cookieVal) {

		String map = "";
		JFreeChart chart = (JFreeChart) chartTable.get(cookieVal);
		
		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		
		try {
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				ChartUtilities.writeChartAsPNG(out,chart,600,400,info);
				map = ChartUtilities.getImageMap("chart",info);
				out.close();
		} catch (IOException e) {
			
			logger.error("Failed creating map tag. Error was " + e);
		}		
		
		return map;

	}
You could keep your ChartRenderingInfo object persistant with a HashTable like I did with the JFreeChart object, but I didn't seem to have luck with that.

I hope this helps . . .

bella
Posts: 2
Joined: Thu Jan 06, 2011 2:22 pm
antibot: No, of course not.

Re: Creating a ImageMap from An Image

Post by bella » Thu Jan 06, 2011 2:44 pm

Hi,

I have created one gannt chart using jfreeChart. I tried to add url and tooltip in the chart, but it is not coming properly.
my code snippets is following

def reservations = filterReservation()
NumberFormat num = new DecimalFormat()

reservations.eachWithIndex { reservationObj, index ->

println "reservationObj.id" +reservationObj.id
renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
def urlGen = new StandardCategoryURLGenerator("../reservation/show?id=" +reservationObj.id);
def toolTipGen = new StandardCategoryToolTipGenerator (reservationObj.name,num)

renderer.setSeriesItemURLGenerator(index, urlGen)
renderer.setSeriesToolTipGenerator(index, toolTipGen);
}

My chart containing 9 bars and each bar corresponding to only one reservation , but the problem is some of the bar's reservation are showing same. For tooltip also getting the same result.

please do reply me

Thanks in adavnce

Locked