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:
- CREATE TABLE tblRevenue (
- Id int Primary Key IDENTITY(1,1) NOT NULL,
- amount bigint NULL,
- quarter varchar(4) NULL,
- year varchar(4) NULL,
- )
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:
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:
- <script src="Script/jquery.min.js" type="text/javascript"></script>
- <script src="Script/highcharts.js" type="text/javascript"></script>
- <script src="Script/drilldown.js" type="text/javascript"></script>
Step 4
Implement jQuery Ajax as in the following:
- <script type="text/javascript">
-
- $(document).ready(function () {
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- url: "Services/WebServiceChart.asmx/GetRevenueByYear",
- data: "{}",
- dataType: "json",
- success: function (Result) {
-
- Result = Result.d;
- var data = [];
-
- for (var i in Result) {
- var serie = { name: Result[i].year, y: Result[i].amount, drilldown: Result[i].drilldown };
- data.push(serie);
- }
-
- BindChart(data);
- },
- error: function (Result) {
- alert("Error");
- }
- });
- });
-
- function BindChart(seriesArr) {
- $('#container').highcharts({
- chart: {
- type: 'column',
- backgroundColor: '#CCE6FF',
- borderColor: '#6495ED',
- borderWidth: 2,
- className: 'dark-container',
- plotBackgroundColor: '#F0FFF0',
- plotBorderColor: '#6495ED',
- plotBorderWidth: 1,
- events: {
-
- drilldown: function (e) {
- if (!e.seriesOptions) {
- var chart = this;
- chart.showLoading('Loading Quarter wise Revenue ...');
- var dataArr = CallChild(e.point.name);
- chart.setTitle({
- text: 'Quarter wise Revenue Report'
- });
- data = {
- name: e.point.name,
- data: dataArr
- }
- setTimeout(function () {
- chart.hideLoading();
- chart.addSeriesAsDrilldown(e.point, data);
- }, 1000);
- }
- }
- }
- },
- title: {
- text: 'Year wise Revenue Report'
- },
- xAxis: {
- type: 'category'
- },
-
-
- plotOptions: {
- series: {
- borderWidth: 0,
- dataLabels: {
- enabled: true
- }
- }
- },
- series: [{
- name: 'Year',
- colorByPoint: true,
- data: seriesArr
- }],
-
- drilldown: {
- series: []
- }
- });
- }
-
- function CallChild(name) {
- var Drilldowndata = [];
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- url: "Services/WebServiceChart.asmx/GetRevenueByQuarter",
- data: JSON.stringify({ year: name }),
- dataType: "json",
- success: function (Result) {
- Result = Result.d;
- for (var i in Result) {
- var serie = { name: Result[i].year, y: Result[i].amount };
- Drilldowndata.push(serie);
- }
- },
- error: function (Result) {
- alert("Error");
- }
- })
- return Drilldowndata;
- }
- t;/script>
Step 5
Do the UI Design as in the following:
- <body>
- <form id="form1" runat="server">
- <div id="container">
- </div>
- </form>
- </body>
Step 6
The output in a browser is as in the following:
After clicking on bar the following chart by quarter will be generated.
I hope you like this article and understood how to bind a drilldown highchart in ASP.NET using jQuery Ajax.