I am using the same concept to create a web method in a web service and calling those methods in jQuery.

Step 1

Create a table as in the following:

  1. CREATE TABLE tblRevenue (
  2. Id int Primary Key IDENTITY(1,1) NOT NULL,
  3. amount bigint NULL,
  4. quarter varchar(4) NULL,
  5. year varchar(4) NULL,
  6. )

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



We will now create a web method in the web service and use that method to call it from jQuery.

Step 2

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. namespace DrillDownHighchart.Services
  9. {
  10. /// <summary>
  11. /// Summary description for WebServiceChart
  12. /// </summary>
  13. [WebService(Namespace = "http://tempuri.org/")]
  14. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  15. [System.ComponentModel.ToolboxItem(false)]
  16. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  17. [System.Web.Script.Services.ScriptService]
  18. public class WebServiceChart : System.Web.Services.WebService
  19. {
  20. public class RevenueEntity
  21. {
  22. public string year { get; set; }
  23. public int amount { get; set; }
  24. public Boolean drilldown { get; set; }
  25. }
  26. [WebMethod]
  27. public List<RevenueEntity> GetRevenueByYear()
  28. {
  29. List<RevenueEntity> YearRevenues = new List<RevenueEntity>();
  30. DataSet ds = new DataSet();
  31. using (SqlConnection con = new SqlConnection("Data Source=.;Trusted_Connection=true;DataBase=test"))
  32. {
  33. using (SqlCommand cmd = new SqlCommand())
  34. {
  35. cmd.CommandText = "select year,SUM(amount)amount from tblRevenue group by year";
  36. cmd.Connection = con;
  37. using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  38. {
  39. da.Fill(ds, "dsRevenue");
  40. }
  41. }
  42. }
  43. if (ds != null)
  44. {
  45. if (ds.Tables.Count > 0)
  46. {
  47. if (ds.Tables["dsRevenue"].Rows.Count > 0)
  48. {
  49. foreach (DataRow dr in ds.Tables["dsRevenue"].Rows)
  50. {
  51. YearRevenues.Add(new RevenueEntity
  52. {
  53. year = dr["year"].ToString(),
  54. amount = Convert.ToInt32(dr["amount"]),
  55. drilldown = true
  56. });
  57. }
  58. }
  59. }
  60. }
  61. return YearRevenues;
  62. }
  63. [WebMethod]
  64. public List<RevenueEntity> GetRevenueByQuarter(string year)
  65. {
  66. List<RevenueEntity> QuarterRevenues = new List<RevenueEntity>();
  67. DataSet ds = new DataSet();
  68. using (SqlConnection con = new SqlConnection("Data Source=.;Trusted_Connection=true;DataBase=test"))
  69. {
  70. using (SqlCommand cmd = new SqlCommand())
  71. {
  72. cmd.CommandText = "select quarter,SUM(amount)amount from tblRevenue where year='" + year + "' group by quarter";
  73. cmd.Connection = con;
  74. using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  75. {
  76. da.Fill(ds, "dsQuarter");
  77. }
  78. }
  79. }
  80. if (ds != null)
  81. {
  82. if (ds.Tables.Count > 0)
  83. {
  84. if (ds.Tables["dsQuarter"].Rows.Count > 0)
  85. {
  86. foreach (DataRow dr in ds.Tables["dsQuarter"].Rows)
  87. {
  88. QuarterRevenues.Add(new RevenueEntity
  89. {
  90. year = dr["quarter"].ToString(),
  91. amount = Convert.ToInt32(dr["amount"])
  92. });
  93. }
  94. }
  95. }
  96. }
  97. return QuarterRevenues;
  98. }
  99. }
  100. }

Don't forget to enable the attribute as in the following:

[System.Web.Script.Services.ScriptService]

Step 3

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>
  3. <script src="Script/drilldown.js" type="text/javascript"></script>

Step 4

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/WebServiceChart.asmx/GetRevenueByYear",
  7. data: "{}",
  8. dataType: "json",
  9. success: function (Result) {
  10. Result = Result.d;
  11. var data = [];
  12. for (var i in Result) {
  13. var serie = { name: Result[i].year, y: Result[i].amount, drilldown: Result[i].drilldown };
  14. data.push(serie);
  15. }
  16. BindChart(data);
  17. },
  18. error: function (Result) {
  19. alert("Error");
  20. }
  21. });
  22. });
  23. function BindChart(seriesArr) {
  24. $('#container').highcharts({
  25. chart: {
  26. type: 'column',
  27. backgroundColor: '#CCE6FF',
  28. borderColor: '#6495ED',
  29. borderWidth: 2,
  30. className: 'dark-container',
  31. plotBackgroundColor: '#F0FFF0',
  32. plotBorderColor: '#6495ED',
  33. plotBorderWidth: 1,
  34. events: {
  35. drilldown: function (e) {
  36. if (!e.seriesOptions) {
  37. var chart = this;
  38. chart.showLoading('Loading Quarter wise Revenue ...');
  39. var dataArr = CallChild(e.point.name);
  40. chart.setTitle({
  41. text: 'Quarter wise Revenue Report'
  42. });
  43. data = {
  44. name: e.point.name,
  45. data: dataArr
  46. }
  47. setTimeout(function () {
  48. chart.hideLoading();
  49. chart.addSeriesAsDrilldown(e.point, data);
  50. }, 1000);
  51. }
  52. }
  53. }
  54. },
  55. title: {
  56. text: 'Year wise Revenue Report'
  57. },
  58. xAxis: {
  59. type: 'category'
  60. },
  61. plotOptions: {
  62. series: {
  63. borderWidth: 0,
  64. dataLabels: {
  65. enabled: true
  66. }
  67. }
  68. },
  69. series: [{
  70. name: 'Year',
  71. colorByPoint: true,
  72. data: seriesArr
  73. }],
  74. drilldown: {
  75. series: []
  76. }
  77. });
  78. }
  79. function CallChild(name) {
  80. var Drilldowndata = [];
  81. $.ajax({
  82. type: "POST",
  83. contentType: "application/json; charset=utf-8",
  84. url: "Services/WebServiceChart.asmx/GetRevenueByQuarter",
  85. data: JSON.stringify({ year: name }),
  86. dataType: "json",
  87. success: function (Result) {
  88. Result = Result.d;
  89. for (var i in Result) {
  90. var serie = { name: Result[i].year, y: Result[i].amount };
  91. Drilldowndata.push(serie);
  92. }
  93. },
  94. error: function (Result) {
  95. alert("Error");
  96. }
  97. })
  98. return Drilldowndata;
  99. }
  100. t;/script>

Step 5

Do the UI Design as in the following:

  1. <body>
  2. <form id="form1" runat="server">
  3. <div id="container">
  4. </div>
  5. </form>
  6. </body>

Step 6

The output in a browser is as in the following:

Output in Browser

After clicking on bar the following chart by quarter will be generated.

quarter wise chart

I hope you like this article and understood how to bind a drilldown highchart in ASP.NET using jQuery Ajax.