using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
//http://www.codeproject.com/csharp/julijanpiechart.asp
using System.Drawing.PieChart;
namespace VainWebSpider
{
#region frmPie CLASS
/// <summary>
/// Sets up the pie chart with the values that match
/// the data type the user selected "views", "votes",
/// "popularity", "ratings"
/// </summary>
public partial class frmPie : Form
{
#region Instance Fields
//instance fields
private DataGridView gridInUse;
private int CLUSTERED_THRESHOLD = 20;
#endregion
#region Contructor
/// <summary>
/// Constructs a new frmPie object
/// </summary>
public frmPie()
{
InitializeComponent();
}
#endregion
#region Public Properties
/// <summary>
/// Sets the <see cref="DataGridView">DataGridView</see> to use
/// </summary>
public DataGridView GridIsUse
{
set { gridInUse = value; }
}
/// <summary>
/// Sets the AuthorString to use
/// </summary>
public string AuthorString
{
set { lblCurrentUser.Text = value; }
}
#endregion
#region Private Methods
/// <summary>
/// Calls the populatePieData() method and sets up some
/// other miscellaneous pie chart values
/// </summary>
private void setupPie()
{
populatePieData();
pnlPie.Font = new Font("Arial", 8F);
pnlPie.ForeColor = SystemColors.WindowText;
pnlPie.EdgeColorType = EdgeColorType.DarkerThanSurface;
pnlPie.LeftMargin = 10F;
pnlPie.RightMargin = 10F;
pnlPie.TopMargin = 10F;
pnlPie.BottomMargin = 10F;
pnlPie.SliceRelativeHeight = 0.25F;
pnlPie.InitialAngle = -90F;
}
/// <summary>
/// Sets up the pie chart with the values that match
/// the data type the user selected "views", "votes",
/// "popularity", "ratings"
/// </summary>
private void populatePieData()
{
//Switch on the current data type
switch (cmbViewData.SelectedItem.ToString().ToLower())
{
//Views DataGridView column = 0
//Rating DataGridView column = 1
//Votes DataGridView column = 2
//Popularity DataGridView column = 3
//URL DataGridView column = 5
case "views" :
getGridData("views", 0);
break;
case "votes":
getGridData("votes", 2);
break;
case "popularity":
getGridData("popularity", 3);
break;
case "ratings":
getGridData("ratings", 1);
break;
default:
getGridData("views", 0);
break;
}
}
/// <summary>
/// Returns a single dimesion decimal array of data, extracted
/// from this forms gridInUse field, which is then used to display
/// on the embedded pie chart
/// </summary>
/// <param name="type">The type of columns "views", "votes",
/// "popularity", "ratings" </param>
/// <param name="column">Column number 0-3</param>
private void getGridData(string type, int column)
{
try
{
//setup some golding fields for the pie data
int qty = gridInUse.RowCount;
qty--; // drop TOTAL row
decimal[] results = new decimal[qty];
string[] pieToolTips = new string[qty];
string[] pieText = new string[qty];
float[] pieRelativeDisplacements = new float[qty];
Color[] pieColors = new Color[qty];
int alpha = 60;
Random rnd = new Random();
Color[] colorsAvailable = new Color[] { Color.FromArgb(alpha, Color.Red),
Color.FromArgb(alpha, Color.Green),
Color.FromArgb(alpha, Color.Yellow),
Color.FromArgb(alpha, Color.Blue),
Color.FromArgb(alpha, Color.CornflowerBlue),
Color.FromArgb(alpha, Color.Cyan),
Color.FromArgb(alpha, Color.DarkGreen),
Color.FromArgb(alpha, Color.PeachPuff),
Color.FromArgb(alpha, Color.Plum),
Color.FromArgb(alpha, Color.Peru) };
//loop through the grid and set up the pie chart to use the grids data
for (int i = 0; i < qty; i++)
{
//Views DataGridView column = 0
//Rating DataGridView column = 1
//Votes DataGridView column = 2
//Popularity DataGridView column = 3
//URL DataGridView column = 5
pieToolTips[i] = "URL " + gridInUse[5, i].Value.ToString() + " " +
"Views " + gridInUse[0, i].Value.ToString() + " " +
"Rating " + gridInUse[1, i].Value.ToString() + " " +
"Votes " + gridInUse[2, i].Value.ToString() + " " +
"Popularity " + gridInUse[3, i].Value.ToString();
if (type.Equals("ratings"))
{
string val = gridInUse[column, i].Value.ToString();
int idx = val.LastIndexOf("/");
string sNewValue = val.Substring(0, idx);
results[i] = decimal.Parse(sNewValue);
}
else
{
results[i] = decimal.Parse(gridInUse[column, i].Value.ToString());
}
//if there are loads of articles, we dont want any text on pie chunks
//as it becomes illegible
if (gridInUse.RowCount < CLUSTERED_THRESHOLD)
{
pieText[i] = gridInUse[column, i].Value.ToString();
}
else
{
pieText[i] = " ";
}
pieRelativeDisplacements[i] = 0.1F;
int idxColor = rnd.Next(0, colorsAvailable.Length - 1);
pieColors[i] = colorsAvailable[idxColor];
}
//update the pie components
pnlPie.ToolTips = pieToolTips;
pnlPie.Texts = pieText;
pnlPie.SliceRelativeDisplacements = pieRelativeDisplacements;
pnlPie.Colors = pieColors;
pnlPie.Values = results;
}
catch (Exception)
{
//Cant do much about it, but catch it all the same.
//just dont update pie chart if we get an Exception
}
}
/// <summary>
/// Selects the 1st index in the cmbViewData combobox and then
/// Calls the setupPie() method
/// </summary>
/// <param name="sender">frmPie</param>
/// <param name="e">EventArgs</param>
private void frmPie_Load(object sender, EventArgs e)
{
cmbViewData.SelectedIndex = 1;
setupPie();
}
/// <summary>
/// Calls the setupPie() method
/// </summary>
/// <param name="sender">cmbViewData</param>
/// <param name="e">EventArgs</param>
private void cmbViewData_SelectedValueChanged(object sender, EventArgs e)
{
setupPie();
}
#endregion
}
#endregion
}