How to create Graphs,charts from Database Data in MVC3

Posted by Ravinderk under ASP.NET on 4/30/2012 | Points: 10 | Views : 6904 | Status : [Member] | Replies : 2
Hi to all,

I have a table in my Database. Where the marks are stored of the students.
I want that the name of the Subjects should be displayed in the X-Axis and marks of those subjects in the Y-Axis.
How can i assign values of columns names to X axis and the Data to Y axis.



Posted by: Ravinderk on: 5/7/2012 [Member] Starter | Points: 25

I got solutions for my issue. The solution is given below....

I have created a database with 2 tables called Users and Countries. Now I want to generate a graph on How many users are there in each countries. For this I assigned the Users at Y-Axis and Countries are at X-Axis. Code is below here I create a method called Graph() to generate the graph

My Controller is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;
using Carrots.Web.Models;

namespace Carrots.Web.Controllers

public class ReportSystemController : Controller

private DatabaseContext db = new DatabaseContext();

public ActionResult Index()
return View();

public ActionResult Graph()
var countries = db.Countries.ToList();

List<string> countryNames = new List<string>();
List<string> countryUsers = new List<string>();
int cu = -1;

foreach (Country country in countries)

cu = (from x in db.Users
where x.CountryID == country.CountryID
select x).Count();

var bytes = new Chart(width: 600, height: 400)
chartType: "Column",
legend: "Country vs User",
xValue: countryNames.ToArray(),
yValues: countryUsers.ToArray())
return File(bytes, "image/png");

And my View is :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2> Analysis in Graphs </h2>

<%:Html.ActionLink("No of Recipients received an offer", "Graph")%>&nbsp &nbsp &nbsp &nbsp


I think this will help you.....

Ravinderk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jure1111 on: 6/27/2012 [Member] Starter | Points: 25

can you post database schema?

Ravinderk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response