Tag Cloud

Overview

A tag cloud, also known as a word cloud or weighted list, is a visual representation of text data. This chart is typically used to show keyword metadata (tags) on websites, or to visualize free form text. Tags are usually single words, and the importance of each tag, which is often based on its frequency, is shown with font size or color.

This article explains how to create a basic Tag Cloud chart as well as configure settings that are specific to the type. You can also see the table below to get a brief overview of the Tag Cloud's characteristics:

API
Classanychart.charts.TagCloud
DATA
Data Fieldsx, value, category
Multiple SeriesN/A
OPTIONS
StackedN/A
VerticalN/A
3DN/A
Error BarsN/A
SUPPORTED CHART PLOTS
PolarN/A
RadarN/A
ScatterN/A
StockN/A
RELATED TYPES
N/A
SEE ALSO
Chartopedia: Venn Diagram
General Settings

Quick Start

To create a Tag Cloud, use the anychart.tagCloud() chart constructor, like in the following sample:

var data = [
    {x: "learning", value: 80},
    {x: "includes", value: 56},
    {x: "lists", value: 44},
    {x: "meaning", value: 40},
    {x: "useful", value: 36},
    {x: "different", value: 32}
];

// create a chart and set the data
chart = anychart.tagCloud(data);

// set the container id
chart.container("container");

// initiate drawing the chart
chart.draw();

Playground

General Settings

In AnyChart there are many settings that are configured in the same way for all chart types, including the Tag Cloud chart (for example, legend settings).

Read the overview of general settings: General Settings.

Special Settings

Data

Data for a Tag Cloud can be passed to the chart constructor anychart.tagCloud() or to the data() method.

There are two ways to create data: you can add either a list of words or a text.

When you add a list of words (or other elements), you have to specify their frequencies. Use the following data fields:

  • x to set words
  • value to set frequencies
  • category to set categories

This is how working with x and value looks like:

// create data
var data = [
    {"x": "learning", "value": 80},
    {"x": "includes", "value": 56},
    {"x": "lists", "value": 44},
    {"x": "meaning", "value": 40},
    {"x": "useful", "value": 36},
    {"x": "different", "value": 32},
    {"x": "grammar", "value": 28},
    {"x": "teaching", "value": 24},
    {"x": "example", "value": 20},
    {"x": "thing", "value": 12},
];

// create a chart and set the data
chart = anychart.tagCloud(data);

Playground

The category field is optional: it causes elements to be colored according to the categories they belong to. This field also affects the legend and can be used with the ordinal color scale. Here is a sample showing how to add categories and how the chart looks after that:

// create data   
var data = [
    {x: "learning", value: 80, category: "noun"},
    {x: "includes", value: 56, category: "verb"},
    {x: "lists", value: 44, category: "noun"},
    {x: "meaning", value: 40, category: "noun"},
    {x: "useful", value: 36, category: "adjective"},
    {x: "different", value: 32, category: "adjective"},
    {x: "grammar", value: 28, category: "noun"},
    {x: "teaching", value: 24, category: "noun"},
    {x: "example", value: 20, category: "noun"},
    {x: "thing", value: 12, category: "noun"}
];

// create a chart and set the data
chart = anychart.tagCloud(data);

Playground

When you add just a text, the next step is to parse it into elements and calculate their frequencies. For parsing, use the data() method with settings listed in anychart.data.TextParsingSettings:

  • cutLength cuts the length of words
  • ignoreItems adds a list of ignored elements
  • ignoreTrailingSpaces ignores trailing spaces
  • maxItems sets the number of the most frequent elements to be displayed
  • maxLength ignores elements longer than a certain value
  • minLength ignores elements shorter than a certain value
  • mode sets the parsing mode

The parsing mode, unlike other settings, must be always specified. There are two parsing modes:

  • byWord for parsing text into words
  • byChar for parsing text into characters

If the only thing you want to set is the mode, you can use a shortcut from anychart.anychart.enums.TextParsingMode:

chart.data("Tyger, tyger, burning bright", "by-char");

The following sample shows how to work with parsing settings. It is a Tag Cloud with the 16 most frequent words in William Blake's poem "The Tyger", some function words ignored:

// create data
var text =
    "Tyger, tyger, burning bright " +
    "In the forests of the night, " +
    "What immortal hand or eye " +
    "Could frame thy fearful symmetry? ";

// set the parsing mode and configure parsing settings
chart.data(text, {
        mode: "byWord",
        maxItems: 16,
        ignoreItems: [
                     "the",
                     "and",
                     "he",
                     "or",
                     "of",
                     "in",
                     "thy"
        ]
});

Playground

Appearance

The appearance settings of a Tag Cloud can be configured in three states: normal, hover, and selected. Use the normal(), hovered(), and selected() methods.

Combine them with the following methods:

Also, you can use some other methods from anychart.core.StateSettings.

Note: If settings are specified only for the normal state, they are inherited by the hover and selected states. The only exception is the fill: the selected state does not inherit its settings (but the hover state does).

In this sample, there is a Tag Cloud with appearance settings configured:

// configure the visual settings of the chart
chart.normal().fill("#1fadad");
chart.hovered().fill("#93bfec");
chart.selected().fill("#1f66ad");
chart.normal().fontWeight(600);

Playground

Color Scale

By default, elements are colored in the colors of the default palette. You can also create a color scale, linear or ordinal, and add a color range.

Linear

To create a linear color scale, use the linearColor() constructor.

Then call colors() to set two colors: the second one is applied to the most frequent element, and the first one indicates 0. Elements of frequencies that lie within this range are colored automatically in different mixtures of these two colors, and if you do not specify them, the default colors of the linear color scale are used.

To set your scale as the color scale of the chart, use the colorScale() method.

Optionally, you can use colorRange() to enable a color range – a special interactive element representing the color scale. With the linear color scale, it looks like a gradient from the first to the second color.

The colorLineSize() allows you to customize the size of the color scale (20 by default). See other settings: anychart.core.ui.ColorRange.

The following sample shows a Tag Cloud with a linear color scale and a color range:

// create and configure a color scale.
var customColorScale = anychart.scales.linearColor();
customColorScale.colors(["#ffcc00", "#00ccff"]);

// set the color scale as the color scale of the chart
chart.colorScale(customColorScale);

// add a color range
chart.colorRange().enabled(true);

Playground

Ordinal

To create an ordinal color scale, you should use the ordinalColor() constructor.

Combine it with ranges() to set frequency ranges (two or more) you want to be marked by different colors. Then you can set a color for each of these ranges by using the colors() method. Please note that if you do not specify colors and ranges, the default settings of the ordinal color scale are used.

Finally, call colorScale() to set your scale as the color scale of the chart, and colorRange() to add a color range. With the ordinal color scale, the color range shows the ranges and their colors.

In this sample, there is a Tag Cloud with an ordinal color scale and a color range:

// create and configure a color scale.
var customColorScale = anychart.scales.ordinalColor();
customColorScale.ranges([
    {less: 50},
    {from: 50, to: 60},
    {greater: 60}
]);
customColorScale.colors(["lightgray", "#ffcc00", "#00ccff"]);

// set the color scale as the color scale of the chart
chart.colorScale(customColorScale);

// add a color range
chart.colorRange().enabled(true);

Playground

Categories

Instead of frequency ranges, the ordinal color scale and the color range can indicate the categories of data. Add the category field to your data to set categories, then specify colors for the scale:

// create data   
var data = [
    {x: "learning", value: 80, category: "noun"},
    {x: "includes", value: 56, category: "verb"},
    {x: "lists", value: 44, category: "noun"},
    {x: "meaning", value: 40, category: "noun"},
    {x: "useful", value: 36, category: "adjective"},
    {x: "different", value: 32, category: "adjective"},
    {x: "grammar", value: 28, category: "noun"},
    {x: "teaching", value: 24, category: "noun"},
    {x: "example", value: 20, category: "noun"},
    {x: "thing", value: 12, category: "noun"}
];

// create and configure a color scale.
var customColorScale = anychart.scales.ordinalColor();
customColorScale.colors(["#00b8e6", "#e6b800", "#ff4d4d"]);

// set the color scale as the color scale of the chart
chart.colorScale(customColorScale);

// add a color range
chart.colorRange().enabled(true);

Playground

Angles

Every element on a Tag Cloud chart is rotated by a certain angle. To configure these angles, use the following methods:

You can specify the set of angles to be used in your chart by passing an array of angles to angles():

// configure angles
chart.angles([0, 30, 90]);

Playground

There is also another way to configure angles. Call fromAngle() and toAngle() to set the first and the last angle in a range, then call anglesCount() to set the total number of angles. The defaults are: 0°, 90°, and 2.

In this sample the number of angles is 5, the first angle is 10°, the last is 100°, and 3 angles lying between them are calculated automatically:

// configure angles
chart.fromAngle(10);
chart.toAngle(100);
chart.anglesCount(5);

Playground

Text Spacing

The textSpacing() method allows you to change the spacing between elements of your Tag Cloud chart. The default value is 1.

In the sample below the spacing is set to 15:

// set text spacing
chart.textSpacing(15);

Playground

Mode

There are two modes of positioning elements on a Tag Cloud: spiral (default) and rectangle. To set the mode, use the mode() method with either "spiral" or "rect" as a parameter – see anychart.enums.TagCloudMode:

// set the mode of the tag cloud
chart.mode("rect");

Playground

Scales

To learn about scales in general, see the Scales section.

If frequencies of elements cover a large range, the font size of the least frequent elements tends to be too small. You can prevent this situation with the help of the logarithmic scale.

Use the log() method to create a logarithmic scale and scale() to set it as the value scale of your chart:

/* create a logarithmic scale and set it
as the value scale of the chart */
tagCloud2.scale(anychart.scales.log());

Playground

Tooltips

A Tooltip is a text box displayed when a point on a chart is hovered. There is a number of visual and other settings available: for example, you can edit the text by using font settings and text formatters, change the style of background, adjust the position of a tooltip, and so on.

Tokens

In the case of Tag Clouds, you need to know that the {%value} token returns the frequency of an element, and {%yPercentOfTotal} returns the percentage frequency. By default, both are shown. To change the text of tooltips, use tokens with the format() method, combined with tooltip():

// configure tooltips
chart.tooltip().format("{%yPercentOfTotal}% ({%value})");

Playground

Formatting Functions

You can also configure tooltips by using formatting functions instead of tokens. For example, the function in the sample below modifies the format of tooltips depending on percentage frequencies of elements:

// enable HTML for tooltips
chart.tooltip().useHtml(true);

// configure tooltips
chart.tooltip().format(function (){
  var percentOfTotal = (this.getData("value")*100)/this.getStat("sum");
  if (percentOfTotal < 7)
    return percentOfTotal.toFixed(1) +  "%";
  if (percentOfTotal > 15)
    return "<span style='font-size:26'>" +
           percentOfTotal.toFixed(1) +  "%</span>";
  return "<span style='font-size:18'>" +
         percentOfTotal.toFixed(1) +  "%</span>";
});

Playground

Interactivity

You might want to link elements of a Tag Cloud to web pages. In this case, use the listen() method to add an event listener to your chart. Specify the event type you want to trigger the action and the action itself (opening a web page).

For example, here clicking on a word leads to its page opening in Wiktionary:

// add an event listener
chart.listen("pointClick", function(e){
  var url = "//en.wiktionary.org/wiki/" + e.point.get("x");
  window.open(url, "_blank");
});

Playground