Click here to Skip to main content
15,891,204 members
Articles / Web Development / HTML

A Really Vain "How are my articles doing" Web Spider

Rate me:
Please Sign up or sign in to vote.
4.56/5 (43 votes)
4 Feb 2013CPOL6 min read 92K   984   74  
A simple web spider to see fetch CodeProject articles.
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
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions