Monday, 13 March 2017

MVC Pie Chart From DataBase Using C#.Net | MVC Pie Chart Example | ASP.NET MVC Pie Charts Example

3/13/2017 - By Pranav Singh 2

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.

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" />




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. how to draw the chart?index page tutorial left out?

    ReplyDelete
    Replies
    1. Hi I have added the code to draw the chart. please check if you any issue plz drop a comment. Thanks

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top