When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, February 21, 2001

Charting with Office Web Components (OWC), Part 3

By Bret Hern

  • Read Part 1
  • Read Part 2

  • 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!

    - continued -

    Cleaning Up After Yourself
    Using the absolute and relative paths we defined at the top, our ExportChartToGIF function executes the chartspace ExportPicture method to export the generated chart to an image format:

    Function ExportChartToGIF(objCSpace, strAbsFilePath, strRelFilePath)
       'Get a random filename
       Dim strFileName
       strFileName = Timer & Rnd & ".gif"
       'Save the image as a GIF
       objCSpace.ExportPicture strAbsFilePath & "\" & strFileName, _
                               "gif", 600, 350
       'Return the path to the GIF image of the graph
       ExportChartToGIF = strRelFilePath & "/" & strFileName
    End Function

    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 pixels).

    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:

    Sub CleanUpGIF(GIFpath)
       Dim objFS
       Dim objFolder
       Dim gif
       set objFS = Server.CreateObject("Scripting.FileSystemObject")
       set objFolder = objFS.GetFolder(GIFpath)
       'Loop through each file in the GIFpath folder
       for each gif in objFolder.Files
           'Delete GIF files older than 10 minutes
           if instr(gif.Name, ".gif") > 0 and _
             DateDiff("n", gif.DateLastModified, now) > 10 then
               objFS.DeleteFile GIFpath & "\" & gif.Name, True
           end if
       set objFolder = nothing
       set objFS = nothing
    End Sub

    The 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.

    Happy Programming!

  • By Bret Hern

    Attachments and Related Articles:

  • Download the source code in text format
  • Visit the Graphing with ASP Article Index
  • Read Using Office Web Components
  • Read Making Charts in ASP (using the Office Web Components (OWCs))

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article