Sunday, August 11, 2019

How to create google Bar chart with database data in ASP.NET

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for get data for chart.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok. 
In this example, I have used one tables as below 

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >

hose your data connection > select your database > next > Select tables > enter Model Namespace > Finish. 

Step-5: Add a webpage and design for show google bar chart with database data.

HTML Code 
  1. <div id="chart_div" style="width:500px;height:400px">
  2. <%-- Here Chart Will Load --%>
  3. </div>

Step-6: Add Jquery code for call server side function for get data from sql server database.

Write this below jquery code inside page <head></head> section. 
Jquery Code 
  1. <%-- Here We need to write some js code for load google chart with database data --%>
  2. <script src="Scripts/jquery-1.7.1.js"></script>
  3. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  4.  
  5. <script>
  6. var chartData; // globar variable for hold chart data
  7. google.load("visualization", "1", { packages: ["corechart"] });
  8.  
  9. // Here We will fill chartData
  10.  
  11. $(document).ready(function () {
  12. $.ajax({
  13. url: "GoogleChart.aspx/GetChartData",
  14. data: "",
  15. dataType: "json",
  16. type: "POST",
  17. contentType: "application/json; chartset=utf-8",
  18. success: function (data) {
  19. chartData = data.d;
  20. },
  21. error: function () {
  22. alert("Error loading data! Please try again.");
  23. }
  24. }).done(function () {
  25. // after complete loading data
  26. google.setOnLoadCallback(drawChart);
  27. drawChart();
  28. });
  29. });
  30.  
  31.  
  32. function drawChart() {
  33. var data = google.visualization.arrayToDataTable(chartData);
  34.  
  35. var options = {
  36. title: "Company Revenue",
  37. pointSize: 5
  38. };
  39.  
  40. var barChart = new google.visualization.BarChart(document.getElementById('chart_div'));
  41. barChart.draw(data, options);
  42.  
  43. }
  44.  
  45. </script>

Step-7: Write server side code (function) for get data from sql server database.

Before writing code add this namespace to your code behind page. 

  1. using System.Web.Script.Services;
  2. using System.Web.Services;
Here is the function, which is called by jquery code.
  1. [WebMethod]
  2. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  3. public static object[] GetChartData()
  4. {
  5. List<GoogleChartData> data = new List<GoogleChartData>();
  6. //Here MyDatabaseEntities is our dbContext
  7. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  8. {
  9. data = dc.GoogleChartDatas.ToList();
  10. }
  11.  
  12. var chartData = new object[data.Count + 1];
  13. chartData[0] = new object[]{
  14. "Year",
  15. "Electronics",
  16. "Books & Media",
  17. "Home & Kitchen"
  18. };
  19.  
  20. int j = 0;
  21. foreach (var i in data)
  22. {
  23. j++;
  24. chartData[j] = new object[] {i.Year.ToString(), i.Electronics, i.BookAndMedia, i.HomeAndKitchen };
  25. }
  26. return chartData;
  27. }
Step-8: Run Application

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...