开发者

How to implement custom JSF component for drawing chart?

开发者 https://www.devze.com 2022-12-28 04:25 出处:网络
I want to create a component which can be used like: <mc:chart data=\"#{bean.data}\" width=\"200\" height=\"300\" />

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 s开发者_开发知识库ome 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..


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消