I am using Highchart plugin to generate stack column chart. Here, I will use the same concept to create a web method in a web service and calling those methods in jQuery Ajax.

Step 1: Create a table as in the following:

  1. CREATE TABLE tblRevenue(
  2. Id int primary key IDENTITY(1,1) NOT NULL,
  3. year varchar](4) NULL,
  4. quarter [varchar](4) NULL,
  5. amount bigint NULL
  6. )

But in real time scenario you might get data from one or more tables using joins.

After completion of table design, enter some of the test data into the table to work for our sample.

I have attached script in sample download files, you can use that script to execute into the database.

Step 2: Create a stored procedure.

  1. CREATE PROCEDURE Pr_getrevenue
  2. AS
  3. BEGIN
  4. SELECT * FROM
  5. (SELECT year,quarter,Sum(amount)amount
  6. FROM tblrevenue
  7. GROUP BY year,quarter) AS s
  8. PIVOT ( Sum(amount)
  9. FOR [quarter] IN ([Q1],[Q2],[Q3],[Q4])
  10. )AS pv
  11. END

Step 3: Create an ASP.NET Web service. Add an .asmx page to the current solution and modify the code as in the following example:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Data; //
  7. using System.Data.SqlClient; //
  8. [WebService(Namespace = "http://tempuri.org/")]
  9. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  10. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  11. [System.Web.Script.Services.ScriptService]
  12. public class ChartWebService: System.Web.Services.WebService
  13. {
  14. public class RevenueEntity
  15. {
  16. public string year
  17. {
  18. get;
  19. set;
  20. }
  21. public int quarter1
  22. {
  23. get;
  24. set;
  25. }
  26. public int quarter2
  27. {
  28. get;
  29. set;
  30. }
  31. public int quarter3
  32. {
  33. get;
  34. set;
  35. }
  36. public int quarter4
  37. {
  38. get;
  39. set;
  40. }
  41. }
  42. [WebMethod]
  43. public List < RevenueEntity > GetRevenueDetails()
  44. {
  45. List < RevenueEntity > revenues = new List < RevenueEntity > ();
  46. using(SqlConnection con = new SqlConnection("Data Source=.;Trusted_Connection=true;DataBase=test"))
  47. {
  48. using(SqlCommand cmd = new SqlCommand())
  49. {
  50. cmd.Connection = con;
  51. cmd.CommandType = CommandType.StoredProcedure;
  52. cmd.CommandText = "Pr_getrevenue";
  53. SqlDataAdapter da = new SqlDataAdapter(cmd);
  54. DataSet ds = new DataSet();
  55. da.Fill(ds, "Revenue");
  56. if (ds != null)
  57. {
  58. if (ds.Tables.Count > 0)
  59. {
  60. if (ds.Tables["Revenue"].Rows.Count > 0)
  61. {
  62. foreach(DataRow dr in ds.Tables["Revenue"].Rows)
  63. {
  64. revenues.Add(new RevenueEntity
  65. {
  66. year = dr["year"].ToString(), quarter1 = Convert.ToInt32(dr["Q1"]),
  67. quarter2 = Convert.ToInt32(dr["Q3"]), quarter3 = Convert.ToInt32(dr["Q3"]), quarter4 = Convert.ToInt32(dr["Q4"])
  68. });
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. return revenues;
  76. }
  77. }
Don't forget to enable the following attributes in web service.

[System.Web.Script.Services.ScriptService]

Step 4: Add jQuery references as in the following:

  1. <script src="Script/jquery.min.js" type="text/javascript"></script>
  2. <script src="Script/highcharts.js" type="text/javascript"></script>

Step 5: Implement jQuery Ajax as in the following.

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. $.ajax({
  4. type: "POST",
  5. contentType: "application/json; charset=utf-8",
  6. url: "Services/ChartWebService.asmx/GetRevenueDetails",
  7. data: "{}",
  8. dataType: "json",
  9. success: function(Result) {
  10. Result = Result.d;
  11. var series = [];
  12. var categories = [];
  13. var quarter1 = [];
  14. var quarter2 = [];
  15. var quarter3 = [];
  16. var quarter4 = [];
  17. for (var i in Result) {
  18. categories.push(Result[i].year);
  19. quarter1.push(Result[i].quarter1);
  20. quarter2.push(Result[i].quarter2);
  21. quarter3.push(Result[i].quarter3);
  22. quarter4.push(Result[i].quarter4);
  23. }
  24. series.push({
  25. name: 'Quarter 1',
  26. data: quarter1
  27. },
  28. {
  29. name: 'Quarter 2',
  30. data: quarter2
  31. },
  32. {
  33. name: 'Quarter 3',
  34. data: quarter3
  35. },
  36. {
  37. name: 'Quarter 4',
  38. data: quarter4
  39. }
  40. );
  41. BindChart(categories, series);
  42. },
  43. error: function(xhr) {
  44. alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
  45. }
  46. });
  47. });
  48. function BindChart(categories, series) {
  49. $('#container').highcharts({
  50. chart: {
  51. type: 'column'
  52. },
  53. title: {
  54. text: 'Stack Column Chart Demo'
  55. },
  56. xAxis: {
  57. categories: categories,
  58. labels: {
  59. style: {
  60. color: 'black',
  61. fontWeight: 'bold',
  62. fontSize: '14px'
  63. },
  64. }
  65. },
  66. legend: {
  67. itemStyle: {
  68. fontSize: '15px',
  69. font: '15pt Trebuchet MS, Verdana, sans-serif',
  70. color: '#0000FF'
  71. }
  72. },
  73. yAxis: {
  74. min: 0,
  75. title: {
  76. text: 'Amount in (Rs.)'
  77. },
  78. labels: {
  79. style: {
  80. color: 'black',
  81. fontWeight: 'bold',
  82. fontSize: '12px'
  83. }
  84. },
  85. stackLabels: {
  86. enabled: true,
  87. style: {
  88. fontWeight: 'bold',
  89. fontSize: '15px',
  90. color: 'black'
  91. }
  92. }
  93. },
  94. tooltip: {
  95. formatter: function() {
  96. return '<b>' + this.x + '</b><br/>' +
  97. this.series.name + ': ' + this.y + '<br/>' +
  98. 'Total: ' + this.point.stackTotal;
  99. }
  100. },
  101. plotOptions: {
  102. column: {
  103. stacking: 'normal',
  104. dataLabels: {
  105. enabled: true,
  106. color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
  107. style: {
  108. textShadow: '0 0 3px black'
  109. }
  110. }
  111. }
  112. },
  113. series: series
  114. });
  115. }
  116. </script>

Step 6: UI Design as in the following.

  1. <div id="container">
  2. </div>

Step 7: Check output in the browser (see below screenshot of Stack Column Chart).

I hope you liked this article and understood how to bind a Stack Column Chart in ASP.NET using jQuery Ajax.