How to create Graphs,charts from Database Data in Asp.net MVC3

Posted by Ravinderk under ASP.NET on 4/30/2012 | Points: 10 | Views : 6261 | 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.


Thanks




Responses

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

Up
0
Down
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)
{
countryNames.Add(country.Name);


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


countryUsers.Add(cu.ToString());
}
var bytes = new Chart(width: 600, height: 400)
.AddSeries(
chartType: "Column",
legend: "Country vs User",
xValue: countryNames.ToArray(),
yValues: countryUsers.ToArray())
.GetBytes("png");
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">
Index
</asp:Content>

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

<h2> Analysis in Graphs </h2>

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

</p>
</asp:Content>


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

Up
0
Down
can you post database schema?

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

Login to post response