I need to add a keyword cloud in my asp net web pages! Can anyone suggest me the best way to do it? I got a few working samples implemented using C# but I want to know if there are other possibilities to do that! An开发者_如何学Goy good suggestion would be really appreciated! Thanks in advance :)
you can try using this code..
Int32 rank, minVal, maxVal, scale, s;
minVal = st.GetMinRank();
maxVal = st.GetMaxRank();
scale = (maxVal - minVal) / 6;
rank = Convert.ToInt32(yourRankValue that is stored in your DB);
if (scale == 0)
s = 0;
else
s = (rank - minVal) / scale;
if (s == 0)
{
lblSTName.Font.Size = FontUnit.Smaller;
}
else if (s == 1)
{
lblSTName.Font.Size = FontUnit.Small;
}
else if (s == 2)
{
lblSTName.Font.Size = FontUnit.Medium;
}
else if (s == 3)
{
lblSTName.Font.Size = FontUnit.Large;
}
else if (s == 4)
{
lblSTName.Font.Size = FontUnit.Larger;
}
else if (s >= 5)
{
lblSTName.Font.Size = FontUnit.Larger;
lblSTName.Font.Bold = true;
}
I guess to some extent, the answer depends on how and where your keyword data is stored.
I have a database with the following tables:
So Photos are linked to Tags through the TagsPhotos table.
I then used the following methods to generate my keyword cloud:
protected void Page_Load(object sender, EventArgs e) {
// "zhpCtx" is a LINQ to SQL data context.
// "TagCloud" is a Panel control.
// Select a list of tags and a count of how often they appear in TagsPhotos
var tagDensity = from t in zhpCtx.siteContent_Tags
join tp in zhpCtx.siteContent_TagsPhotos
on t.TagID equals tp.TagID
group t by new { t.TagID, t.TagText } into g
where g.Count() > 0
orderby g.Key.TagText
select new { g.Key.TagID, g.Key.TagText, NumTags = g.Count() };
if (null != tagDensity && tagDensity.Count() > 0) {
// We have some tags, get the lowest number of counts greater than 0, and
// the highest to give our range.
int? min = tagDensity.Min(t => t.NumTags);
int? max = tagDensity.Max(t => t.NumTags);
// Loop through each tag
foreach (var tag in tagDensity) {
// Create a new HyperLink control to take the user to a given tag
// Include the tag count in the list for accessibility.
// Could probably move it to the title attribute.
// Build the link however suits.
HyperLink tagLink = new HyperLink {
NavigateUrl = string.Format("Photos.aspx?TagID={0}", tag.TagID),
Text = string.Format("{0} ({1})", tag.TagText, tag.NumTags) };
// Adjust the font-size of the tag link - calling getPercentage.
// This will adjust the size based on the baseline font size for the
// container
tagLink.Style.Add("font-size",
string.Format("{0}%",
getPercentageSize((int)max, (int)min, tag.NumTags)));
// Add the HyperLink to the Panel.
TagCloud.Controls.Add(tagLink);
// Add a LiteralControl with a comma and a space.
TagCloud.Controls.Add(new LiteralControl(", "));
}
// Remove the last separator control from the Panel.
TagCloud.Controls.RemoveAt(TagCloud.Controls.Count - 1);
} else {
// Hide the tag cloud panel
TagCloud.Visible = false;
}
}
private int getPercentageSize(int max, int min, int score) {
// Ensure we've got a sensible number
if (min < 1) {
min = 1;
}
double spread = max - min;
if (spread == 0) {
spread = 1;
}
// Setup sensible bounds for the font sizes
int minSize = 80;
int maxSize = 200;
// Step ensures that our least used keyword is 80% and our
// most used is 200%
double step = (maxSize - minSize) / spread;
return (int)(minSize + ((score - min) * step));
}
精彩评论