Tuesday, August 30, 2011

Chart Helpers in ASP.NET MVC3- Create chart with asp.net mvc3

I am exploring ASP.NET MVC3 and everyday I am learning something new. In today’s post I am going to explain how we can create chart with ASP.NET MVC3.

Chart is one of greatest way of expressing figures. We all need that functionality in any commercial application. I am still remembering old days with ASP.NET  where we have to use the third party controls for charts or we have to create it with the use of CSS and Server side controls. But now with ASP.NET MVC Helpers you can very easily create chart in few lines of code.

ASP.NET MVC Chart helper and other helpers comes inbuilt with the latest ASP.NET Tools update. If you don’t have ASP.NET MVC Chart helpers then you can find more information about it from here.

http://haacked.com/archive/2011/04/12/introducing-asp-net-mvc-3-tools-update.aspx

But still if you have not updated it then don’t worry about it. There is nuget package called “ASP.NET Webhelper Library”. You can add that package like following and still use same functionality.

ChartHelper-ASP.NET Web Helpers Library Pacakge

Now once you have all set for reference. We can start working on coding part. So draw a chart I have taken simple example like number of records vs time take chart. This Chart Helper support there types of cjart.
  1. Bar Chart
  2. Pie Chart
  3. Column Chart
In this example we are going to use Bar Chart. Following is code for that. I have create one simple action result called “DrawChart” which will return chart as PNG format.

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

Here you can see that first I have included System.Web.Helper name space on top and then in DrawChart Action Result I have created chart variable where I have specified the height and width of chart and in add series method of it I have provided the chart type and x axis and y axis value. Now as we have to display it as image so for that I have used @URL.Action to point src to our Action Result DrawChart like following.
<img src="@Url.Action("DrawChart")" alt="Drawing chart with HTML Helper" />

Now let’s run this application in browser via pressing Ctrl+F5 and following is the output as expected.

Output

That’s it. You can see its very to create any kind of chart with Chart Helpers in ASP.NET MVC 3. Hope you like it. Stay tuned for more.. Till then happy programming and Namaste!!

Shout itkick it on DotNetKicks.com
Share:
Sunday, August 28, 2011

Visual Studio 2010 Features post list on my blog

Share:
Saturday, August 27, 2011

ILSpy-Alternative of .NET Reflector

Sometimes we need to have decomposer tool or reflector tool for our applications to improve the performance or to know the internals of the assembly we have created. I was using Red Gate .NET Reflector earlier for same as it was free. Now Red Gate has made that tool paid version so I was finding some tools from that I can decompile the stuff. After digging some time on the internet I have found a great tool ILSpy which almost giving same functionalities like .NET Reflector. You can download that tool from following link.

http://wiki.sharpdevelop.net/ilspy.ashx

You will get following features in ILSpy. It is also listed on above page.
  • Assembly browsing
  • IL Disassembly
  • Decompilation to C#
    • Supports lambdas and 'yield return'
    • Shows XML documentation
  • Saving of resources
  • Search for types/methods/properties (substring)
  • Hyperlink-based type/method/property navigation
  • Base/Derived types navigation
  • Navigation history
  • BAML to XAML decompiler
  • Save Assembly as C# Project
  • Find usage of field/method
  • Extensible via plugins (MEF)

Now let’s see how we can use ILSpy for decompiling our application. I have created a very basic console application which will concat string and print name. Here I have intentionally used string concatenation instead of StringBuilder class to show what’s going internally with ILSpy. Following is code for that.

using System;
namespace ThisExample
{
     class Program
     {
          static void Main(string[] args)
          {
              Test test = new Test();
              test.PrintName();
           }
     }
     public class Test
     {
          private string name = "Jalpesh Vadgama";
          public void PrintName()
          {
              name += " vishal vadgama";
              Console.WriteLine(name);
          }
     }
}

Once we are done with code let’s run that application and following will be a output.

Output window for ILSpy-Alternative of .NET Reflector

Let’s check that console application with the ILSpy. Once you double click exe of ILSpy it will look like following.

ILSpy- Alternative of .NET Reflector

Now let’s open our application via File->Open Menu. So it will load our application like following


ClassHirerchay with ILSpy-.NET Reflector Alternative

As you can see in above screenshot It has loaded whole class hierarchy of console application we have just created as we can see Program and Test class directly and on right hand pane it has loaded whole assembly information for this application. You can see that in below image.


AssemblyInformation from ILSpy- Alternative of .NET Reflector

Now once you click on program it will load program information on right pane like following.

RightPane of code from ILSpy-.NET Reflector Alternative

Event it’s provide IL mode also so you can see what’s going on internally on top its having button like this.

You can select IL from ILSpy-Alternative .NET Reflector

Once you select IL you right pane will load IL like following.In that you can see its using concat method

ILRightPane

So you can see its almost providing functionalities which was provided by .NET Reflector. Hope you like it.. stay tuned for more..till then happy programming.

Shout it
kick it on DotNetKicks.com
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews