In the direct streaming method, an <IMG> is sent to the browser without creating the chart image first. When the browser uses the URL to load the chart image, the server creates the image on the fly and streams it back to the browser. In a typical case, the <IMG> tag is like:
<img src="createChart.aspx">
In the above, "createChart.aspx" is a script that creates the chart on the fly. It can then stream the chart image to the browser with Response.BinaryWrite. An example is:
[ASP.NET - VB Version]
<%@ Language="VB" Debug="true" %>
<%@ Import Namespace="ChartDirector" %>
<%
'The data for the bar chart
Dim data() As Double = {85, 156, 179.5, 211, 123}
'The labels for the bar chart
Dim labels() As String = {"Mon", "Tue", "Wed", "Thu", "Fri"}
'Create a XYChart object of size 250 x 250 pixels
Dim c As XYChart = New XYChart(250, 250)
'Set the plotarea at (30, 20) and of size 200 x 200 pixels
c.setPlotArea(30, 20, 200, 200)
'Add a bar chart layer using the given data
c.addBarLayer(data)
'Set the x axis labels using the given labels
c.xAxis().setLabels(labels)
'output the chart
Response.ContentType = "image/png"
Response.BinaryWrite(c.makeChart2(Chart.PNG))
Response.End
%>
[ASP.NET - C# Version]
<%@ Language="C#" Debug="true" %>
<%@ Import Namespace="ChartDirector" %>
<%
//The data for the bar chart
double[] data = {85, 156, 179.5, 211, 123};
//The labels for the bar chart
string[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};
//Create a XYChart object of size 250 x 250 pixels
XYChart c = new XYChart(250, 250);
//Set the plotarea at (30, 20) and of size 200 x 200 pixels
c.setPlotArea(30, 20, 200, 200);
//Add a bar chart layer using the given data
c.addBarLayer(data);
//Set the x axis labels using the given labels
c.xAxis().setLabels(labels);
//output the chart
Response.ContentType = "image/png";
Response.BinaryWrite(c.makeChart2(Chart.PNG));
Response.End();
%>
For ASP.NET MVC, the <IMG> URL can point to an action:
<img src="/myController/CreateChart">
The action can create the chart as usual, and stream the chart image to the browser using the File ActionResult. An example is:
public ActionResult CreateChart()
{
//The data for the bar chart
double[] data = {85, 156, 179.5, 211, 123};
//The labels for the bar chart
string[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};
//Create a XYChart object of size 250 x 250 pixels
XYChart c = new XYChart(250, 250);
//Set the plotarea at (30, 20) and of size 200 x 200 pixels
c.setPlotArea(30, 20, 200, 200);
//Add a bar chart layer using the given data
c.addBarLayer(data);
//Set the x axis labels using the given labels
c.xAxis().setLabels(labels);
//output the chart
return File(c.makeChart2(Chart.PNG), "image/png");
}
Note that with the direct streaming method, you output HTML in the charting code, as the output is for the <IMG> tag, which supports only images.