Tool Tag Cloud

This is a fun one, create a tag cloud from a list of words, which you can also use as a menu.

There are 9 weights which correspond to the 9 font weights available, although not all fonts provide 9 weights. The one I've used as default has 5.

Customizable as much as possible, you can add colors for each weight, set a minimum count to achieve before an item is shown, and quite a bit more.

The actual cloud is created in HTML on a webview.

SLTagCloud
Author:
Steve Laming
Version: 0.9

  • Methods:
    • Add(Word As String) As String
      Add a word to the word list

      AddAll(Words As List) As String
      Add all the words in List to the word list

      AddFont(Weight As String, FontFile As String) As String
      Add/Replace a user font, the files should be added to the Files TAB in the IDE
      It is possible to specify fonts with weights of: 100, 200, 300, 400 (Normal), 500, 600, 700 (Bold), 800, 900
      If you clear the fonts then the default browser font is used for weights that you don't specify, it is better to double up if you
      don't have a font for all weights. you can also specify a minimum and maximum weight to use via the
      MinimumWeight and MaximumWeight methods. If you don't clear the fonts then the lato font will be used for any you don't specify.
      TagCloud.AddFont(100,"mythinfont")


      Background As Object
      Get or Set the WebView background to one of ColorDrawable OR GradientDrawable
      Will return false setting an Object that is not one of the above


      Clear As String
      Clear the word list

      ClearFont As String
      Clear the current fonts in use
      If you clear the fonts and don't specify new user fonts with AddFont then the default browser font will be used.


      Create As String
      Create the TagCloud

      Draw As String
      Draw the tagCloud

      getSize As int
      Get the current size of the word list

      getTCView As WebView
      Returns the WebView Object that is used to display the TagCloud

      Initialize(mPnl As Panel, Module As Object, EventName As String) As String
      Initializes the object.
      Requires a panel on which to put the tagcloud and an event name For callback when an item is selected.
      Will return a callback to {EventName}_SelectedItem(Item As String) if it exists in the calling module.


      IsInitialized As boolean
      Tests whether the object has been initialized.

      setBackgroundImage(Image As Bitmap) As String - [Write Only]
      Sets the background drawable for the TagCloud

      setColor(Color As int) As String - [Write Only]
      Set the background color for the TagCloud

      setFontSizeRatio(Ratio As float) As String - [Write Only]
      If the data set has a small range, you can increase the relative difference in font sizes

      setLog(TL As boolean) As String - [Write Only]
      Send generated HTML to the Log

      setMaximumWeight(Weight As int) As String - [Write Only]
      Set the maximum weight to use from range: 100,200,300,400,500,600,700,800,900
      Set before calling Create


      setMaxWords(NoWords As int) As String - [Write Only]
      Set the Maximum number of words to appear on one line

      setMinFontSize(MinSize As int) As String - [Write Only]
      Set the minimum start font size, Default is 20

      setMinimumWeight(Weight As int) As String - [Write Only]
      Set the minimum weight to use from range: 100,200,300,400,500,600,700,800,900
      Set before calling Create


      setMinimumWordCount(Count As int) As String - [Write Only]
      Don't display words with a count of less than count.
      Set before calling Create


      setShowCount(Show As boolean) As String - [Write Only]
      Show or hide the word count. Hidden by default
      Default font size is 14, you can change this with setShowCountFontSize


      setShowCountFontSize(px As int) As String - [Write Only]
      If Showcount is requested, you can set a font size. Default is 14px

      setTop(Top As int) As String - [Write Only]
      Set the top position of the view in the provided panel

      setWeightColors(Color As int()) As boolean - [Write Only]
      Set colors for each of the 9 available weights. Returns true if successful
      If you specify a single word color after setting this, weight colors will be ignored.
      If you do not specify enough colors then False will be returned, if you specify
      too many it will be logged and the surplus ignored.


      setWordColor(Color As int) As String - [Write Only]
      Set a single wordcolor. If you also specify weight colors after setting this, it will be ignored.

      Style As String
      Get or Set additional css styling for the words

      Visible As boolean
      Get or Set the TagCloud's Visibility


      Depends on:
      javaobject, SLColorlut V1.01

The attached demo is quite large as it has a 10,000+ word list that it builds the cloud from.

When you create a new project with Tagcloud, you'll need to load the fonts into your files tab.

This is definitely a beta library. Copy the jar and xml files to your addl libs folder.

Currrent Version is 0.92

If you are using the V3 Beta3+ please download SLTagCloud-3Beta3+.zip, otherwise download SLTagCloudLibs.zip.
 

Attachments

  • TagCloud.zip
    268.9 KB · Views: 280
  • SLTagCloudLibs.zip
    9.1 KB · Views: 346
  • SLTagCloud-3Beta3+.zip
    9 KB · Views: 344
  • TagCloud092-Source.zip
    268.3 KB · Views: 204
Last edited:

stevel05

Expert
Licensed User
Longtime User
Thanks NJDude, not quite that pretty yet, but functional. :)
 

stevel05

Expert
Licensed User
Longtime User
Bug fix, where word count was not clearing correctly in all circumstances. V0.92 attached to first post.
 

stevel05

Expert
Licensed User
Longtime User
New version compiled for B4a V3 Beta3
 

moster67

Expert
Licensed User
Longtime User
All sorted now.

Nice....I will need to fiddle a bit with this to adapt the output to my needs. This could be great in one of my apps.

Thanks!
 

moster67

Expert
Licensed User
Longtime User
Steve,

any chance you could add an extra, hidden, field for each item/word being loaded and which will not be displayed. It could be useful if I have some related information to the word. Imagine a word with a related URL. You click on the word and then in the "SelectedItem"-event, I go the web-page specified by the URL.

Of course, I could once I know the item selected, look up the related URL perhaps stored in another Map but what suggested above could make things easier at least for the programmer (me) using the library although perhaps not for the library-developer (you) :)
 

stevel05

Expert
Licensed User
Longtime User
That would not really stack up with the main design concept which is that you can fill the tagcloud with unknown data and it will display the cloud.

It could be done as an option, but you would have to provide a Map of known data and data to be linked which the library could look up and return as a tag. By the time you've coded the Map and read the tag in the return subroutine, you might as well manage it yourself. It would require the same amount of coding effort on your part either way.
 

carloz

Member
Licensed User
Longtime User
Hi,

I am getting following error while compiling TCLibTest.b4a

A referenced library is missing: jcore
 

stevel05

Expert
Licensed User
Longtime User
What version of B4a are you using?
 

stevel05

Expert
Licensed User
Longtime User
I don't recognise jcore as a required library for tagcloud, it's actually the name of the core library for B4j, do you have any other libraries enabled in the app?

For the test app, you should only need, Core, SLColorLUT and SLTagCloud checked in the libs tab.
 

mr23

Active Member
Licensed User
Longtime User
Hi Steve, are you still maintaining this library? I'm trying it out in an app.
I'm wondering if you could provide an option to specify the count for a word. My app is already tracking the count. Perhaps .Add2(Word As String, Count as Int) and .AddAll2(Words As List, Counts as List) ? Could you also add a .Remove(Word As String) ?
Also, I found if I give the cloud an empty set, it gives an exception during the create/draw.
B4X:
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
It then removes the 'webpage' and the webview displays an error.
Upon adding a word back in, then it is back to normal.
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Hi @mr23 ,

I have attached the source for the TagCloud Library (TagCloud092-Source.zip) to the first post, which is written in B4a. You can amend it as you wish.

The count is currently calculated, so you will need to create new variables to hold your counts and display them instead of the calculated ones.

Steve
 

mr23

Active Member
Licensed User
Longtime User
Here is my changes. I extracted 'WordFrequency', used for Word counting, from TC. Added some test buttons to the layout, and a few other minor changes.
I use several WordFrequency objects, swap in the one desired as needed, create, draw, etc. Could be optimized further, I'll repost if I do any more work on it.
 

Attachments

  • TagCloud095-Source.zip
    270.7 KB · Views: 169
Top