A Sales Dashboard Application in ASP.Net

Introduction

In this article, we tackle a common data visualization task; creating a sales dashboard. A sales dashboard is widely used in business presentations, to outline key performance indicators for a given business process or objective. Key to any such presentation is the good visualization of the data, as well as the polished appearance. To do this, I am using related chart components, that offer all of the required functionality. The sample uses ASP.NET chart from ShieldUI, that is freely available from their site.

The finished presentation looks like this:

sales-dashboard-1.jpg

Using the code

To start off, I create a new Visual Studio project for the web. The web application needs to contain a single .aspx file, that will host the related controls. The second step is to include the DLL files for the chart components to the project: 

sales-dashboard-2.jpg

Once we have added the DLLs for the component we will use, we need to reference it in the project.

This can be done directly in the .aspx page, containing our dashboard sample:   

  1. <%@ Register Assembly="Shield.Web.UI" Namespace="Shield.Web.UI" TagPrefix="shield" %>  

Then, since this control is a server-side wrapper of a client JavaScript component, we also need to include references to the base JavaScript chart. This is also done in the .aspx file:

  1. <head>  
  2.     <link rel="stylesheet" type="text/css" href="shield-chart.1.2.2-Trial/shield-chart.1.2.2-Trial/css/shield-chart.min.css" />  
  3.     <script src="shield-chart.1.2.2-Trial/shield-chart.1.2.2-Trial/js/jquery-1.9.1.min.js" type="text/javascript"></script>  
  4.     <script src="shield-chart.1.2.2-Trial/shield-chart.1.2.2-Trial/js/shield-chart.all.min.js" type="text/javascript"></script>  
  5. </head>
The next phase of the project is to transform the requirements into code. The requirements specify that we need to have two or more related charts. In our case, we have a control to host the quarters, as well as a second chart to display data related to each quarter and one more chart to relate to the second one. In this manner, we can have subdivision by quarter and product-line; a common data visualization scenario for a sales dashboard. Following the outline above, we add the first chart to the .aspx file. Its declaration looks like this:

  1. <asp:updatepanel id="UpdatePanel2" runat="server" updatemode="Conditional" childrenastriggers="false">    
  2.        <ContentTemplate>    
  3.            <shield:shieldchart id="ShieldChart1" runat="server" autopostback="true" onselectionchanged="ShieldChart1_SelectionChanged"    
  4.                width="320px" height="330px" ontakedatasource="ShieldChart1_TakeDataSource">    
  5.                <PrimaryHeader Text="Quarterly Sales">    
  6.                </PrimaryHeader>    
  7.                <ExportOptions AllowExportToImage="false" AllowPrint="false" />    
  8.                <TooltipSettings CustomPointText="Sales Volume: <b>{point.y}</b>">    
  9.                </TooltipSettings>    
  10.                <Axes>    
  11.                    <shield:ChartAxisX CategoricalValuesField="Quarter">    
  12.                    </shield:ChartAxisX>    
  13.                    <shield:ChartAxisY>    
  14.                        <Title Text="Quarter Overview"></Title>    
  15.                    </shield:ChartAxisY>    
  16.                    </Axes>    
  17.                    <DataSeries>    
  18.                        <shield:ChartBarSeries DataFieldY="Sales">    
  19.                            <Settings EnablePointSelection="true" EnableAnimation="true">    
  20.                                <DataPointText BorderWidth="">    
  21.                                </DataPointText>    
  22.                            </Settings>    
  23.                        </shield:ChartBarSeries>    
  24.                    </DataSeries>    
  25.                    <Legend Align="Center" BorderWidth=""></Legend>    
  26.             </shield:shieldchart>    
  27.        </ContentTemplate>    
  28. </asp:updatepanel>  

It is wrapped in an update panel, to provide smooth visual updates.

In order to populate the chart control with data, we use the TakeDataSource event handler, in the code-behind of the .aspx file:

  1. protected void ShieldChart1_TakeDataSource(object sender, Shield.Web.UI.ChartTakeDataSourceEventArgs e)  
  2. {  
  3.     ShieldChart1.DataSource =  new object[]  
  4.     {  
  5.         new {Quarter = "Q1", Sales = 312 },  
  6.         new {Quarter = "Q2", Sales = 212 },  
  7.         new {Quarter = "Q3", Sales = 322 },  
  8.         new {Quarter = "Q4", Sales = 128 }  
  9.     };  
  10. }
The DataSource property of the control tells the chart what data will be passed to it for visualization. Here we pass a simple object array with a sales entry for each quarter. This will represent the chart with the quarterly data. The second, related chart, also added in the .aspx file, looks like this:

  1. <div id="container2" style="width: 490px; height: 340px; margin: auto; top: 5px;  
  2.     left: 5px; position: inherit;">  
  3.     <asp:updatepanel id="UpdatePanel1" runat="server" updatemode="Conditional" childrenastriggers="false">  
  4.             <ContentTemplate>  
  5.                 <shield:shieldchart id="ShieldChart2" ontakedatasource="ShieldChart2_TakeDataSource"  
  6.                     autopostback="true" onselectionchanged="ShieldChart2_SelectionChanged" runat="server"  
  7.                     width="463px" height="331px">  
  8. <ExportOptions AllowExportToImage="false" AllowPrint="false" />  
  9. <PrimaryHeader Text="Select a Quarter to show products sales">  
  10. </PrimaryHeader>  
  11. <Axes>  
  12. <shield:ChartAxisY>  
  13. <Title Text="Break-Down for selected quarter"></Title>  
  14. </shield:ChartAxisY>  
  15. </Axes>  
  16. <DataSeries>  
  17. <shield:ChartDonutSeries EnableValueXSorting="false" CollectionAlias="Q Data" DataFieldY="Data" DataFieldX="Product">  
  18. <Settings EnablePointSelection="true" EnableAnimation="true" AddToLegend="true">  
  19. <DataPointText BorderWidth="">  
  20. </DataPointText>  
  21. </Settings>  
  22. </shield:ChartDonutSeries>  
  23. </DataSeries>  
  24. <Legend Align="Center" BorderWidth=""></Legend>  
  25. </shield:shieldchart>  
  26.             </ContentTemplate>  
  27.             <Triggers>  
  28.                 <asp:AsyncPostBackTrigger ControlID="ShieldChart1" EventName="SelectionChanged" />  
  29.             </Triggers>  
  30.         </asp:updatepanel>  
  31. </div>
Both the first and the second charts have a selection event handler enabled, in order to allow the re-creation of the related control, since they both have a sub-chart attached to them. The flow of the project allows the end user to select a quarter from among the four quarters visualized in "ShieldChart1". Then, for this quarter, all available data is displayed in a donut chart, that is hosted in "ShieldChart2". The user can then select a specific category from the donut, which in turn populates the third chart. Its declaration is listed below:

  1. <div id="container3_box" style="width: 925px; height: 300px; border: 2px solid #40B3DF;  
  2.     margin: auto; top: 420px; left: 25px; position: absolute;">  
  3.     <div id="container3" style="width: 890px; height: 290px; margin: auto; top: 5px;  
  4.         left: 5px; position: inherit;">  
  5.         <asp:updatepanel id="UpdatePanel3" runat="server" updatemode="Conditional">  
  6. <ContentTemplate>  
  7. <shield:ShieldChart ID="ShieldChart3" runat="server" OnTakeDataSource="ShieldChart3_TakeDataSource"  
  8. Width="905px" Height="280px">  
  9. <DataSeries>  
  10. <shield:ChartLineSeries DataFieldY="QuarterSales">  
  11. <Settings AddToLegend="false">  
  12. <DataPointText BorderWidth="">  
  13. </DataPointText>  
  14. </Settings>  
  15. </shield:ChartLineSeries>  
  16. </DataSeries>  
  17. <PrimaryHeader Text="Select a product to show sales details...">  
  18. </PrimaryHeader>  
  19. <ExportOptions AllowExportToImage="false" AllowPrint="false" />  
  20. <Legend Align="Center" BorderWidth=""></Legend>  
  21. </shield:ShieldChart>  
  22. </ContentTemplate>  
  23. <Triggers>  
  24. <asp:AsyncPostBackTrigger ControlID="ShieldChart2" EventName="SelectionChanged" />  
  25. </Triggers>  
  26. </asp:updatepanel>  
  27.     </div>  
  28. </div>
One additional step needs to be taken, in order to allow the re-creation of the sub-charts. This is done on the server, once the selection of the charts is triggered. This cancels out the need to write any client-side code. The server side code looks like this: 

  1. protected void ShieldChart1_SelectionChanged(object sender, ChartSelectionChangedEventArgs e)  
  2. {  
  3.     if (e.Selected)  
  4.     {  
  5.         SelectedQuarter = e.Name;  
  6.         DataPerformance = GetPerformanceData().Where(d => d.Quarter == SelectedQuarter).ToList();  
  7.     }  
  8.     else  
  9.     {  
  10.         DataPerformance = null;  
  11.     }  
  12.     ShieldChart2.DataBind();  
  13. }  
  14. protected void ShieldChart2_SelectionChanged(object sender, ChartSelectionChangedEventArgs e)  
  15. {  
  16.     if (e.Selected)  
  17.     {  
  18.         SalesData = GetSalesDataProducts().Where(s => s.Quarter == SelectedQuarter && s.Product == e.Item.ValueX.ToString()).ToList();  
  19.     }  
  20.     else  
  21.     {  
  22.         SalesData = null;  
  23.     }  
  24.     ShieldChart3.DataBind();  
  25. }

This completes our setup, and the sales dashboard is ready for use. Feel free to download the code sample on the top of the article and explore the code for further reference.


Similar Articles