ChartDirector 5.1 (.NET Edition)

Output Methods for WebChartViewer


According to the HTML standard, HTML cannot "contain" any image. It can only contain the URL of the images, typically in <IMG> tags. Other tags, such as <IFRAME>, <OBJECT> or <EMBED>, or the "background" property of CSS, etc, can also be used. The browser will use the URLs to load the images from separate HTTP connections.

If a chart is created in a Web Form, to deliver the chart image to the browser, the WebChartViewer control uses the following strategy:

  1. Stores the chart image in a temporary location.

  2. Includes an <IMG> tag in the HTML output to represent the chart image.

  3. When the browser processes the HTML, it sees the <IMG> tag, and loads the image using a separate HTTP connection.

  4. The WebChartViewer handles this separate HTTP connection. It retrieves the chart image from the temporary location and returns it to the browser.
Note that steps 1 and 4 occur in two different HTTP connections. So the temporary location must be persistent across HTTP connections.

Using Session Variables as Temporary Storage

By default, WebChartViewer will use session variables to store the chart image. The code will be something like:

[VB]
WebChartViewer1.Image = c.makeWebImage(Chart.PNG)
[C#]
WebChartViewer1.Image = c.makeWebImage(Chart.PNG);

For the above code to work, session variables must not be disabled.

Using Temporary Files as Temporary Storage

WebChartViewer also supports using files as the temporary storage. In this case, no session variable is needed. ChartDirector has a special method BaseChart.makeTmpFile that can create temporary files and automatically remove old temporary files.

When using temporary files, the code will be something like:

[VB]
WebChartViewer1.ImageUrl = "/tmp_chart/" & c.makeTmpFile(Server.MapPath("/tmp_chart"))
[C#]
WebChartViewer1.ImageUrl = "/tmp_chart/" + c.makeTmpFile(Server.MapPath("/tmp_chart"));

In the above code, the web application directory "/tmp_chart" is used for the temporary files. Since BaseChart.makeTmpFile expects a file system path, the Server.MapPath function is needed to map the web application path to a file system path.

BaseChart.makeTmpFile creates the chart image as a temporary file under the given directory, and returns the temporary file name. At the same time, it removes old temporary files from the directory.

The URL of the temporary file is obtained by appending the temporary file name to the temporary directory path, and assigned to WebChartViewer.ImageUrl. WebChartViewer will put the URL in an <IMG> tag for output to browser.

Note that for the above to work, the "/tmp_chart" directory must exist. The web server anonymous user must have sufficient privileges to write to that directory.

In most systems, by default, the anonymous user cannot write to the web application directory or any of its subdirectory. You may need to modify the security settings. The recommended setting is to grant the "Everyone" group read, write and delete access to the "/tmp_chart" directory.

As an alternative to temporary files, one can use normal files (files with given file names, rather than automatically generated) as the storage. Please refer to Creating Charts as Image Files for details.