Click here to Skip to main content
14,091,993 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 28 Jul 2014
Licenced CPOL

Microsoft Chart in MVC Application

, 28 Jul 2014
Rate this:
Please Sign up or sign in to vote.
In this tip, we will implement Microsoft chart chart in MVC application


Chart and graph are common in almost all business applications. In Web Form, we can use Microsoft’s chart tool to implement various charts which are pretty simple and nice in look but in MVC, how we will implement chart? Probably there are many approaches like you may use JavaScript based chart which deals with JSON data and there are ‘n’ number of JavaScript frameworks which serves charts and graphs. But in this example, we will use Microsoft’s in built chart which is defined within “System.Web.Helpers” namespace. So, let’s create one MVC application and add the below view into it. The view is very simple to understand. It contains just three image element. The Url.Action() is pointing to three different actions which are defined within current controller.

    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
                    <img src="@Url.Action("CreateBar")" alt="Bar Chart using MVC" />
                    <img src="@Url.Action("CreatePie")" alt="Pie Chart using MVC" />
                <td colspan="2">
                    <img src="@Url.Action("CreateLine")" alt="Line Chart using MVC" />


Now, we have to define the controller and actions to implement chart. Have a look at the below code:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.SessionState;
using System.Web.Helpers;

namespace Authentication.Controllers
    public class HomeController : Controller
        public ActionResult CreateBar()
            //Create bar chart
            var chart = new Chart(width:300,height:200)
            .AddSeries(     chartType: "bar",
                            xValue: new[] { "10 ", "50", "30 ", "70" },
                            yValues: new[] { "50", "70", "90", "110" })
            return File(chart, "image/bytes");

        public ActionResult CreatePie()
            //Create bar chart
            var chart = new Chart(width: 300, height: 200)
            .AddSeries(chartType: "pie",
                            xValue: new[] { "10 ", "50", "30 ", "70" },
                            yValues: new[] { "50", "70", "90", "110" })
            return File(chart, "image/bytes");

        public ActionResult CreateLine()
            //Create bar chart
            var chart = new Chart(width: 600, height: 200)
            .AddSeries(chartType: "line",
                            xValue: new[] { "10 ", "50", "30 ", "70" },
                            yValues: new[] { "50", "70", "90", "110" })
            return File(chart, "image/bytes");

        public ActionResult Index()
            return View("Index");

The code is very clear and self explanatory. We have set various parameters of Chart class and calling the AddSeries() method to set value in X and Y axis, then we are setting value “png” as argument of GetBytes() function, so it will return “png” image as chart. At last, we are returning File as return data of action and in output, we will see that three different charts will get created as below:

Border Line

The chart has generated with default setting, in need we can implement our custom look and feel. The chart is very simple and easy to implement compared to various JavaScript based charts and even we can render this chart asynchronously so that it will reduce render time.


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


About the Author

Sourav Kayal
Software Developer DELL International
India India
I am software developer from INDIA. Beside my day to day development work, i like to learn new technologies to update myself. I am passionate blogger and author in various technical community including , and codeproject. My area of interest is modern web technology in Microsoft stack. Visit to my personal blog here.

You may also be interested in...


Comments and Discussions

QuestionHow to set multiple color for bar chart series. Pin
Rahul Newara3-Nov-17 4:00
professionalRahul Newara3-Nov-17 4:00 
QuestionBrowser Compatability Issues Pin
Member 1319063011-May-17 19:14
memberMember 1319063011-May-17 19:14 
Generalcool. simple Pin
iceMail13-Feb-15 17:58
membericeMail13-Feb-15 17:58 
GeneralMy vote of 4 Pin
gicalle7529-Jul-14 21:12
professionalgicalle7529-Jul-14 21:12 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190518.1 | Last Updated 28 Jul 2014
Article Copyright 2014 by Sourav Kayal
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid