Click here to Skip to main content
15,895,667 members
Articles / Desktop Programming / Windows Forms

Word Cloud (Tag Cloud) Generator Control for .NET Windows.Forms in C#

Rate me:
Please Sign up or sign in to vote.
4.91/5 (47 votes)
30 Jul 2011CPOL3 min read 218.5K   13.1K   126  
Generate a word cloud form some input text. A word cloud is a randomly arranged set of words used in your text. The size and the color of each word expresses its usage frequency. Rarely used words are small and pale. The control is clickable and allows to identify a word under mouse.
using System.Drawing;

namespace Gma.CodeCloud.Controls.Geometry
{
    public class TypewriterLayout : BaseLayout
    {
        public TypewriterLayout(SizeF size) : base(size)
        {
            m_Carret = new PointF(size.Width, 0);
        }

        private PointF m_Carret;
        private float m_LineHeight;
 
        public override bool TryFindFreeRectangle(SizeF size, out RectangleF foundRectangle)
        {
            foundRectangle = new RectangleF(m_Carret, size);
            if (HorizontalOverflow(foundRectangle))
            {
                foundRectangle = LineFeed(foundRectangle);
                if (!IsInsideSurface(foundRectangle))
                {
                    return false;
                }
            }
            m_Carret = new PointF(foundRectangle.Right, foundRectangle.Y);
            
            return true;
        }

        private RectangleF LineFeed(RectangleF rectangle)
        {
            RectangleF result = new RectangleF(new PointF(0, m_Carret.Y + m_LineHeight), rectangle.Size);
            m_LineHeight = rectangle.Height;
            return result;
        }

        private bool HorizontalOverflow(RectangleF rectangle)
        {
            return rectangle.Right > Surface.Right;
        }
    }
}

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
Germany Germany
Tweeter: @gmamaladze
Google+: gmamaladze
Blog: gmamaladze.wordpress.com

Comments and Discussions