This
article will show you how you can create a bar chart in mvc application. In
this I have used mvc chart class to draw the chart with c#.net.
So for this article first we will create a new mvc application and add a model class file.
So for this article first we will create a new mvc application and add a model class file.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVC_Demos.Models
{
public class ChartModel
{
public List<ChartFields>
ChartData { get; set; }
}
public class ChartFields
{
public string EmployeeName { get; set; }
public int Salery { get; set; }
}
}
|
After
adding model class file we will add the controller file. In this we will create
and ActionResult return type function. This function is responsible for drawing the chart.
First we
will prepare the data to bind the chart.
/// For gstting data
/// You can use you DB data to
bind the list
///
public List<ChartFields> ChartDataList()
{
List<ChartFields> _chart = new List<ChartFields>();
_chart.Add(new ChartFields { EmployeeName = "Emp 1", Salery = 30000 });
_chart.Add(new ChartFields { EmployeeName = "Emp 2", Salery = 20000 });
_chart.Add(new ChartFields { EmployeeName = "Emp 3", Salery = 50000 });
_chart.Add(new ChartFields { EmployeeName = "Emp 4", Salery = 60000 });
return _chart;
}
|
Here I have
shown assign the values to list. You can get data from database. Now we will see
the code to draw the chart.
public ActionResult
BarTypeChart()
{
List<ChartFields>
chartdata = new List<ChartFields>();
chartdata = ChartDataList();
var chart = new Chart(width: 300,
height: 200)
.AddSeries(chartType: "bar",
xValue:
chartdata, xField: "EmployeeName",
yValues:
chartdata, yFields: "Salery")
.AddTitle("Employee Salery")
.GetBytes("png");
return File(chart, "image/bytes");
}
|
In above code I have assign the list value as datasource with fields which we want to bind on which coordinate.
Here is the complete code of controller.
using MVC_Demos.Models;
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using
System.Web.Helpers;
using System.Web.Mvc;
namespace
MVC_Demos.Controllers
{
public class ChartController : Controller
{
//
// GET: /Chart/
public ActionResult
Index()
{
return View();
}
public ActionResult
BarTypeChart()
{
List<ChartFields>
chartdata = new List<ChartFields>();
chartdata = ChartDataList();
var chart = new Chart(width: 300,
height: 200)
.AddSeries(chartType: "bar",
xValue:
chartdata, xField: "EmployeeName",
yValues: chartdata,
yFields: "Salery")
.AddTitle("Employee Salery")
.GetBytes("png");
return File(chart, "image/bytes");
}
///
/// For gstting data
/// You can use you DB data to
bind the list
///
public List<ChartFields> ChartDataList()
{
List<ChartFields> _chart = new List<ChartFields>();
_chart.Add(new ChartFields { EmployeeName = "Emp 1", Salery = 30000 });
_chart.Add(new ChartFields { EmployeeName = "Emp 2", Salery = 20000 });
_chart.Add(new ChartFields { EmployeeName = "Emp 3", Salery = 50000 });
_chart.Add(new ChartFields { EmployeeName = "Emp 4", Salery = 60000 });
return _chart;
}
}
}
|
@{
ViewBag.Title
= "microsoft chart in mvc";
}
<img src="@Url.Action("BarTypeChart")" alt="Bar Chart using MVC" />
|
In above i have assign the control method to image. Now we have done run the application to check the output.
0 comments:
Please let me know your view