Legend: Series Labels

Overview

Legend in AnyChart Stock Component is rather complex, but flexible element. It is used not only to show color code for chart series, but also to show a lot of additional information.

In this article we describe so-called series and technical indicators "Legend Items". Legend Item is a specially formatted string which defines how series or technical indicator looks like in legend in various situations.

to top

Configuration Options

AnyChart Stock XML provide a lot of options to configure legend items: you can control any series or indicator item individually or define defaults to keep XML small and avoid mistakes.

This section tells you where you can configure legend items

Individual settings for any series

If you need to define personal settings for series representation in legend - you need to configure legend item in given series.

No matter what series type is the XML is the same. For example:

XML/JSON Syntax
Plain code
01 <series type="Line" data_provider="dp2" color="#0066DD">
02   <name><![CDATA[ORCL]]></name>
03   <legend_item>
04     <labels>
05       <focus_settings>
06         <mouse_over>
07           <format><![CDATA[{%SeriesIcon} {%SeriesName}: {%Value.Current}]]></format>
08         </mouse_over>
09         <mouse_out>
10           <format><![CDATA[{%SeriesIcon} {%SeriesName}: {%Value.LastVisible}]]></format>
11         </mouse_out>
12       </focus_settings>
13     </labels>
14   </legend_item>
15 </series>
01{
02  type: "Line",
03  dataProvider: "dp2",
04  color: "#0066DD",
05  name: "ORCL",
06  legendItem: {
07    labels: {
08      focusSettings: {
09        mouseOver: {
10          format: "{%SeriesIcon} {%SeriesName}: {%Value.Current}"
11        },
12        mouseOut: {
13          format: "{%SeriesIcon} {%SeriesName}: {%Value.LastVisible}"
14        }
15      }
16    }
17  }
18}

As you can see legend options are configured in <legend_item> node.

Note: in this article we use mostly individual settings, but you can use the same settings in default because the syntax is identical.

to top

Defaults for series types

In case is you use several series of the same type in a chart, these series may have similar settings for representation in the legend. There is an option to define these settings in one place.

For example you have several Lines and you want to show value changes in legend. Sample XML below shows how to set legend defaults for all line series:

XML/JSON Syntax
Plain code
01 <chart>
03     <line_series>
04       <legend_item>
05         <labels>
06           <focus_settings>
07             <mouse_over>
08               <format><![CDATA[{%SeriesIcon} {%SeriesName}: {%ValueChange.Current}]]></format>
09             </mouse_over>
10             <mouse_out>
11               <format><![CDATA[{%SeriesIcon} {%SeriesName}: {%ValueChange.LastVisible}]]></format>
12             </mouse_out>
13           </focus_settings>
14         </labels>
15       </legend_item>
16     </line_series>
18 </chart>
01{
03    lineSeries: {
04      legendItem: {
05        labels: {
06          focusSettings: {
07            mouseOver: {
08              format: "{%SeriesIcon} {%SeriesName}: {%ValueChange.Current}"
09            },
10            mouseOut: {
11              format: "{%SeriesIcon} {%SeriesName}: {%ValueChange.LastVisible}"
12            }
13          }
14        }
15      }
16    }
17  }
18}

As you can see the legend item is set in <line_series> - subnode of <series_settings>.

<series_settings> node contains defaults for several series types, which are functionally similar: Line, Spline, StepLine and Stick. Table below lists this dependence:

Series Type Node
Line, Spline, StepLine, Stick <line_series>
Area, SplineArea, StepLineArea <area_series>
Bar <bar_series>
RangeArea, RangeSplineArea <range_area_series>
RangeBar <range_bar_series>
Candlestick <candlestick_series>
OHLC <ohlc_series>
Marker <marker_series>

Live sample shows use of <line_series> defaults for Lines:

Live Sample:  Series Legend Label Default Settings

to top

Text and Font Formatting

Formatting string is very important for it defines what information is shown in the legend for this or that series or indicator. AnyChart Stock provides very versatile way to configure this string, including HTML, special tokens and formatting options.

States

When you configure legend item you need to define two formats for different situations - States:

You can define two completely different formatting strings for each state. On Mouse Over you can show detailed information - with icon, series name, current value, percent change and so on, and on Mouse Out you can show only icon and series name - to give viewer the idea of what is displayed on the chart.

The main difference between these states is the fact that on Mouse Over you can use "Current" token and its properties. See more about that in Token Properties section .

Sample XML to define format for different states:

XML/JSON Syntax
Plain code
01 <labels>
02   <focus_settings>
03     <mouse_out>
04       <format><![CDATA[Mouse Out: {%SeriesName} {%SeriesIcon} {%Value.LastVisible}]]></format>
05     </mouse_out>
06     <mouse_over>
07       <format><![CDATA[Mouse Over: {%SeriesName} {%SeriesIcon} {%Value.Current}]]></format>
08     </mouse_over>
09   </focus_settings>
10 </labels>
01{
02  focusSettings: {
03    mouseOut: {
04      format: "Mouse Out: {%SeriesName} {%SeriesIcon} {%Value.LastVisible}"
05    },
06    mouseOver: {
07      format: "Mouse Over: {%SeriesName} {%SeriesIcon} {%Value.Current}"
08    }
09  }
10}

As you can see format is set in <format> node using CDATA section. The formatting string can contain either static text and/or special tokens. See more in Text Formatting and HTML Tags.

Besides formatting strings <labels> node contains some other settings.

to top

Tokens

To show values of series and technical indicator points you can use special tokens that are replaced by actual values when chart is shown. There are a lot of tokens in AnyChart Stock and you can use them with static text or alone.

Illustration below shows the legend and highlights sample tokens used to create this legend:

Any token can be used in any part of the formatting string along with another tokens or the static text. Sample XML with {%Value} token:

XML/JSON Syntax
Plain code
01 <labels>
02   <focus_settings>
03     <mouse_out>
04       <format><![CDATA[Value: {%Value.LastVisible}]]></format>
05     </mouse_out>
06     <mouse_over>
07       <format><![CDATA[Value: {%Value.Current}]]></format>
08     </mouse_over>
09   </focus_settings>
10 </labels>
01{
02  focusSettings: {
03    mouseOut: {
04      format: "Value: {%Value.LastVisible}"
05    },
06    mouseOver: {
07      format: "Value: {%Value.Current}"
08    }
09  }
10}

As you can see token has properties (Current and LastVisible) that are retrieved using dot notation and different properties are shown in different states. Learn how to use tokens in the next article.

Table below list all tokens:

Token Name Type Description
{%SeriesName} String Series name.
{%SeriesIcon} Shape Series icon.
{%Date} DateTime Date.
{%ChangeIcon} Shape Changes icon.
{%Value} Number "Value" field value.
{%ValueChange} Number Value change (in comparison mode).
{%PercentValueChange} Number Percent change (in comparison mode).
{%Open} Number "Open" field value.
{%High} Number "High" field value.
{%Low} Number "Low" field value.
{%Close} Number "Close" field value.
{%Volume} Number "Volume" field value.

Tokens in legend describe only type of the value to be inserted, but this information is not enough to acquire the value. Depending on the state and purpose you should specify one of the properties.

Section below describes how to use properties.

to top

Tokens Properties

Tokens Properties return certain value associated with the token. Some of the tokens are values themselves, but most of them - not. Table below list all tokens and their properties:

Token Group Available Properties
{%SeriesName}
{%SeriesIcon}
There are no properties for these tokens.
{%Value}
{%ValueChange}
{%PercentValueChange}
{%Open}
{%High}
{%Low}
{%Close}
{%Volume}
{%ChangeIcon} *
{%CustomField} **
"%ChangeColor" ***
First
Last
FirstVisible
LastVisible
Min
Max
MinVisible
MaxVisible
Current
{%Date} First
Last
FirstVisible
LastVisible
Current

* - {%ChangeIcon} is replaced by the icon that illustrates whether value grows or decreases, but as any numeric tokens it should know what point should be analyzed. Use of this token is described in details in Using Falling/Rising Icons.

** - {%CustomField} denotes any custom field that can be defined in data provider used by series. If you need some additional information to be shown in legend - you can use custom fields to store them and custom tokens to display them. This option is described in details in Using Custom Fields of Data-Provider.

*** - "%ChangeColor" token is used when you color text using HTML formatting. Using this token you can color the part of the text in Green (growth) or Red (decrease) to show the change direction. And should define what value should be used. Small sample:

<format><![CDATA[Change: <font color="%ChangeColor.Current"></font>]]></format>

You can see that %ChangeColor token is used in color attribute in <font> HTML tag. More detailed description can be found in Using Color Tokens.

Table below lists all available properties:

Property Name Description
First Value of the field from the first point in a series.
Last Value of the field from the last point in a series.
FirstVisible Value of the field from the first visible point in a series.
LastVisible Value of the field from the last visible point in a series.
Min Minimal value of the field in a series.
Max Maximal value of the field in a series.
MinVisible Minimal value of the field in the visible range of a series.
MaxVisible Maximal value of the field in the visible range of a series.
Current Value of the field from the point with the hovered date. If series doesn't have a point with the hovered date property returns empty string.

Illustration below shows what properties mean basing on the sample of {%Value} token for Line series. You can see the chart in the state when user zoomed to some range and all series data is not shown on the screen. Blue regions on the left and on the right are imaginable - user can't see the first and the last point, but these values can be shown in the legend using properties:

Properties of Tokens

Illustration shows the properties of {%Value} token, but the model is applicable for all other tokens with the sample properties.

Live sample below shows usage of all properties of {%Value} token in legend item of the single series. Zoom, drag and scroll the chart in the sample to see properties changes:

Live Sample:  Legend - Token Properties

to top

Tokens formatting

All tokens that return date time or numerical values can be formatted.

1. Numerical tokens. All tokens below return numerical values:

You can define how numbers returned using these tokens are displayed: what is used as decimal or thousands separator, etc. If, for example, you want to show {%Value.Current} with plus ("+") sign when value is positive - you should write it as {%Value.Current}{plusSign:true}

This works for any tokens in different elements in AnyChart Stock. Learn more in Numbers Formatting article.

2. Date time token {%Date}. It is rather rare situation when you need to put date into the legend item, but you can do that using {%Date} token.

Date token can be formatted too - {%Date}{"%yyyy-%MM-%dd %hh:%mm %tt"}.

This formatting options are applicable to all date time tokens in AnyChart Stock. Learn more in Date/Time Formatting.

Here is a live sample with two Volume+MA technical indicators, the first one doesn't use any formatting in legend, and another uses formatting using scale parameter:

Live Sample:  Legend - Token Parameters

As you can see - scaling improves the look of the legend tremendously and makes it easier to read.

to top

Font Settings and HTML tags

<font> node is used to configure font in any text elements in AnyChart Stock.

There are two options for font configuration:

Single Font

By default legend uses complex HTML formatting from defaults, but you can turn this formatting off and use uniform font for the item.

Sample XML to use uniform font:

XML/JSON Syntax
Plain code
01 <series type="Line" data_provider="dp1" color="#005ECB">
02   <name><![CDATA[IBM]]></name>
03   <legend_item>
04     <labels>
05       <font render_as_html="false" family="Tahoma" size="12" bold="true" />
06       <focus_settings>
07         <mouse_over>
08           <format><![CDATA[{%SeriesIcon} {%SeriesName} {%Value.Current}]]></format>
09         </mouse_over>
10         <mouse_out>
11           <format><![CDATA[{%SeriesIcon} {%SeriesName} {%Value.LastVisible}]]></format>
12         </mouse_out>
13       </focus_settings>
14     </labels>
15   </legend_item>
16 </series>
01{
02  type: "Line",
03  dataProvider: "dp1",
04  color: "#005ECB",
05  name: "IBM",
06  legendItem: {
07    labels: {
08      font: {
09        renderAsHtml: false,
10        family: "Tahoma",
11        size: 12,
12        bold: true
13      },
14      focusSettings: {
15        mouseOver: {
16          format: "{%SeriesIcon} {%SeriesName} {%Value.Current}"
17        },
18        mouseOut: {
19          format: "{%SeriesIcon} {%SeriesName} {%Value.LastVisible}"
20        }
21      }
22    }
23  }
24}

As you can see you need to disable HTML mode using render_as_html attribute in <font> node and define font settings in node.

Live sample of such settings:

Live Sample:  Legend - Labels Using General Font

As you can see from the sample "IBM" string became black as all other text - different coloring is possible only in HTML mode, which is more complex, but provides more options.

to top

HTML Tags

By default all labels in legend use HTML formatting. Depending on the type of series default is different.

With HTML formatting you have more flexibility. HTML gives you an ability to use %SeriesColor and %ChangeColor tokens, which color parts of the text depending on the value.

HTML tags should be specified using CDATA section. Sample XML with bold HTML tag <b>:

XML/JSON Syntax
Plain code
01 <labels>
02   <focus_settings>
03     <mouse_out>
04       <format><![CDATA[Value: <b>{%Value.LastVisible}</b>]]></format>
05     </mouse_out>
06     <mouse_over>
07       <format><![CDATA[Value: <b>{%Value.Current}</b>]]></format>
08     </mouse_over>
09   </focus_settings>
10 </labels>
01{
02  focusSettings: {
03    mouseOut: {
04      format: "Value: <b>{%Value.LastVisible}</b>"
05    },
06    mouseOver: {
07      format: "Value: <b>{%Value.Current}</b>"
08    }
09  }
10}

XML syntax below shows sample HTML formatting. It uses coloring, series icon, and value. Each token is colored using <font> tag:

XML/JSON Syntax
Plain code
02   <labels>
03     <focus_settings>
04       <mouse_over>
05         <format><![CDATA[{%SeriesIcon} <b><font color="%SeriesColor">{%SeriesName}</font> <font color="#808080">Value: </font><font color="#333333">{%Value.Current}</font></b> ]]></format>
06       </mouse_over>
07       <mouse_out>
08         <format><![CDATA[{%SeriesIcon} <b><font color="%SeriesColor">{%SeriesName}</font> <font color="#808080">Value: </font><font color="#333333">{%Value.LastVisible}</font></b> ]]></format>
09       </mouse_out>
10     </focus_settings>
11   </labels>
12 </legend_item>
01{
02  labels: {
03    focusSettings: {
04      mouseOver: {
05        format: "{%SeriesIcon} <b><font color=\"%SeriesColor\">{%SeriesName}</font> <font color=\"#808080\">Value: </font><font color=\"#333333\">{%Value.Current}</font></b> "
06      },
07      mouseOut: {
08        format: "{%SeriesIcon} <b><font color=\"%SeriesColor\">{%SeriesName}</font> <font color=\"#808080\">Value: </font><font color=\"#333333\">{%Value.LastVisible}</font></b> "
09      }
10    }
11  }
12}

Settings may look complex, but they allow to achieve great results:

Live Sample:  Legend - Labels Using HTML Formatting

This sample uses static texts and color tokens that change color of the text depending on the values. See more about this in Color Tokens section.

Flash Player doesn't support all HTML tags. Full list of supported tags can be found in Font Configuration article.

to top

Using Data Provider Custom Fields

In Required and Custom Fields section of Data Provider Configuration article you can find the description how to add custom fields into the data provider. One of the places of application of this feature is legend.

You can add custom fields values into the legend to provide additional information. XML for this may look like:

XML/JSON Syntax
Plain code
03     <data_provider data_set="dataSet1" id="dp1">
04       <fields>
05         <field type="Value" column="1" approximation_type="Average" />
06         <field type="Custom" custom_type_name="ExtraValue1" column="2" approximation_type="Average" />
07         <field type="Custom" custom_type_name="ExtraValue2" column="3" approximation_type="Average" />
08         <field type="Custom" custom_type_name="ExtraValue3" column="4" approximation_type="Average" />
09       </fields>
10     </data_provider>
13     <data_provider data_set="dataSet1" column="1" />
01{
03    {
04      dataSet: "dataSet1",
05      id: "dp1",
06      fields: [
07        {
08          type: "Value",
09          column: 1,
10          approximationType: "Average"
11        },
12        {
13          type: "Custom",
14          customTypeName: "ExtraValue1",
15          column: 2,
16          approximationType: "Average"
17        },
18        {
19          type: "Custom",
20          customTypeName: "ExtraValue2",
21          column: 3,
22          approximationType: "Average"
23        },
24        {
25          type: "Custom",
26          customTypeName: "ExtraValue3",
27          column: 4,
28          approximationType: "Average"
29        }
30      ]
31    }
32  ],
34    {
35      dataSet: "dataSet1",
36      column: 1
37    }
38  ]
39}

As you can see there is a standard Value fields and three fields named: ExtraValue1, ExtraValue2 and ExtraValue3, which come from the data set.

XML syntax below shows how to use the fields from data provider:

XML/JSON Syntax
Plain code
02   <mouse_over>
03     <format><![CDATA[{%Value.Current}, {%ExtraParam1.Current}, {%ExtraParam2.Current}]]></format>
04   </mouse_over>
05   <mouse_out>
06     <format><![CDATA[{%Value.LastVisible}, {%ExtraParam1.LastVisible}, {%ExtraParam2.LastVisible}]]></format>
07   </mouse_out>
01{
02  mouseOver: {
03    format: "{%Value.Current}, {%ExtraParam1.Current}, {%ExtraParam2.Current}"
04  },
05  mouseOut: {
06    format: "{%Value.LastVisible}, {%ExtraParam1.LastVisible}, {%ExtraParam2.LastVisible}"
07  }
08}

When you add custom field value you should {%CustomFieldName} token, where CustomFieldName is the name in data provider, for example: <format>{%ExtraValue1}</format>.

Live sample below shows several custom fields:

Live Sample:  Legend - Using Custom Fields

Another example of the similar situation: you show the line chart that visualizes closing price, but may also want to show open, high, low, close and volume in legend. You can either define custom name or use standard (reserved) names.

Following live sample shows how to do that:

Live Sample:  Legend - Using Custom Fields with OHLC Series

If you need text or additional date time fields you can use "Text" and "DateTime" custom fields as described in Data Provider Configuration: Text Fields and Data Provider Configuration: DateTime Fields. Sample XML Snippet below shows a data provider with two text fields and one additional DateTime field:

XML/JSON Syntax
Plain code
01 <?xml version="1.0" encoding="UTF-8"?>
02 <stock xmlns="http://anychart.com/products/stock/schemas/1.9.0/schema.xsd">
03   <data>
04     <data_providers>
05       <general_data_providers>
06         <data_provider data_set="dataSet1" id="s1">
07           <fields>
08             <field type="Value" column="1" />
09             <field type="Text" custom_type_name="Description" column="2" />
10             <field type="Text" custom_type_name="Note" column="3" />
11             <field type="DateTime" custom_type_name="UnixTimeStamp" column="4">
12               <date_time>
13                 <format><![CDATA[%u]]></format>
14               </date_time>
15             </field>
16           </fields>
17         </data_provider>
18       </general_data_providers>
19     </data_providers>
20   </data>
21 </stock>
01{
02  data: {
03    dataProviders: {
04      generalDataProviders: [
05        {
06          dataSet: "dataSet1",
07          id: "s1",
08          fields: [
09            {
10              type: "Value",
11              column: 1
12            },
13            {
14              type: "Text",
15              customTypeName: "Description",
16              column: 2
17            },
18            {
19              type: "Text",
20              customTypeName: "Note",
21              column: 3
22            },
23            {
24              type: "DateTime",
25              customTypeName: "UnixTimeStamp",
26              column: 4,
27              dateTime: {
28                format: "%u"
29              }
30            }
31          ]
32        }
33      ]
34    }
35  }
36}

To use these fields you just refer to them by name and provide formatting options if needed, here is a live sample of a chart with data provider configured as shown above and tokens used in legend:

Live Sample:  Legend - Using Text and DateTime Fields

to top

Series Custom Attributes

Each series can have several custom attributes that contain additional information about the series. These attributes can be used in legend items.

Syntax of series attributes definition:

XML/JSON Syntax
Plain code
01 <chart>
02   <legend>
03     <labels>
04       <focus_settings>
05         <mouse_over>
06           <format><![CDATA[{%ShortName} - {%LongName}]]></format>
07         </mouse_over>
08         <mouse_out>
09           <format><![CDATA[{%ShortName} - {%LongName}]]></format>
10         </mouse_out>
11       </focus_settings>
12     </labels>
13   </legend>
14   <series_list>
15     <series type="Area" color="#DC3912" data_provider="dp1">
16       <attributes>
17         <attr name="ShortName"><![CDATA[MSFT]]></attr>
18         <attr name="LongName"><![CDATA[Microsoft Corp.]]></attr>
19       </attributes>
20     </series>
21     <series type="Area" color="#0066DD" data_provider="dp2">
22       <attributes>
23         <attr name="ShortName"><![CDATA[ORCL]]></attr>
24         <attr name="LongName"><![CDATA[Oracle Corp.]]></attr>
25       </attributes>
26     </series>
27   </series_list>
28 </chart>
01{
02  legend: {
03    labels: {
04      focusSettings: {
05        mouseOver: {
06          format: "{%ShortName} - {%LongName}"
07        },
08        mouseOut: {
09          format: "{%ShortName} - {%LongName}"
10        }
11      }
12    }
13  },
14  seriesList: [
15    {
16      type: "Area",
17      color: "#DC3912",
18      dataProvider: "dp1",
19      attributes: {
20        ShortName: "MSFT",
21        LongName: "Microsoft Corp."
22      }
23    },
24    {
25      type: "Area",
26      color: "#0066DD",
27      dataProvider: "dp2",
28      attributes: {
29        ShortName: "ORCL",
30        LongName: "Oracle Corp."
31      }
32    }
33  ]
34}

As you can see from the listing - legend item uses custom attributes. To put attribute value into the legend use {%SomeAttributeName} tokens, where SomeAttributeName is the name specified in: <attr name="AttrName"/>.

Live sample below shows series with ShortName and LongName attributes. These attributes are used in legend items:

Live Sample:  Legend - Using Series Custom Attributes

to top

Technical Indicators Legend Differences

Technical indicators use pretty similar to series visualization options. Different technical indicators are represented by some parameters that are displayed on the chart. For example, Volume+MA shows: Volume and Moving Average as Bar and Line series.

All visualization settings of technical indicator are identical to the series visualization series and so, the legend item is similar too.

For example, Stochastic Oscillator. This indicator visualizes K-Period and D-Period. There is a special section for both of them in XML. XML below shows the settings for K-Period and D-Period:

XML/JSON Syntax
Plain code
02   <k_series type="Line" color="#253992">
03     <legend_item>
04       <labels />
05     </legend_item>
06   </k_series>
07   <d_series type="Line" color="#DB2A0E">
08     <legend_item>
09       <labels />
10     </legend_item>
11   </d_series>
01{
02  kPeriod: 15,
03  dPeriod: 5,
04  kSeries: {
05    type: "Line",
06    color: "#253992",
07    legendItem: {
08      labels: {}
09    }
10  },
11  dSeries: {
12    type: "Line",
13    color: "#DB2A0E",
14    legendItem: {
15      labels: {}
16    }
17  }
18}

As you can see settings are similar to series settings and for each parameter you can configure <legend_item>.

Sample XML of indicator with legend item formatting strings defined:

XML/JSON Syntax
Plain code
02   <k_series type="Line" color="#253992">
03     <legend_item enabled="true">
04       <labels>
05         <focus_settings>
06           <mouse_over>
07             <format><![CDATA[{%SeriesIcon} <b><font color="%Color">{%SeriesName} {%Value.Current}</font></b> ]]></format>
08           </mouse_over>
09           <mouse_out>
10             <format><![CDATA[{%SeriesIcon} <b><font color="%Color">{%SeriesName} {%Value.LastVisible}</font></b> ]]></format>
11           </mouse_out>
12         </focus_settings>
13       </labels>
14     </legend_item>
15     <name><![CDATA[Stochastic: %K(15)]]></name>
16   </k_series>
17   <d_series type="Line" color="#DB2A0E">
18     <legend_item enabled="true">
19       <labels>
20         <focus_settings>
21           <mouse_over>
22             <format><![CDATA[{%SeriesIcon} <b><font color="%Color">{%SeriesName} {%Value.Current}</font></b>]]></format>
23           </mouse_over>
24           <mouse_out>
25             <format><![CDATA[{%SeriesIcon} <b><font color="%Color">{%SeriesName} {%Value.LastVisible}</font></b>]]></format>
26           </mouse_out>
27         </focus_settings>
28       </labels>
29     </legend_item>
30     <name><![CDATA[%D(5)]]></name>
31   </d_series>
01{
02  kPeriod: 15,
03  dPeriod: 5,
04  kSeries: {
05    type: "Line",
06    color: "#253992",
07    legendItem: {
08      enabled: true,
09      labels: {
10        focusSettings: {
11          mouseOver: {
12            format: "{%SeriesIcon} <b><font color=\"%Color\">{%SeriesName} {%Value.Current}</font></b> "
13          },
14          mouseOut: {
15            format: "{%SeriesIcon} <b><font color=\"%Color\">{%SeriesName} {%Value.LastVisible}</font></b> "
16          }
17        }
18      }
19    },
20    name: "Stochastic: %K(15)"
21  },
22  dSeries: {
23    type: "Line",
24    color: "#DB2A0E",
25    legendItem: {
26      enabled: true,
27      labels: {
28        focusSettings: {
29          mouseOver: {
30            format: "{%SeriesIcon} <b><font color=\"%Color\">{%SeriesName} {%Value.Current}</font></b>"
31          },
32          mouseOut: {
33            format: "{%SeriesIcon} <b><font color=\"%Color\">{%SeriesName} {%Value.LastVisible}</font></b>"
34          }
35        }
36      }
37    },
38    name: "%D(5)"
39  }
40}

Live sample of such settings:

Live Sample:  Legend - Technical Indicators

Learn more about technical indicators settings in Technical Indicator Settings.

to top

Series Icon

When you use {%SeriesIcon} token in legend formatting string it is replaced by icon. Be default icon is a square 8x8 pixels. You can configure icon for each series: shape, size, fill and border.

Sample XML syntax of icon configuration:

XML/JSON Syntax
Plain code
01 <series color="#FF0000">
02   <legend_item>
03     <labels>
04       <icon type="Diamond" size="10">
05         <fill type="Solid" color="%Color" opacity="1" />
06         <border color="DarkColor(%Color)" opacity="0.8" />
07       </icon>
08     </labels>
09   </legend_item>
10 </series>
01{
02  color: "#FF0000",
03  legendItem: {
04    labels: {
05      icon: {
06        type: "Diamond",
07        size: 10,
08        fill: {
09          type: "Solid",
10          color: "%Color",
11          opacity: 1
12        },
13        border: {
14          color: "DarkColor(%Color)",
15          opacity: 0.8
16        }
17      }
18    }
19  }
20}

As you can see:

Icon color is set using %Color token. This token value comes from series color attribute. You can use color transform options described in Color article or static color like <fill color="#55DD00"/>.

size attribute defines icon width and height in pixels. Note that setting size to big may result in icon out of legend borders, it is not recommended to use size bigger than 15px.

Shape is set using type attribute. By default it is set to Square, but you can use any of the shapes listed below:

Type
Circle
Square
Diamond
Cross
DiagonalCross
HLine
VLine
Star4
Star5
Star6
Star7
TriangleUp
TriangleDown

Live sample of the legend with different icons in different series:

Live Sample:  Legend - Labels Icon Settings

to top

Color Tokens

As it was said above HTML formatting is used in legend configuration. To color part of the text <font> tag is used. For example: <font color="#FF0000">{%Value.Current}</font>.

You can either use constant color or color tokens.

There are two color tokens that can be used:

XML to use {%Color} token. In this sample text is colored to the series color:

XML/JSON Syntax
Plain code
02   <mouse_over>
03     <format><![CDATA[<font color="%Color">{%SeriesName} {%Value.Current}</font>]]></format>
04   </mouse_over>
05   <mouse_out>
06     <format><![CDATA[<font color="%Color">{%SeriesName} {%Value.LastVisible}</font>]]></format>
07   </mouse_out>
01{
02  mouseOver: {
03    format: "<font color=\"%Color\">{%SeriesName} {%Value.Current}</font>"
04  },
05  mouseOut: {
06    format: "<font color=\"%Color\">{%SeriesName} {%Value.LastVisible}</font>"
07  }
08}

XML to use {%ChangeColor} token. In this sample text us either Green or Red - depending on the value:

XML/JSON Syntax
Plain code
01 <chart>
02   <legend>
03     <labels falling_color="DarkRed" rising_color="#005500" />
04   </legend>
05   <series_list>
06     <series>
07       <legend_item>
08         <labels>
09           <focus_settings>
10             <mouse_over>
11               <format><![CDATA[<b><font color="%ChangeColor">{%ValueChange.Current}</font></b>]]></format>
12             </mouse_over>
13             <mouse_out>
14               <format><![CDATA[<b><font color="%ChangeColor">{%ValueChange.LastVisible}</font></b>]]></format>
15             </mouse_out>
16           </focus_settings>
17         </labels>
18       </legend_item>
19     </series>
20   </series_list>
21 </chart>
01{
02  legend: {
03    labels: {
04      fallingColor: "DarkRed",
05      risingColor: "#005500"
06    }
07  },
08  seriesList: [
09    {
10      legendItem: {
11        labels: {
12          focusSettings: {
13            mouseOver: {
14              format: "<b><font color=\"%ChangeColor\">{%ValueChange.Current}</font></b>"
15            },
16            mouseOut: {
17              format: "<b><font color=\"%ChangeColor\">{%ValueChange.LastVisible}</font></b>"
18            }
19          }
20        }
21      }
22    }
23  ]
24}

As you can see both colors are defined in falling_color and rising_color attributes of <labels> node.

Live sample below shows how {%Color} and {%ChangeColor} tokens can be used:

Live Sample:  Legend - Working with Color Tokens

to top

Falling/Rising Icons

Besides coloring the changes you can show them as icons: red arrow down or green arrow up. These icons are added into the legend using {%ChangeIcon} token.

{%ChangeIcon} has Current, First, Last, FirstVisible and LastVisible properties that define which value change should be shown and you should always specify property when you use this token. As the matter of fact {%ChangeIcon} is similar to {%ValueChange} and {%PercentValueChange}, but it shows only the trend.

XML to add these icons into the legend:

XML/JSON Syntax
Plain code
02   <mouse_over>
03     <format><![CDATA[{%SeriesIcon} {%SeriesName} {%ChangeIcon.Current} {%ValueChange.Current}]]></format>
04   </mouse_over>
05   <mouse_out>
06     <format><![CDATA[{%SeriesIcon} {%SeriesName} {%ChangeIcon.LastVisible} {%ValueChange.LastVisible}]]></format>
07   </mouse_out>
01{
02  mouseOver: {
03    format: "{%SeriesIcon} {%SeriesName} {%ChangeIcon.Current} {%ValueChange.Current}"
04  },
05  mouseOut: {
06    format: "{%SeriesIcon} {%SeriesName} {%ChangeIcon.LastVisible} {%ValueChange.LastVisible}"
07  }
08}

Visual appearance of icons can be configured, you can change size, fill and border.

XML for that looks like that:

XML/JSON Syntax
Plain code
01 <chart>
02   <legend>
03     <labels>
04       <falling_rising_icons size="12">
05         <falling>
06           <fill enabled="true" color="#CC3300" opacity="1" />
07           <border enabled="false" />
08         </falling>
09         <rising>
10           <fill color="Green" opacity="1" />
11           <border enabled="false" />
12         </rising>
13       </falling_rising_icons>
14     </labels>
15   </legend>
16 </chart>
01{
02  legend: {
03    labels: {
04      fallingRisingIcons: {
05        size: 12,
06        falling: {
07          fill: {
08            enabled: true,
09            color: "#CC3300",
10            opacity: 1
11          },
12          border: {
13            enabled: false
14          }
15        },
16        rising: {
17          fill: {
18            color: "Green",
19            opacity: 1
20          },
21          border: {
22            enabled: false
23          }
24        }
25      }
26    }
27  }
28}

Live sample below shows icons configuration:

Live Sample:  Legend - Using Change Icons

to top

Line Breaks in Legend

In charts with the big number of series or when series names are long it may be useful to put line breaks between to arrange items in several rows.

Illustration below shows legend with default settings and line breaks. As you can see "MSFT" and "ORCL" series are not visible at all with default settings:

XML below shows how to add line breaks:

XML/JSON Syntax
Plain code
01 <chart>
02   <legend>
03     <title line_break="true" />
04   </legend>
05   <series_list>
06     <series type="Candlestick" data_provider="dp1" color="#005ECB">
07       <name><![CDATA[IBM]]></name>
08       <legend_item line_break="true" />
09     </series>
10     <series type="Line" data_provider="dp2" color="#253992">
11       <name><![CDATA[MSFT]]></name>
12       <legend_item line_break="true" />
13     </series>
14     <series type="Line" data_provider="dp3" color="#DB2A0E">
15       <name><![CDATA[ORCL]]></name>
16       <legend_item line_break="true" />
17     </series>
18   </series_list>
19 </chart>
01{
02  legend: {
03    title: {
04      lineBreak: true
05    }
06  },
07  seriesList: [
08    {
09      type: "Candlestick",
10      dataProvider: "dp1",
11      color: "#005ECB",
12      name: "IBM",
13      legendItem: {
14        lineBreak: true
15      }
16    },
17    {
18      type: "Line",
19      dataProvider: "dp2",
20      color: "#253992",
21      name: "MSFT",
22      legendItem: {
23        lineBreak: true
24      }
25    },
26    {
27      type: "Line",
28      dataProvider: "dp3",
29      color: "#DB2A0E",
30      name: "ORCL",
31      legendItem: {
32        lineBreak: true
33      }
34    }
35  ]
36}

Live sample with complex formatting and line breaks:

Live Sample:  Legend - Using Breaklines

to top

Removing Items from Legend

By default any series or indicator has the item in the legend. If you don't want to see some of them- you can disable it using enabled attribute: <legend_item enabled="false"> in <series> node.

Sample XML to leave only one series item in legend:

XML/JSON Syntax
Plain code
01 <chart>
02   <series_list>
03     <series type="Line" color="#DC3912" data_provider="dp1">
04       <name><![CDATA[MSFT]]></name>
05       <legend_item enabled="false" />
06     </series>
07     <series type="Line" color="#0066DD" data_provider="dp2">
08       <name><![CDATA[ORCL]]></name>
09     </series>
10     <series type="Line" color="#FF9900" data_provider="dp3">
11       <name><![CDATA[CSCO]]></name>
12       <legend_item enabled="false" />
13     </series>
14   </series_list>
15 </chart>
01{
02  seriesList: [
03    {
04      type: "Line",
05      color: "#DC3912",
06      dataProvider: "dp1",
07      name: "MSFT",
08      legendItem: {
09        enabled: false
10      }
11    },
12    {
13      type: "Line",
14      color: "#0066DD",
15      dataProvider: "dp2",
16      name: "ORCL"
17    },
18    {
19      type: "Line",
20      color: "#FF9900",
21      dataProvider: "dp3",
22      name: "CSCO",
23      legendItem: {
24        enabled: false
25      }
26    }
27  ]
28}

Sample chart below shows three series. For the series named MSFT and CSCO legend item is turned off:

Live Sample:  Legend - Excluding Items

to top

Elements with Fixed Width

When legend items do not use line breaks and are displayed in one line you may observe "jumping" of the items when you hover different dates. To avoid this situation you can define fixed width for the items.

Sample XML to set fixed width of the items:

XML/JSON Syntax
Plain code
01 <chart>
02   <series_list>
03     <series type="Line" color="#DC3912" data_provider="dp1">
04       <name><![CDATA[MSFT]]></name>
05       <legend_item>
06         <labels width="140" />
07       </legend_item>
08     </series>
09     <series type="Line" color="#0066DD" data_provider="dp2">
10       <name><![CDATA[ORCL]]></name>
11       <legend_item>
12         <labels width="140" />
13       </legend_item>
14     </series>
15   </series_list>
16 </chart>
01{
02  seriesList: [
03    {
04      type: "Line",
05      color: "#DC3912",
06      dataProvider: "dp1",
07      name: "MSFT",
08      legendItem: {
09        labels: {
10          width: 140
11        }
12      }
13    },
14    {
15      type: "Line",
16      color: "#0066DD",
17      dataProvider: "dp2",
18      name: "ORCL",
19      legendItem: {
20        labels: {
21          width: 140
22        }
23      }
24    }
25  ]
26}

As you can see we use width attribute in <label> node: <labels width="140">. The width is set in pixels, so you should make sure that it is large enough for the longest possible values with the certain font settings.

Live sample where all items have fixed width:

Live Sample:  Legend - Label with Fixed Width

to top