connectseekers.com

Friday, 10 March 2017

MVC Bar Chart From DataBase Using C#.Net | MVC Chart Example | Microsoft Chart in MVC

3/10/2017 - By Pranav Singh 0

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.

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;
        }
    }
}

After this we will create the view of index.


@{
    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.





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

0 comments:

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
Email Id : contact@aspdotnet-pools.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


Top jQuery Plugins

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