KavaChart on the Server

Here's our page, with a dynamically generated chart. If you refresh the page, you'll see the bars change with the data. If you place your cursor over a bar, you'll see a tooltip label provided by the tag defaults and refined by the style set. If you view the source code to the page, you'll see that a servlet is doing the work of storing the image data in memory and streaming it to the browser.

So how does this all work? First, there's a chart definition, that looks like this:

	<chart:streamed style="styles/paramchart.properties" dataProviderID="foo" />
This says that we're going to use a chart style defined by "paramchart.properties". That file is here: properties file. It simply defines some axis labels, a width and height, and some non-default strings for the tooltip labels.

It also says we're going to use a "DataProvider" named "foo".

A DataProvider is a little piece of code that defines the numbers and labels for the bars. It's abstracted from the chart definition for good reasons: it's portable from chart to chart, it can be put into a variety of persistence models (application, session, page, etc.), and it can be tested outside an HTTP environment. This particular DataProvider just generates random numbers.

You can see it here: RandomNumberDataProvider.

Pretty simple.

Value: 9% Value: 98% Value: 71% Value: 31% Value: 72%

Our "foo" DataProvider could be installed in a lot of ways, but this page just loads it as a JavaBean, using JSP's "useBean" syntax. Another technique would be to set it like this:

pageContext.setAttribute("foo" new RandomNumberDataProvider());

If you're accustomed to building complex web applications with multiple page generation layers, you'll appreciate that the data management code can be installed at just about any point in the page construction process.