ChartDirector 6.0 (Ruby Edition)

Using ChartDirector with Database in Web Pages


To display any image in a HTML web page, an <IMG> tag is needed. The browser will first load the HTML web page. When it sees <IMG> tag, it will load the image using the URL in the <IMG> tag. If the image happens to be a chart, the <IMG> tag URL may point to a charting script that uses ChartDirector to generate the chart on the fly.

Thus two actions may be invoked when displaying a ChartDirector chart in a HTML web page - the action that create the HTML web page, and the action referenced in the <IMG> tag URL that delivers the chart image to the browser.

Strategies in Passing Data to ChartDirector

To create a chart in which the data come from a database, there are several options:

  1. Perform the database query in the <IMG> tag action to obtain the data, create the chart image, and deliver the chart to the browser.

  2. Perform the database query in the action for the HTML page, then pass the data to the <IMG> tag action as HTTP query parameters. The <IMG> tag action retrieves the data from the HTTP query parameters, create the chart image, and deliver the chart to the browser.

    This method allows the data from the database query to be used in multiple charts in the HTML page, and in the HTML page itself (eg. displayed as tables).

    One limitation of this method is that most browsers have a limit of around 2048 bytes for query parameters. So it may not be suitable for large amounts of data.

  3. Same as above, but instead of using HTTP query parameters to pass the data, use "session variables". This overcomes the limitations of HTTP query parameters and can be used for large amount of data.

  4. Perform the database query and create the chart image in the action for the HTML page. Save the chart image to a "session variable". The <IMG> tag action retrieves the chart image from the session variable and delivers it to the browser.

    This approach is commonly used in clickable charts, but it can be used for other charts as well.

  5. Same as above, but instead of using "session variables", use temporary files to save the chart.

    To make using temporary files easy, ChartDirector has a special method BaseChart.makeTmpFile that saves charts in temporary files and automatically remove old temporary files.

    Although this method uses temporary files, in practice it is quite efficient. The performance of modern file systems is well-suited for this kind of tasks.

    Note: When using temporary files, make sure the security settings allow the web server anonymous user to write to the temporary directory. (By default anonymous users may not have write access to most directories.)