Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET

Streaming Chart Images as FileResult from MVC Controllers

Rate me:
Please Sign up or sign in to vote.
4.93/5 (11 votes)
10 Mar 2009CPOL2 min read 116.3K   1.5K   45   21
Using a proper MVC pattern to stream an image from Microsoft Chart controls for the Microsoft .NET Framework 3.5.

Image 1

three_charts.png

Introduction

Not a lot of server controls play nicely with the ASP.NET MVC framework; Microsoft Chart does. To implement a true MVC design pattern, the Controller should relate to the View any dependencies from the Model (data). This project shows how the Microsoft Chart controls can be streamed from the Controller into an MVC View. The article assumes a basic knowledge of the ASP.NET MVC framework (Dino Esposito article here) and the restrictions associated with using server controls (no control-level events in the code-behind and the control won’t be able to postback).

Background

The use of the Microsoft Chart in the MVC framework is fairly well documented. Code-Inside Blog International gives a good introduction here. The author's examples have been included on the first two links on the default page of the project. Reading the responses to the article, I stumbled onto an interesting post by Florian (January 18 2009 @ 10:46 pm). He states:

A third option that works very well is to have the following code in your ASPX:

HTML
<img src="/Product/GetChart" alt="Chart" />

This calls the GetChart action on the myProduct controller. The action creates a chart in memory, saves the chart as an image to a temp folder, and then streams the image of the chart to the client. By the way, the action returns a FileResult.

The main advantage of this third method is that it does not require the ASPX page to have any ASP server controls, and subsequently no code-behind is needed.

I looked into FileResult (derived from ActionResult) and found an interesting article on thuban.org :: Keith J. Farmer's blog here. Adopting this code, I quickly found the overload for the Chart.SaveImage that used a stream and plugged it all together (see below).

The View code:

streaming_view.png

The Controller code:

get_chart_action_result.png

Using the code

To run the project, the following components must be installed:

To use the mechanisms discussed in this article in your own project, you will need these additional steps:

  • add the sections shown in the Code-Inside Blog International article to the web.config.
  • add the chart or img markup (depending on the approach).
  • add the code-behind (method 2) or the controller methods (method 3).
  • include the FileResult class, if using method 3.

The examples shown are simplified, and do not draw data from a database.

Points of interest

The streaming of the image plugged nicely into the Chart API - they catered for this Use Case. On the downside, the data-binding on this release of the Microsoft Chart Controls for Microsoft .NET Framework 3.5 is quite primitive and is not LINQ or Entity friendly.

History

  • 2009-Mar-09 - Typos and source-code update.
  • 2009-Mar-05 - First submission.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Encore Software
Australia Australia
Contractor in Desktop and Web applications.
Gold Coast, Queensland.

Comments and Discussions

 
QuestionWhat if i have to show tooltips in the charts?? Pin
M Ali Raza11-Aug-09 0:40
M Ali Raza11-Aug-09 0:40 
AnswerRe: What if i have to show tooltips in the charts?? Pin
Nic_Roche11-Aug-09 10:25
professionalNic_Roche11-Aug-09 10:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.