ASP.NET - How to create charts from Database Data in Asp.net MVC3

Asked By Sameer Khan on 05-Dec-11 09:16 AM
Earn up to 30 extra points for answering this tough question.

Hi,

My application is in MVC3, i want to create chart of some record from the Database.Not many proper examples are available for MVc3 charts.

Can anyone guide or provide with some good tutorial to create chart from Database data.

This is sample of  how im creating the chart

//var key = new Chart(width: 600, height: 400)
       //    .AddSeries(
       //      chartType: "bar",
       //      legend: "Rainfall",
       //      xValue: new[] { "Jan", "Feb", "Mar", "Apr", "May" },
       //      yValues: new[] { "10", "20", "30", "40", "50" })
       //    .Write();
     
      return View();

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.
This is my query below
var res=from StudRes in db.StudensResult select StudRes.
How can i assign values of columns names to X axis and the Data to Y axis.
Thanks

Riley K replied to Sameer Khan on 05-Dec-11 12:07 PM


To pass a data source that isn’t hard coded, you can use the DataBindTable method. It accepts any value that returns an IEnumerable object. 

public ActionResult GetMonths()
      {
      var d = new DateTimeFormatInfo();
      var key = new Chart(width: 600, height: 400)
          .AddSeries(chartType: "column")
          .DataBindTable(d.MonthNames)
          .Write(format: "gif");
      return null;
      }

http://weblogs.asp.net/gunnarpeipman/archive/2010/10/15/chartresult-testable-asp-net-mvc-3-controller-actions-that-return-chart.aspx

Regards
Suchit shah replied to Sameer Khan on 05-Dec-11 01:46 PM
Like the ASP.NET Chart control, the Chart helper can render an image that displays data in a variety of chart types. It supports many options for formatting and labeling. The Chart helper can render more than 30 types of charts, including all the types of charts that you might be familiar with from Microsoft Excel or other tools - area charts, bar charts, column charts, line charts, and pie charts, along with more specialized charts like stock charts. Moreover, the Chart helper can take data source from a common array, an XML file, and database.


Now, let's take a look at the definition of the helper Chart disassembled using Object Browser.

http://dotnetslackers.com/articles/aspnet/Experience-ASP-NET-MVC-3-Beta-New-View-Helpers-Part1.aspx


Just look on this link it helps