Text Formatters

Overview

Sometimes it might be necessary to display any text with the points on a chart for some reasons. That's when you need to use the format() method.

String Tokens

String tokens are special string values you can use in text formatters instead of using formatting functions described below. They are suitable when you need only basic formatting, but they cover most of the cases.

Here is how tokens can be used in tooltips, series labels or axes labels:

anychart.onDocumentReady(function() {
  // chart type
  var chart = anychart.column([
  {x: 'January', value: -10, season:"Winter"},
  {x: 'February', value: -8, season:"Winter"},
  {x: 'March', value: -4, season:"Spring"},
  {x: 'April', value: 1, season:"Spring"},  
  {x: 'May', value: 7, season:"Spring"},  
  {x: 'June', value: 12, season:"Summer"}
    ]);

  // set tooltip text template
  var tooltip = chart.getSeries(0).tooltip();
  tooltip.title().text("Content");
  tooltip.format("{%x} is a {%season} month\nLowest temp: {%Value}°C");

  // set series labels text template
  var seriesLabels = chart.getSeries(0).labels().enabled(true);
  seriesLabels.format("{%x}");
  
  // format axis labels
  var axisLabels = chart.xAxis().labels();
  axisLabels.useHtml(true);
  axisLabels.format("<b style='color:black;'>{%Value}</b>");

  // draw
  chart.container("container");
  chart.draw();
});

A live sample of chart tooltip, labels and axes labels formatted using string tokens:

Playground

Tokens List

Here is a list of the tokens you can use in formatting strings. Note that some tokens don't work universally: you can't use {%BubbleSize} outside of Bubble series or {%Close} outside of Candlestick or OHLC and so on.

The full list of tokens is available in API: anychart.enums.Statistics

Token Description
{%Average}Average value.
{%Value}The y value of this point.
{%YValue}The y value of this point.
{%YPercentOfSeries}The percentage of the series this point represents.
{%YPercentOfTotal}The percentage of all the series on the chart this point represents.
{%XValue}The x value of this point (Scatter Plot charts).
{%BubbleSize}The bubble size value of this point (Bubble chart).
{%BubbleSizePercentOfCategory}The percentage of all the points with the same name this point represents (Categorized charts).
{%BubbleSizePercentOfSeries}The percentage of the series this point represents.
{%BubbleSizePercentOfTotal}The percentage of all the series on the chart this point represents.
{%Index}The index of this point in the series this point represents (zero-based).
{%SeriesName}The name of this series.
{%SeriesYSum}The sum of all the points y values.
{%SeriesYAverage}The average y value of all the points within this series.
{%SeriesPointCount}The number of points in this series.
{%SeriesBubbleMaxSize}The maximal bubble size value of all the points within this series (Bubble chart).
{%SeriesBubbleMinSize}The minimal bubble size value of all the points within this series (Bubble chart).
{%SeriesBubbleSizeAverage}The average bubble size value of all the points within this series (Bubble chart).
{%SeriesBubbleSizeMedian}The median bubble size value of all the points within this series (Bubble chart).
{%SeriesBubbleSizeMode}The mode bubble size value of all the points within this series (Bubble chart).
{%SeriesBubbleSizeSum}The sum of all the points bubble sizes (Bubble chart).
{%SeriesFirstXValue}The x value of the first point in this series (Scatter plot charts).
{%SeriesFirstYValue}The y value of the first point in this series.
{%SeriesLastXValue}The x value of the last point in this series (Scatter plot charts).
{%SeriesLastYValue}The y value of the first point in this series.
{%SeriesXAverage}The average x value of all the points within this series.
{%SeriesXAxisName}The title text of the X Axis.
{%SeriesXMax}The maximal x value of all the elements within this series (Scatter plot charts).
{%SeriesXMedian}The median x value of all the points within this series (Scatter plot charts).
{%SeriesXMin}The minimal x value of all the elements within this series (Scatter plot charts).
{%SeriesXMode}The mode x value of all the points within this series (Scatter plot charts).
{%SeriesXSum}The sum of all the points x values (Scatter plot charts).
{%SeriesYAxisName}The title text of the Y Axis.
{%Name}The name of this point.
{%High}The high value of this point (OHLC, Candlestick).
{%Low}The low value of this point (OHLC, Candlestick).
{%Open}The open value of this point (OHLC, Candlestick).
{%Close}The close value of this point (OHLC, Candlestick).
{%SeriesYMax}The maximal y value of all the elements within this series.
{%SeriesYMin}The minimal y value of all the elements within this series.
{%SeriesYMedian}The median y value of all the points within this series.
{%SeriesYMode}The mode y value of all the points within this series.
{%RangeStart}The starting value of this point (Range charts).
{%RangeEnd}The ending value of this point (Range charts).
{%SeriesYRangeMax}The maximal range in this series (Range charts).
{%SeriesYRangeMin}The minimal range in this series (Range charts).
{%SeriesYRangeSum}The sum of all ranges in this series (Range charts).
{%Range}The range of this point (RangeEnd - RangeStart).
{%DataPlotYSum}The sum of all the points y values.
{%DataPlotYMax}The maximal of all the points y values.
{%DataPlotYMin}The minimal of all the points y values.
{%DataPlotYAverage}The average y value of all the points.
{%DataPlotPointCount}The number of the points within the chart.
{%DataPlotBubbleMaxSize}The maximal of all the points bubble sizes (Bubble chart).
{%DataPlotBubbleMinSize}The minimal of all the points bubble sizes (Bubble chart).
{%DataPlotBubbleSizeAverage}The average bubble size of all the points (Scatter plot charts).
{%DataPlotBubbleSizeSum}The sum of all the points bubble sizes (Bubble chart).
{%DataPlotSeriesCount}The number of the series within the chart.
{%DataPlotXAverage}The average x value of all the points (Scatter plot charts).
{%DataPlotXMax}The maximal of all the points x values (Scatter plot chart).
{%DataPlotXMin}The minimal of all the points x values (Scatter plot chart).
{%DataPlotXSum}The sum of all the points x values (Scatter plot charts).
{%DataPlotYRangeMax}The maximal of the ranges of the points within the chart.
{%DataPlotYRangeMin}The minimal of the ranges of the points within the chart.
{%DataPlotYRangeSum}The sum of the ranges of the points within the chart.
{%AxisName}The name of the axis.
{%AxisScaleMax}The maximum value on the axis.
{%AxisScaleMin}The minimum value on the axis.
{%CategoryName}The name of the category.
{%CategoryYAverage}The name of the category.
{%CategoryYMedian}The median of all the points within this category.
{%CategoryYMode}The mode of all the points within this category.
{%CategoryYPercentOfTotal}The percent of all the data on the chart this category represents.
{%CategoryYRangeAverage}The average range in this category (Range charts).
{%CategoryYRangeMax}The maximal range in this category (Range charts).
{%CategoryYRangeMedian}The median range in this category (Range charts).
{%CategoryYRangeMin}The minimal range in this category (Range charts).
{%CategoryYRangeMode}The mode range in this category (Range charts).
{%CategoryYRangePercentOfTotal}Category Y range percent of total (Range charts).
{%CategoryYRangeSum}The sum of all ranges in this category (Range charts).
{%CategoryYSum}The sum of all the points within this category.
{%DataPlotMaxYSumSeriesName}The name of the series with a maximal sum of the points y values.
{%DataPlotMaxYValuePointName}The name of the point with a maximal of all the points y values.
{%DataPlotMaxYValuePointSeriesName}The name of the series with a maximal of all the points y values.
{%DataPlotMinYSumSeriesName}The name of the series with a minimal sum of the points y values.
{%DataPlotMinYValuePointName}The name of the point with a minimal of all the points y values.
{%DataPlotMinYValuePointSeriesName}The name of the series with a minimal of all the points y values.
{%XPercentOfSeries}The percentage of the series this point represents (Scatter Plot charts).
{%XPercentOfTotal}The percentage of all the series on the chart this point represents.
{%YPercentOfCategory}The percentage of all the points with the same name this point represents.

Formatting Parameters

Along with usage of special tokens which help to define the source of text, you can set options which help to format numeric values in the text. Options follow the token in curly brackets.

Here is a simple code with tokens:

// setting the tooltips with formatting
var columnTooltip = columnSeries.tooltip();
columnTooltip.format("{%SeriesName}: {%Value}{groupsSeparator:', decimalsCount:3}");

var lineTooltip = lineSeries.tooltip();
lineTooltip.format("{%SeriesName}: {%Value}{decimalsCount:1}%");

Here is a chart with a tooltip configured using tokens with options:

Playground

Formatting Parameters List

There is a list of formatting parameters, which help to organize your data presentation in the way you prefer. You can find a sample after the table that list formatting parameters.

Option Type Description
decimalsCountnumericThe number of visible decimal characters (including characters for the integer values).
decimalPointbooleanSets a character for separating decimal part of a number.
groupsSeparatorstringSets a character for separating thousands of an integer number.
useBracketsForNegativeuseNegativeSignbooleanControls the "-" sign.
zeroFillDecimalsbooleanHides or displays decimal characters for integer values.
scaleSets value scaling.

In the next sample we have formatted the scale according to the Old British currency system (before 1971), when £1 was equivalent to 20 shillings and 1 shilling = 12 pence.

var columnTooltip = columnSeries.tooltip();

// scaling value
columnTooltip.format("{%SeriesName}: {%Value}{scale:(1)(12)(20)|( p)( s)( £)}");

Here is a live sample with such settings:

Playground

If you explore the sample, you can see that all values are set in pence, but the shown value is formatted.

On the sample below there is another popular case of scale formatting shown: a thousandfold increase.

// string for formatting tooltips
var formatter = "{%SeriesName}: {%Value}{scale:(1)(1000)(1000)(1000)|( d)( th)( M)( B)}";

Here is a live sample with such settings:

Playground

Escaping symbols

If you want to use a symbol which is already reserved in token's parser, you need to use double slash (\) before this symbol to prevent it from been parsed.

var tooltip = series.tooltip();

// use coma as the thousands separator
tooltip.format("{%Value}{groupsSeparator:\\,}");

Here is a live sample with such settings:

Playground

Formatting functions

For complex formatting use formatting function instead of token strings. Formatting functions are set like this:

// formatting using a function
var lineTooltip = lineSeries.tooltip();
lineTooltip.format(function(){
  return "Income: " + this.value/100 + "%";
});

Here is a live sample with such settings:

Playground

Note: you can use anychart.format.number(), anychart.format.dateTime() and other members of anychart.format namespace to format values in formatting functions:

var currentLabels = chart.labels();
// format the number
currentLabels.format(function() {
    return anychart.format.number(this.value, 3, ".", ",")
});

Here is a sample where anychart.format.number() is used to present label in a desired way:

Playground

Default fields

There are some standard fields available in formatters depending on a chart type. Below you can see a table with all chart types and fields available for them by default.

Chart type(s)Default fields of series.labels().format()
Area
Bar
Column
Error
Marker
Percent Stacked Area
Percent Spline Area
Percent StepLine Area
Percent Stacked Bar
Percent Stacked Column
Polar
Radar
Sparkline
Stacked Line
Stacked Spline
x
value
seriesName
index
Range Line
Range Spline Area
Range Column
Range Bar
x
seriesName
index
high
low
Scatter x
seriesName
index
value
valueLowerError
valueUpperError
xLowerError
xUpperError
Bubble x
value
seriesName
size
Box x
seriesName
index
lowest
q1
median
q3
highest
outliers
Japanese Candlestick
Open-High-Low-Close
x
open
high
low
close
seriesName
index
Pie/Donut
Funnel
Pyramid
Note! As those types have an only series by default,
you should use the format() method with chart.label().
x
value
index

First of all, enable the labels. Then set the fields of values you want those labels to show using the format() function according to the table above.

//set data series
var series_1 = chart.bar(Sales2003);
var series_2 = chart.bar(Sales2004);

//set series name
series_1.name("Winter");
series_2.name("Summer");

var labels_winter = series_winter.labels();
labels_winter.enabled(true);
labels_winter.format(function(){
    return(this.seriesName + ": $" + this.value);
});

var labels_summer = series_summer.labels();
labels_summer.enabled(true);
labels_summer.format(function(){
    return(this.seriesName + ": $" + this.value);
});

Playground

Extra fields

The number and variety of default fields might be not enough in some cases. Sometimes it's necessary to show some extra information. In this case you should use one of the following methods: getStat(), getData() or getMeta(). Which one to use depends on the unique situation.

getData

Using these methods, you can display the values from the extra parameters, if you have added any to the series or to the data. Look at the sample and its code below:

// create box chart series with our data
var series_1 = chart.box(data_1);
var series_2 = chart.box(data_2);

// enable the labels
var labels = series_2.labels();
labels.enabled(true);

// use format
labels.format(function(){
    return(this.getData("extra_inf"));
});

Playground

In this sample we have added some extra information to the data: we defined the "extra_inf" parameter of "redundant" value for the second point of the second series and displayed it, using getData().

Managing additional information for chart tooltips works pretty much the same as it does for chart labels. Define extra parameter in your data set and use the name of your parameter as a value for getData() method.

// map data for series
var seriesData = dataSet.mapAs({
    // set data row for x parameter
    x: [0],
    // set data row for value parameter
    value: [1],
    // set data row for custom parameter
    yoy: [2]
});

// set data for series
var series = chart.column(seriesData);
series.name("Unique users in 2013");

// series tooltip settings
var tooltip = series.tooltip();

// adjust tooltip text
tooltip.format(function(){
    return 
        this.seriesName + ": " + this.value + " millions" +
        "\nYear over year: " + this.getData("yoy") + "%";
});

Here is a sample with additional information in the chart tooltip. Full information on tooltip settings can be found in Tooltip article.

Playground

Series Meta

You can add extra parameters not only to the data points but to series too. Let's add an extra parameter as to the series of OHLC chart and show it with format.

To add any parameter to the meta of the series, you need to set the parameter name first and then its value, which can be of any type.

// set first series data

var series_1 = chart.ohlc([
    {x: Date.UTC(2007, 7, 28), open: 511.53, high: 514.98, low: 505.79, close :506.40},
    {x: Date.UTC(2007, 7, 30), open: 517.36, high: 518.40, low: 516.58, close: 516.80},
    {x: Date.UTC(2007, 8, 1), open: 513.10, high: 516.50, low: 511.47, close: 515.25},
]);
series_1.meta("company", "ACME Corp.");
 
// set second series data

var series_2 = chart.ohlc([
    {x: Date.UTC(2007, 7, 28), open: 522.95, high: 523.10, low: 522.50, close: 522.52},
    {x: Date.UTC(2007, 7, 30), open: 524.49, high: 524.91, low: 524.38, close: 524.61},
    {x: Date.UTC(2007, 8, 1), open: 518.81, high: 520.03, low: 517.51, close: 519.73}
]);
series_2.meta("company", "Duff B. Corp.");

// format

var labels_1 = series_1.labels();
labels_1.format(function(){
    return("C: "+this.series.meta("company")+"\nL: "+this.low+"\nH: "+this.high);
});

var labels_2 = series_2.labels();
labels_2.format(function(){
    return("C: "+this.series.meta("company")+"\nL: "+this.low+"\nH: "+this.high);
});

Playground

getStat

This method is to be used when you want to obtain various statistical information from a chart. Read more about this in Statistics article.

Here is a sample of the getStat() method used in a Pie chart.

// format
var labels = chart.labels();
labels.format(function(){
    return((this.getData("value"))+"(of "+this.getStat("sum")) + ")";
});

Playground

You are looking at an outdated v7 version of this document. Switch to the v8 version to see the up to date information.