Pages

Wednesday, January 4, 2012

Charts and graphs using jquery and charting library the HighCharts in asp.net


In this jquery tutorial you will learn how to develop charts and graphs for your website using jquery and charting library the HighCharts. It doesn't matter in which programming language you are developing your website; using Highcharts it's very easy to add interactive charts to your website. Following is the screen shot of the Chart that we will learn to implement. You can develop any sort of chart and graph depend upon your requirements, it doesn't matter how complex the chart or graph you want to develop, using Highcharts every complex chart is easy to develop :-)


Charts and graphs using jquery and charting library the HighCharts

Why HighCharts?

1) Highcharts is cross browser compatible. Even supported in Iphone/Ipad.

2) Easy to integrate.

3) Easy to use.

4) It's free for non commercial use.

5) Highcharts is solely based on native browser technologies and doesn't require any client side plugins like Flash Player or Java.

6) Highcharts supports column, line, spline, area, areaspline, bar, pie and scatter chart types. Further more, you can combine any of these in one chart.

7) Simple Configuration Syntax.

8) Export and Print facility.

To know about more features, please visit www.highcharts.com.

How to develop charts and graphs using Highcharts?


As I have already told you that highcharts is very easy to use. It requires two js files to run: The highcharts.js core and either the jquery or the MooTools framework.

Now let's begin. Go to www.highcharts.com, click on Demo Gallery link and then on left side of the page you will find numerous types of charts, click anyone of them, it will lead you to a webpage in which you will see your desired chart that you want to integrate in your website. Under chart you will see the View Options button, click on that button and you will get the code that you have to copy/paste in your webpage to integrate that chart in your website. Before copy/paste that code, please add jquery file, highcharts library and a div in your webpage code. Give an id to that div because chart will be embedded in that div. Now Let's have a look over the following example.

Highcharts.html

  1. <html>  
  2. <head>  
  3. <title>Using Highcharts to integrate charts and graphs in your webpage</title>  
  4. <script src="jquery-1.4.1.min.js" type="text/javascript"></script>  
  5. <script type="text/javascript" src="highcharts.js"></script>  
  6. <script type="text/javascript">  
  7. var chart = null;  
  8. var _MyArray = new Array(13, 25, 32);  
  9. var _MyArray2 = new Array(8, 14, 54);  
  10. $(document).ready(function () {  
  11. chart = new Highcharts.Chart({  
  12. chart: {  
  13. renderTo: 'container',  
  14. defaultSeriesType: 'column'  
  15. },  
  16. title: {  
  17. text: 'Fruit Consumption'  
  18. },  
  19. xAxis: {  
  20. categories: ['Apples', 'Oranges', 'Mangos']  
  21. },  
  22. yAxis: {  
  23. title: {  
  24. text: 'Fruit eaten'  
  25. }  
  26. },  
  27. series: [{  
  28. name: 'Haris',  
  29. data: _MyArray  
  30. },  
  31. {  
  32. name: 'Adeel',  
  33. data: _MyArray2  
  34. }]  
  35. });  
  36.   
  37.   
  38. $('tspan').last().remove();  
  39. });  
  40. </script>  
  41. </head>  
  42. <body>  
  43. <form id="form1">  
  44. <div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>  
  45. </form>  
  46. </body>  
  47. </html>  

That's it, chart is ready to view. Now let's understand the code. First of all I include the jquery file and highcarts library. And then I copy/paste the code that I get from highcharts.com for the chart that I want to integrate in my webpage.

The code is quite simple. In first line, chart variable is declared that contains all of the settings of the chart. After that have declared two arrays containing three elements. InrenderTo option I have given the id of div that is available in body section of.html page, the chart will be embedded in that div. I have also specified the width and height of the div that will become the width and height of your chart. I have declared two series, one for Haris Fakhar and one for Adeel Fakhar and passed both arrays to those series respectively. 

$('tspan').last().remove(); line removes the Highcharts.com label from your webpage but I personally think the highcharts.com label should be in your webpage because all credit goes to highcharts.

As you have seen, I hard code the data in both arrays. Now let's have a look over the following example to pass dynamic data to those highcharts' arrays using ASP.NET technology.

Highcharts.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="highcharts.aspx.cs" Inherits="highcharts" %>  
  2.   
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5.   
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head id="Head1" runat="server">  
  9. <title>  
  10. Charts and graphs using jquery and charting library the HighCharts  
  11. </title>  
  12. <script src="jquery-1.4.1.min.js" type="text/javascript"></script>  
  13. <script type="text/javascript" src="highcharts.js"></script>  
  14. </head>  
  15. <body>  
  16.   
  17.   
  18. <form id="form1" runat="server">  
  19. <script type="text/javascript" language="javascript">  
  20. var firstSeries='<%=seriesOne %>';  
  21. var secondSeries='<%=seriesTwo %>';  
  22.   
  23.   
  24. //debugger;  
  25.   
  26. var seriesOne = new Array();  
  27. seriesOnefirstSeries.split(',');  
  28. for(var i=0;i<seriesOne.length;i++)  
  29. {  
  30. seriesOne[i]= parseInt(seriesOne[i]);  
  31. }  
  32. var seriesTwo = new Array();  
  33. seriesTwosecondSeries.split(',');  
  34. for(var i=0;i<seriesTwo.length;i++)  
  35. {  
  36. seriesTwo[i]= parseInt(seriesTwo[i]);  
  37. }  
  38. var chart = null;  
  39. var _MyArray = seriesOne;  
  40. var _MyArray2 = seriesTwo;  
  41. var _Series1='Haris Fakhar';  
  42. var _Series2='Adeel Fakhar';  
  43.   
  44. $(document).ready(function ()  
  45. {  
  46. chart = new Highcharts.Chart({  
  47. chart: {  
  48. renderTo: 'container',  
  49. defaultSeriesType: 'column'  
  50. },  
  51. title: {  
  52. text: 'Fruit Consumption'  
  53. },  
  54. xAxis: {  
  55. categories: ['Apples', 'Oranges', 'Mangos']  
  56. },  
  57. yAxis: {  
  58. title: {  
  59. text: 'Fruit eaten'  
  60. }  
  61. },  
  62. series: [{  
  63. name: _Series1,  
  64. data: _MyArray  
  65. },  
  66. {  
  67. name: _Series2,  
  68. data: _MyArray2  
  69. }]  
  70.   
  71.   
  72. });  
  73. $('tspan').last().remove();  
  74. });  
  75. </script>  
  76. <div id="container" style="width: 800px; height: 400px; margin: 0 auto">  
  77. </div>  
  78. </form>  
  79. </body>  
  80. </html>  


Highcharts.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11.   
  12.   
  13. public partial class highcharts : System.Web.UI.Page  
  14. {  
  15. string[] strSplitArr = new string[3];  
  16. string[] strSplitArr2 = new string[3];  
  17. protected string seriesOne { getset; }  
  18. protected string seriesTwo { getset; }  
  19.   
  20.   
  21. protected void Page_Load(object sender, EventArgs e)  
  22. {  
  23. //Assigning values to first Array  
  24. strSplitArr[0] = "13";  
  25. strSplitArr[1] = "25";  
  26. strSplitArr[2] = "32";  
  27. //Assigning values to second Array  
  28. strSplitArr2[0] = "8";  
  29. strSplitArr2[1] = "14";  
  30. strSplitArr2[2] = "54";  
  31.   
  32.   
  33. //Assigning comma splitted array values to the seriesOne variable  
  34. foreach (string arrStr in strSplitArr)  
  35. {  
  36. seriesOne = seriesOne + arrStr + ",";  
  37. }  
  38. //Assigning comma splitted array values to the seriesTwo variable  
  39. foreach (string arrStr in strSplitArr2)  
  40. {  
  41. seriesTwo = seriesTwo + arrStr + ",";  
  42. }  
  43.   
  44.   
  45. seriesOne = seriesOne.Remove(seriesOne.Length - 1);  
  46. seriesTwo = seriesTwo.Remove(seriesTwo.Length - 1);  
  47.   
  48.   
  49. }  
  50.   
  51.   
  52. }  



try this charts..............it's good............

No comments:

Post a Comment