Charting with Office Web Components (OWC), Part 3By Bret Hern
In Part 2 we looked, in great detail, at a sample ASP application that displayed database information in a nice-looking line graph. This graph was saved as a GIF image on the Web server - over time these can accumulate and clutter up your Web server's hard drive space. In this final part of this article we'll examine how to clean up unused graphs!
Cleaning Up After Yourself
Using the absolute and relative paths we defined at the top, our
function executes the chartspace
ExportPicture method to export the generated
chart to an image format:
The lines immediately prior to the
ExportPicture method being called are to
define a safely unique filename as the target for the exported image file. There are
alternative methods of defining temporary files for this sort of activity, but the
combination of the timer function and a random number will be sufficient for our needs --
and less resource intensive than using the temp file generation method of the FileSystemObject
object, for example (see this FAQ).
When coupled with the cleanup process described below, this export
method is safe and efficient. Also note the mild tap dance with relative paths vs. absolute
paths -- the
ExportPicture method requires an absolute path, while for
safety's sake, a relative path is used to refer to the image file defined.
The parameters for the
ExportPicture method are reasonably obvious -- the
final two parameters are the horizontal and vertical size of the image to be created (in
While a simple process, exporting the chart to a graphic file does present a lingering challenge: what to do with the file after it is displayed? The graphic file cannot be deleted until after the script is complete and the page rendered. Therefore, without some form of cleanup process, the number of GIF files will grow unchecked as users visit the site. Several examples of OWC solutions in the available literature suggest either streaming the image file as binary data to the browser, which does allow for immediate deletion of the file within the script, or the use of sessions to permit files to be deleted at session end. These methods are certainly reasonable and acceptable, but I prefer an alternative approach that, when paired with the export function described above, yields excellent results while retaining developmental simplicity:
CleanUpGIF function simply reviews the folder passed to it for GIF files
of a certain age -- I've set it at 10 minutes -- and deletes them if older than that
threshold. This way, files are continuously being safely cleaned up after they've been
rendered and viewed. This method also works perfectly well in any sort of load balanced
situation, something which can give session-based methods a bit of heartburn.
The primary benefit of this approach to image file management is the directness of the approach. It relies only on the Office Web Components themselves, and can be deployed without sessions or additional componentry. By simplifying the delivery approach, we lower the barrier to entry for this valuable toolset. It won't make up for the sketchy documentation, but it's a start.
Attachments and Related Articles: