-->

How to Implement Comparison Report with Bar Chart using ASP.NET.

Introduction

In this post, I explain how to Implement Comparison Report with Bar Chart using ASP.NET.
Visualization can be a very powerful and convincing instrument to present our data. One should always clearly understand what exactly we want to show. Bar charts is a great way to compare items side-by-side. Bar charts also handle multiple data series, for this features we can use this bar charts to compare Data.

Steps :

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 a table for Fetch Data.

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

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 >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a Webpage and Design for show Comparison Report with Bar Chart from Database Data.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code

  1. <h3>Sales Comparison Report with Chart using ASP.NET.</h3>
  2. <table width="100%">
  3. <tr>
  4. <td width="35%" valign="top">
  5. <asp:GridView ID="gvSales" runat="server" AutoGenerateColumns="false" Width="95%">
  6. <Columns>
  7. <asp:BoundField HeaderText="Year" DataField="Year" />
  8. <asp:BoundField HeaderText="Quarter 1" DataField="Quarter1" />
  9. <asp:BoundField HeaderText="Quarter 2" DataField="Quarter2" />
  10. <asp:BoundField HeaderText="Quarter 3" DataField="Quarter3" />
  11. <asp:BoundField HeaderText="Quarter 4" DataField="Quarter4" />
  12. </Columns>
  13. </asp:GridView>
  14. </td>
  15. <td width="50%" valign="top">
  16. </td>
  17. </tr>
  18. <tr>
  19. <td colspan="2">
  20. <asp:Chart ID="Chart1" runat="server" BorderlineWidth="0" Width="800px">
  21. <Series>
  22. <asp:Series Name="Series1" XValueMember="Year" YValueMembers="Quarter1"
  23. LegendText="Quarter 1" IsValueShownAsLabel="false" ChartArea="ChartArea1"
  24. MarkerBorderColor="#DBDBDB">
  25. </asp:Series>
  26.  
  27. <asp:Series Name="Series2" XValueMember="Year" YValueMembers="Quarter2"
  28. LegendText="Quarter 2" IsValueShownAsLabel="false" ChartArea="ChartArea1"
  29. MarkerBorderColor="#DBDBDB">
  30. </asp:Series>
  31.  
  32. <asp:Series Name="Series3" XValueMember="Year" YValueMembers="Quarter3"
  33. LegendText="Quarter 3" IsValueShownAsLabel="false" ChartArea="ChartArea1"
  34. MarkerBorderColor="#DBDBDB">
  35. </asp:Series>
  36.  
  37. <asp:Series Name="Series4" XValueMember="Year" YValueMembers="Quarter4"
  38. LegendText="Quarter 4" IsValueShownAsLabel="false" ChartArea="ChartArea1"
  39. MarkerBorderColor="#DBDBDB">
  40. </asp:Series>
  41. </Series>
  42. <Legends>
  43. <asp:Legend Title="Quarter" />
  44. </Legends>
  45. <Titles>
  46. <asp:Title Docking="Bottom" Text="Sales Report Quarterly" />
  47. </Titles>
  48. <ChartAreas>
  49. <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
  50. </ChartAreas>
  51. </asp:Chart>
  52. </td>
  53. </tr>
  54. </table>

Step-6: Write code in page_load event for fetch data from database and show in Gridview and Create Comparison Bar Chart.


  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. PopulateReport();
  6. }
  7. }
Here is the function...
  1. private void PopulateReport()
  2. {
  3. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  4. {
  5. var v = dc.SalesDatas.ToList();
  6.  
  7.  
  8. gvSales.DataSource = v;
  9. gvSales.DataBind();
  10.  
  11. Chart1.DataSource = v;
  12. Chart1.DataBind();
  13. }
  14. }

Step-7: Run Application.

Related Post :

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: