// Disclaimer and Copyright Information
// Analysis.aspx.cs :
//
// All rights reserved.
//
// Written by Pardesi Services, LLC
// Version 1.01
//
// Distribute freely, except: don't remove our name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc. to
// softomatix@pardesiservices.com
///////////////////////////////////////////////////////////////////////////////
//
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Drawing.Imaging;
using System.IO;
namespace ASPNet_App
{
/// <summary>
/// Summary description for Analysis.
/// </summary>
public class Analysis : System.Web.UI.Page
{
protected System.Web.UI.WebControls.CheckBox wndBrowserChk;
protected System.Web.UI.WebControls.Panel wndReportPanel;
protected System.Web.UI.WebControls.Button wndSubmitQueryBtn;
protected System.Web.UI.WebControls.RadioButtonList wndRadioButtonList;
protected ArrayList m_arrColors;
private void Page_Load(object sender, System.EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
Response.Cache.SetNoServerCaching();
if (IsPostBack)
{
wndReportPanel.Controls.Clear();
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RunAnalysisQuery(Object sender, EventArgs e)
{
// Check the state of various controls.
if (wndBrowserChk.Checked == true)
{
wndReportPanel.Controls.Clear();
// Get the browser information
this.GenerateBrowsersReport();
}
}
/// <summary>
///
/// </summary>
private void GenerateBrowsersReport()
{
XmlDocument browserLogDoc;
WebLogManager logMgr;
try
{
logMgr = new WebLogManager();
browserLogDoc = logMgr.GetBrowsersReport();
// MAke sure that returned document is valid.
if (browserLogDoc == null)
{
return;
}
AddBrowserReportToPanel(browserLogDoc);
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
}
/// <summary>
///
/// </summary>
/// <param name="doc"></param>
private void AddBrowserReportToPanel(XmlDocument doc)
{
// Get the option selected by the user..
switch (Convert.ToInt32(wndRadioButtonList.SelectedItem.Value))
{
case 1:
this.AddBrowserPieChartToPanel(doc);
break;
case 2:
this.AddBrowserBarGraphToPanel(doc);
break;
default:
return;
}
}
/// <summary>
///
/// </summary>
/// <param name="doc"></param>
private void AddBrowserPieChartToPanel(XmlDocument doc)
{
// Add the header to Pie Chart diagram.
/*
Label lbl = new Label();
lbl.Text = "Uer's Browser Analysis Chart";
lbl.Style["font-size"]="20px";
lbl.Style["font-weight"]="bold";
lbl.Style["text-decoration"]="underline";
lbl.Style["color"]= "#e94f40";
wndReportPanel.Controls.Add(lbl);
wndReportPanel.EnableViewState = false;
*/
this.AddTitleLabelForReport(doc);
string strChartFile="";
// Add the pie chart image.
if (this.CreatePieChart(doc, ref strChartFile))
{
Panel chartPanel = new Panel();
chartPanel.HorizontalAlign = HorizontalAlign.Center;
System.Web.UI.WebControls.Image chartImg = new System.Web.UI.WebControls.Image();
chartImg.EnableViewState = false;
if (Request.Browser.Browser.ToUpper().IndexOf("IE") != -1)
{
chartImg.ImageUrl = Server.MapPath(strChartFile);
}
else
{
chartImg.ImageUrl = "http://localhost" + Request.ApplicationPath + "/" + strChartFile;
}
chartImg.BorderWidth = 0;
chartPanel.Controls.Add(chartImg);
wndReportPanel.Controls.Add(chartPanel);
// Add the legends for the pie chart.
AddTheLegendsTable(doc);
}
wndReportPanel.HorizontalAlign = HorizontalAlign.Center;
wndReportPanel.Visible = true;
this.CreateBarGraph(doc, ref strChartFile);
}
/// <summary>
///
/// </summary>
/// <param name="doc"></param>
private void AddBrowserBarGraphToPanel(XmlDocument doc)
{
// Add the header to Pie Chart diagram.
this.AddTitleLabelForReport(doc);
string strChartFile="";
// Add the pie chart image.
if (this.CreateBarGraph(doc, ref strChartFile))
{
Panel chartPanel = new Panel();
chartPanel.HorizontalAlign = HorizontalAlign.Center;
System.Web.UI.WebControls.Image chartImg = new System.Web.UI.WebControls.Image();
chartImg.EnableViewState = false;
if (Request.Browser.Browser.ToUpper().IndexOf("IE") != -1)
{
chartImg.ImageUrl = Server.MapPath(strChartFile);
}
else
{
chartImg.ImageUrl = "http://localhost" + Request.ApplicationPath + "/" + strChartFile;
}
chartImg.BorderWidth = 0;
chartPanel.Controls.Add(chartImg);
wndReportPanel.Controls.Add(chartPanel);
// Add the legends for the pie chart.
AddTheLegendsTable(doc);
}
wndReportPanel.HorizontalAlign = HorizontalAlign.Center;
wndReportPanel.Visible = true;
}
/// <summary>
///
/// </summary>
/// <param name="doc"></param>
private bool CreatePieChart(XmlDocument doc, ref string chartFile)
{
// Get the count of nodes in the document. This number will be
// used to create array of colors and stuff.
int nBrowserTypes = 0;
XmlNode topNode = null;
try
{
topNode = doc.SelectSingleNode("//browsers");
if (topNode != null)
{
nBrowserTypes = topNode.ChildNodes.Count;
if (nBrowserTypes == 0)
{
Trace.Write("No browser log nodes found in the document");
return false;
}
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
if (!this.GenerateRandomColors(nBrowserTypes))
{
return false;
}
Bitmap chartBmp = null;
Rectangle bgRect;
Graphics chartGraphics = null;
try
{
// Create the background for pie chart.
bgRect = new Rectangle(0, 0, 100, 100);
// Create the bitmap object to draw our chart.
chartBmp = new Bitmap(100, 100);
// Create the Graphics object.
chartGraphics = Graphics.FromImage(chartBmp);
// Fill the background of our choice.
chartGraphics.FillRectangle(new SolidBrush(Color.FromArgb(0x78f0fefc)), 0, 0, 120, 120);
// Get the record count from the parent node attribute.
long lTotalRecords = -1;
try
{
lTotalRecords = Convert.ToUInt32(topNode.Attributes["count"].InnerText);
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
float currentDegree = 0.0F;
for (int idx = 0; idx < nBrowserTypes; idx++)
{
float val = (Convert.ToSingle(topNode.ChildNodes[idx].InnerText) / lTotalRecords) * 360;
if (val > 0.0)
{
chartGraphics.FillPie((SolidBrush) m_arrColors[idx], bgRect, currentDegree,
val);
// increment the currentDegree
currentDegree += Convert.ToSingle(topNode.ChildNodes[idx].InnerText) / lTotalRecords * 360;
}
}
chartFile = Server.MapPath("images/PieChart.jpg");
FileStream jpgFile = new FileStream(chartFile, FileMode.Create, FileAccess.ReadWrite);
// Save the graphics as Jpeg file.
chartBmp.Save(jpgFile, ImageFormat.Jpeg);
jpgFile.Close();
chartFile = "images/PieChart.jpg";
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
chartGraphics.Dispose();
chartBmp.Dispose();
return true;
}
/// <summary>
///
/// </summary>
/// <param name="doc"></param>
/// <returns></returns>
private bool AddTitleLabelForReport(XmlDocument doc)
{
// Add the header to Pie Chart diagram.
Label lbl = new Label();
lbl.Text = "Uer's Browser Analysis Chart";
lbl.Style["font-size"]="20px";
lbl.Style["font-weight"]="bold";
lbl.Style["text-decoration"]="underline";
lbl.Style["color"]= "#e94f40";
wndReportPanel.Controls.Add(lbl);
wndReportPanel.EnableViewState = false;
return true;
}
/// <summary>
///
/// </summary>
/// <param name="doc"></param>
private bool AddTheLegendsTable(XmlDocument doc)
{
int nBrowserTypes = 0;
XmlNode topNode = null;
try
{
topNode = doc.SelectSingleNode("//browsers");
if (topNode != null)
{
nBrowserTypes = topNode.ChildNodes.Count;
if (nBrowserTypes == 0)
{
Trace.Write("No browser log nodes found in the document");
return false;
}
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
// Get the record count from the parent node attribute.
long lTotalRecords = -1;
try
{
lTotalRecords = Convert.ToUInt32(topNode.Attributes["count"].InnerText);
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
try
{
Panel legendPanel = new Panel();
legendPanel.HorizontalAlign = HorizontalAlign.Center;
Panel totalRecPanel = new Panel();
totalRecPanel.HorizontalAlign = HorizontalAlign.Center;
totalRecPanel.Style["width"] = "25%";
Label lbToatalRecs = new Label();
lbToatalRecs.Style["font-weight"] = "bold";
lbToatalRecs.Style["font-size"] = "16px";
lbToatalRecs.Style["color"] = "#434ff8";
lbToatalRecs.Text = "Total Users = " + lTotalRecords.ToString();
legendPanel.Controls.Add(lbToatalRecs);
Table lgTable = new Table();
lgTable.Style["width"] = "25%";
lgTable.Style["border"] = "1px solid #434ff8";
lgTable.CellPadding = 1;
lgTable.CellSpacing = 1;
for (int idx = 0; idx < nBrowserTypes; idx++)
{
TableRow tbRow = new TableRow();
// Create and add the color legend coloum.
TableCell tbCellColor = new TableCell();
tbCellColor.Width=20;
tbCellColor.BackColor = ((SolidBrush)this.m_arrColors[idx]).Color;
tbRow.Cells.Add(tbCellColor);
// Create and add the name cell.
TableCell tbCellName = new TableCell();
tbCellName.Width=70;
tbCellName.Style["font-weight"] = "bold";
tbCellName.Style["font-size"] = "14px";
tbCellName.Style["color"] = "#434f78";
Label lbName = new Label();
lbName.Text = topNode.ChildNodes[idx].Name.ToUpper();
tbCellName.Controls.Add(lbName);
tbRow.Cells.Add(tbCellName);
decimal val = (Decimal)(Convert.ToSingle(topNode.ChildNodes[idx].InnerText) / lTotalRecords) * 100;
val = Decimal.Round(val, 1);
// Create and add the numbers cell.
TableCell tbCellNumber = new TableCell();
tbCellNumber.Style["font-weight"] = "bold";
tbCellNumber.Style["font-size"] = "14px";
tbCellNumber.Style["color"] = "#034f78";
Label lbNumber = new Label();
lbNumber.Text = val.ToString() + "%";
tbCellNumber.Controls.Add(lbNumber);
tbRow.Cells.Add(tbCellNumber);
// Add the row to table now.
lgTable.Rows.Add(tbRow);
}
// Add the table to panel.
legendPanel.Controls.Add(lgTable);
// Add the panel to main panel.
wndReportPanel.Controls.Add(legendPanel);
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
return true;
}
/// <summary>
///
/// </summary>
/// <param name="doc"></param>
/// <returns></returns>
private bool CreateBarGraph(XmlDocument doc, ref string chartFile)
{
// Get the count of nodes in the document. This number will be
// used to create array of colors and stuff.
int nBrowserTypes = 0;
XmlNode topNode = null;
try
{
topNode = doc.SelectSingleNode("//browsers");
if (topNode != null)
{
nBrowserTypes = topNode.ChildNodes.Count;
if (nBrowserTypes == 0)
{
Trace.Write("No browser log nodes found in the document");
return false;
}
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
if (!this.GenerateRandomColors(nBrowserTypes))
{
return false;
}
Bitmap chartBmp = null;
Rectangle bgRect;
Graphics chartGraphics = null;
try
{
// Create the background for pie chart.
bgRect = new Rectangle(0, 0, 100, 100);
// Create the bitmap object to draw our chart.
chartBmp = new Bitmap(120, 120);
// Create the Graphics object.
chartGraphics = Graphics.FromImage(chartBmp);
// Fill the background of our choice.
chartGraphics.FillRectangle(new SolidBrush(Color.FromArgb(0x78f0fefc)), 0, 0, 120, 120);
// Draw a border for the bar graph.
Pen borderPen = new Pen (new SolidBrush(Color.CornflowerBlue));
chartGraphics.DrawRectangle(borderPen, 10, 10, 100, 100);
// Get the record count from the parent node attribute.
long lTotalRecords = -1;
try
{
lTotalRecords = Convert.ToUInt32(topNode.Attributes["count"].InnerText);
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
int curX = 10;
int nGap = 2;
int nWidth = (100/nBrowserTypes) - nGap;
int nHeight = 0;
int x = 0;
int y = 0;
for (int idx = 0; idx < nBrowserTypes; idx++)
{
x = curX + nGap;
nHeight = (Int32)((Convert.ToDecimal(topNode.ChildNodes[idx].InnerText)/lTotalRecords)*100);
y = (100 - nHeight) + 10;
chartGraphics.FillRectangle((SolidBrush) m_arrColors[idx], x, y, nWidth, nHeight);
// increment the current x position;
curX = x + nWidth;
}
// Draw the tickers for graph.
DrawTickersForBarGraph(chartGraphics,
100,
100,
10,
1,
false,
Color.Blue,
Color.AliceBlue);
chartFile = Server.MapPath("images/BarGraph.jpg");
FileStream jpgFile = new FileStream(chartFile, FileMode.Create, FileAccess.ReadWrite);
// Save the graphics as Jpeg file.
chartBmp.Save(jpgFile, ImageFormat.Jpeg);
jpgFile.Close();
chartFile = "images/BarGraph.jpg";
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
chartGraphics.Dispose();
chartBmp.Dispose();
return true;
}
/// <summary>
///
/// </summary>
/// <param name="obGraph"></param>
/// <param name="nHeight"></param>
/// <param name="nWidth"></param>
/// <param name="nGrain"></param>
/// <param name="nTickerSize"></param>
/// <param name="bPrintText"></param>
/// <param name="clrTicker"></param>
/// <param name="clrText"></param>
private void DrawTickersForBarGraph(Graphics obGraph,
int nHeight,
int nWidth,
int nGrain,
int nTickerSize,
bool bPrintText,
Color clrTicker,
Color clrText)
{
// Sanity checks..
if (nHeight == 0 ||
nGrain == 0)
{
Trace.Write("Invalid height or granularity sepcified for tickers");
return;
}
// Also make sure that ticker size if not more than width of the
// graphics itself.
if (nTickerSize > nWidth)
{
Trace.Write("Ticker size is more than width of graphics");
return;
}
Pen obPen = new Pen(new SolidBrush(clrTicker));
Point p1 = new Point(10 - nTickerSize, 0);
Point p2 = new Point(9 + nTickerSize, 0);
int nY = 10;
int nOffset = 20;
for (int nIdx = 0; nIdx <= nHeight; nIdx = nIdx + nGrain)
{
p1.Y = nHeight - nY + nOffset;
p2.Y = nHeight - nY + nOffset;
obGraph.DrawLine(obPen, p1, p2);
// Increment the Y corodinate.
nY += nGrain;
}
}
/// <summary>
///
/// </summary>
/// <param name="doc"></param>
private bool GenerateRandomColors(int nSize)
{
if (m_arrColors != null)
{
m_arrColors.Clear();
}
Random rnd = new Random();
try
{
m_arrColors = new ArrayList();
for (int i = 0; i < nSize; i++)
{
m_arrColors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return false;
}
return true;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}