开发者

Retrieving data from Model or ViewModel for a Chart in ASP.NET MVC 3 Razor using Chart Helpers

I want to create a graphical chart in mvc 3 razor using the code below (thanks to DotNetJalps):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;

namespace CodeSimplifiedTest.Controllers
{
  public class HomeController : Controller
  {
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        return View();
    }

    public ActionResult About()
    {
        return View();
    }

    public ActionResult DrawChart()
    {
        var chart = new Chart(width: 300, height: 200)
            .AddSeries(
                        chartType: "bar",
                        xValue: new[] { "10 Records", "20 Records", "30 Records", "40 Records" },
     开发者_高级运维                   yValues: new[] { "50", "60", "78", "80" })
                        .GetBytes("png");
        return File(chart, "image/bytes");
    }
  }
}

But

I want to retrieve the xValues and Yvalues from a SQL Server 2008 R2 database...what code must I put after xValue: and yValue: or some code anywhere in the controller and view to retrieve data from the database? I tried Entity Framework context...but it didn't work.

Thanks


Here is what i am using to make the chart from the dbContext from EF....

public ActionResult PayorPieChart()
{
  string xx = activePhysicianID;

  ArrayList xValue = new ArrayList();
  ArrayList yValue = new ArrayList();

  XXXXXXX_MainEntities dbContext = new XXXXXXX_MainEntities();
  var results = dbContext.Payor.Where(rs => rs.PhysicianIdentity.Equals(xx));

  results.ToList().ForEach(rs => xValue.Add(rs.Identifier));
  results.ToList().ForEach(rs => yValue.Add(rs.Strength));

  var chart = new Chart(600, 300, ChartTheme.Blue);
  chart.AddSeries(chartType: "Pie", xValue: xValue, yValues: yValue);
  chart.AddTitle("Payor");
  chart.Write("png");

  return null;
}


The below link explains how to use "DataBindTable" method which binds the data from Model. Here, the data is framed inside the Model. You can get the data from sql server in the model inside the method "GetChartData".

http://weblogs.asp.net/gunnarpeipman/archive/2010/10/10/asp-net-mvc-3-beta-built-in-support-for-charts.aspx

Hope this helps!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜