Subscribers

Wednesday, February 25, 2009

«Label Cloud Widget»

I've added a new widget at the bottom of the page. It's a label cloud; the larger and brighter the link, the more posts there are that have that label.

I saw the widget in use on a random blog I came across, and I really liked it. After a little searching, I found the code and where it came from. I'm using the version that can be found at Widget-based.

For the sake of preservation, I'm going to include the code for the widget here as well. There hasn't been a new post on the Widget-based blog since 2006 (only 8 total posts), and I'd hate for it to be deleted and this version of the code lost.

Installation in two parts, you'll want to Expand Widget Templates for this:

  1. CSS code. Goes anywhere between the tags <b:skin> ... </b:skin>, preferably right before the end tag.

    /* Label Cloud Styles
    ----------------------------------------------- */
    #labelCloud {text-align:center;font-family:arial,sans-serif;}
    #labelCloud .label-cloud li{display:inline;background-image:none !important;padding:0 5px;margin:0;vertical-align:baseline !important;border:0 !important;}
    #labelCloud ul{list-style-type:none;margin:0 auto;padding:0;}
    #labelCloud a img{border:0;display:inline;margin:0 0 0 3px;padding:0}
    #labelCloud a{text-decoration:none}
    #labelCloud a:hover{text-decoration:underline}
    #labelCloud li a{}
    #labelCloud .label-cloud {}
    #labelCloud .label-count {padding-left:0.2em;font-size:9px;color:#000}
    #labelCloud .label-cloud li:before{content:"" !important}

  2. Widget. Goes anywhere between <b:section id="'sidebar'"> ... </b:section> (or where ever you want it in your template).

    <b:widget id='Label1' locked='false' title='Label Cloud' type='Label'>
    <b:includable id='main'>
    <b:if cond='data:title'>
    <h2><data:title/></h2>
    </b:if>

    <div class='widget-content'>
    <div id='labelCloud'> <ul>
    <b:loop values='data:labels' var='label'>
    <li>
    <b:if cond='data:blog.url == data:label.url'>
    <data:label.name/>
    <b:else/>
    <a expr:href='data:label.url'><data:label.name/></a>
    </b:if>
    (<data:label.count/>)
    </li>
    </b:loop>
    </ul></div>
    <script type='text/javascript'>
    // Label Cloud User Variables
    var maxFontSize = 20;
    var maxColor = [168,181,248];
    var minFontSize = 8;
    var minColor = [0,0,0];
    var lcShowCount = false;
    var minPostCount = 0; //integer

    // Don't change anything past this point -----------------
    function s(a,b,i,x){
    if(a>b){
    var m=(a-b)/Math.log(x),v=a-Math.floor(Math.log(i)*m)
    }
    else{
    var m=(b-a)/Math.log(x),v=Math.floor(Math.log(i)*m+a)
    }
    return v
    }

    var ta=0;
    var c=[];
    var labelCount = new Array();
    var ts = new Object;
    <b:loop values='data:labels' var='label'>
    <b:if cond='data:label.count'>
    if (<data:label.count/> &gt; minPostCount) ts[&quot;<data:label.name/>&quot;] = <data:label.count/>;</b:if></b:loop>

    for (t in ts){
    if (!labelCount[ts[t]]){
    labelCount[ts[t]] = new Array(ts[t])
    }
    }
    tz = labelCount.length-1;
    lc2 = document.getElementById('labelCloud');
    lc2.innerHTML = '';
    ul = document.createElement('ul');
    ul.className = 'label-cloud';
    for(var t in ts){
    for (var i=0;3 &gt; i;i++) {
    c[i]=s(minColor[i],maxColor[i],ts[t]-ta,tz)
    }
    var fs = s(minFontSize,maxFontSize,ts[t]-ta,tz);
    li = document.createElement('li');
    li.style.fontSize = fs+'px';
    li.style.lineHeight = '1';
    a = document.createElement('a');
    a.title = ts[t]+' Posts in '+t;
    a.style.color = 'rgb('+c[0]+','+c[1]+','+c[2]+')';
    a.href = '/search/label/'+encodeURIComponent(t);
    if (lcShowCount){
    span = document.createElement('span');
    span.innerHTML = '('+ts[t]+') ';
    span.className = 'label-count';
    a.appendChild(document.createTextNode(t));
    li.appendChild(a);
    li.appendChild(span);
    }
    else {
    a.appendChild(document.createTextNode(t));
    li.appendChild(a);
    }
    ul.appendChild(li);
    abnk = document.createTextNode(' ');
    ul.appendChild(abnk);
    }
    lc2.appendChild(ul);
    </script>

    <b:include name='quickedit'/>
    </div>

    </b:includable>
    </b:widget>

This is the first blog post that uses my new code boxes. I'll still use the old style for short code snippets.

EDIT: I've decided to maintain consistency and use this style of code block for all code; even short snippets. Please tell me what you think of it.

3 comments:

  1. Well now, isn't that just convenient for you....

    So w/ every visit you force us all to lose the game.

    I see how you are.

    ReplyDelete
  2. Monique: Hey, you don't have to look down there... The Game just happens to be one of my labels.

    Although now that you point it out, it is an interesting side-effect.

    Oh, and I've noticed a small scrollbar glitch in IE... So I'm going to see if I can fix that.

    ReplyDelete
  3. After screwing around with it all morning yesterday, I finally got the code block to display the same in both IE and Firefox. It wasn't easy... That useless vertical scrollbar in IE was persistent. I also had some padding issues with the bottom scrollbar in IE.

    Anyway, it's all fixed now... I hope.

    ReplyDelete

Thanks for taking the time to comment.

Note: Only a member of this blog may post a comment.

»» «« »Home«