views:

155

answers:

1

I want to create a component which can be used like:

<mc:chart data="#{bean.data}" width="200" height="300" />

where #{bean.data} returns a collection of some objects or chart model object or something else what can be represented as a chart (to put it simple, let's assume it returns a collection of integers).

I want this component to generate html like this:

<img src="someimg123.png" width="200" height="300"/>

The problem is that I have some method which can receive data and return image, like:

public RenderedImage getChartImage (Collection<Integer> data) { ... }

and I also have a component for drawing dynamic image:

<o:dynamicImage width="200" height="300" data="#{bean.readyChartImage}/> 

This component generates html just as I need but it's parameter is array of bytes or RenderedImage i.e. it needs method in bean like this:

public RenderedImage getReadyChartImage () { ... }

So, one approach is to use propertyChangedListener on submit to set data (Collection<Integer>) for drawing chart and then use <o:dynamicImage /> component. But I'd like to create my own component which receives data and draws chart.

I'm using facelets but it's not so important indeed. Any ideas how to create the desired component?

P.S. One solution I was thinking about is not to use <o:dynamicImage/> and use some servlet to stream image. But I don't know how to implement that correctly and how to tie jsf component with servlet and how to save already built chart images (generating new same image for each request can cause performance problems imho) and so on..

+1  A: 

P.S. One solution I was thinking about is not to use and use some servlet to stream image. But I don't know how to implement that correctly and how to tie jsf component with servlet and how to save already built chart images (generating new same image for each request can cause performance problems imho) and so on..

Just give it an URL as attribute so that it ends up as <img src="url"> and let that URL point to the servlet. Let the servlet generate/return the image based on the information provided by request parameters or pathinfo in the URL.

As to the caching, just store it somewhere in the disk file system or a DB by some unique identifier and let the servlet on every request check if it is there and handle accordingly.

For this all the standard <h:graphicImage> would be already sufficient. Just make the URL dynamic.

BalusC
I was able to make a custom component just as I want (I lacked some knowledge about jsf custom components but I gained it today). But your recommendation is still very helpful, I'm going to use it tomorrow in to accomplish another task.
Roman
You're welcome.
BalusC