This
article will show you how you can create a pie 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 PieTypeChart()
{
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 PieTypeChart()
{
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;
}
}
}
|
Now lets have a look of the html code os layout page. This code we will use to code the chart.
@{
Layout = null;
}
<img src="@Url.Action("pietypechart")" alt="Employee pie chart" />
|
how to draw the chart?index page tutorial left out?
ReplyDeleteHi I have added the code to draw the chart. please check if you any issue plz drop a comment. Thanks
Delete