Click here to Skip to main content
15,878,953 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.6K   1.5K   45  
Using a proper MVC pattern to stream an image from Microsoft Chart controls for the Microsoft .NET Framework 3.5.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.IO;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;

namespace Mvc_Chart.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Title"] = "Home Page";
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            return View();
        }

        public ActionResult About()
        {
            ViewData["Title"] = "About Page";

            return View();
        }

        public ActionResult XamlOnly()
        {
            ViewData["Title"] = "XamlOnly";
            ViewData["Chart"] = Models.StaticModel.createStaticData();
            return View();
        }

        public ActionResult XamlAndCodeBehind()
        {
            ViewData["Title"] = "XamlAndCodeBehind";
            ViewData["Chart"] = Models.StaticModel.createStaticData();
            return View();
        }

        public ActionResult ControllerStream()
        {
            ViewData["Title"] = "ControllerStream";
            return View();
        }

        public FileResult GetChart()
        {
            ViewData["Chart"] = Models.StaticModel.createStaticData();
            System.Web.UI.DataVisualization.Charting.Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart();
            Chart2.Width = 412;
            Chart2.Height = 296;
            Chart2.RenderType = RenderType.ImageTag;
            Chart2.Palette = ChartColorPalette.BrightPastel;
            Title t = new Title("IMG source streamed from Controller", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
            Chart2.Titles.Add(t);
            Chart2.ChartAreas.Add("Series 1");
            // create a couple of series   
            Chart2.Series.Add("Series 1");
            Chart2.Series.Add("Series 2");
            // add points to series 1   
            foreach (int value in (List<int>)ViewData["Chart"])
            {
                Chart2.Series["Series 1"].Points.AddY(value);
            }
            // add points to series 2   
            foreach (int value in (List<int>)ViewData["Chart"])
            {
                Chart2.Series["Series 2"].Points.AddY(value + 1);
            }
            Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            Chart2.BorderlineWidth = 2;
            Chart2.BorderColor = System.Drawing.Color.Black;
            Chart2.BorderlineDashStyle = ChartDashStyle.Solid;
            Chart2.BorderWidth = 2;
            Chart2.Legends.Add("Legend1");
            MemoryStream imageStream = new MemoryStream();
            Chart2.SaveImage(imageStream, ChartImageFormat.Png);
            return new FileResult("Yo.png", "image/png", imageStream.ToArray());
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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