5.10.3 Legend

Legend is a rectangular box with a list of text labels with legend symbols, corresponding to the chart series. There are two types of legends available in MCharts:

Legend Class Hierarchy

Figure 5.10.3.1 Legend Class Hierarchy

Legend entries can be filled and maintained manually or automatically. The simplest way to prepare a legend is to call Chart’s method createLegendEntries. It creates a list of legend entries, which can be simply added to a legend.

Also, the base class AbstractChartSeries has a createLegendSymbol method, which returns the legend symbol that was set by calling an initLegendSymbol or a setLegendSymbol method:

The returned legend symbol references the same stylesheet, the chart object uses, so when the style of the chart object changes, the legend entry is also updated automatically. For example, changing the line color of the line chart also changes the line color of the corresponding legend entry. There are implemented special symbols for some series (BarSeries, BubbleSeries, StockSeries, LineSeries). These symbols are realized as nested classes, carrying own methods.

You can use legend symbol factory for setting legend symbols for single parts of series. It is implemented by adding interface IndexedEntityLegendSymbolFactory in BarSeries, LineSeries and so on. Legend entries for the single parts of series must be set manually.

Legend

Creating simple Legend:

 
Legend legend = new Legend(); //creating empty legend
legend.addEntries(chart.createLegendEntries());  //setting legend entries
legend.getPaintStyle().setForeground(Color.black);

Legend

Figure 5.10.3.2 Simple Legend Demonstration

ColorScaleLegend

Creating simple ColorScaleLegend:

1
2
3
4
5
   ColorScaleLegend legend = new ColorScaleLegend(chart);
    legend.setLabelAlignment(Alignment.CENTER, Alignment.CENTER);
    legend.setOrientation(Orientation.HORIZONTAL);
    legend.setWordWrappingEnabled(true);
    legend.getPaintStyle().setForeground(Color.black);

ColorScaleLegend Demonstration

Figure 5.10.3.3 ColorScaleLegend Demonstration

Other Legend Properties

Column Size

It is possible to set maximum number of columns in one row, which enables wrapping of legend. This can be set using setMaxColumns() method.

 
Legend legend = new Legend(chart);
legend.setMaxColumns(2);

Maximum number of columns set to 2

Figure 5.10.3.4 Maximum number of columns set to 2

Layout

Layout of legend can be set using setLayout() method. There are 3 main types of layout's that can be used. They are stored in LegendLayout enum class.

Tag Explanation
LegendLayout.EXPANDED_LAYOUTUsing this layout Legend is stretched so that it is of the same width or height as the chart itself
LegendLayout.GRID_LAYOUTUsing this layout, all cells have the same height and width as the largest cell in a Legend
LegendLayout.COMPACT_LAYOUT Cell width is determined by the widest cell in the same column and cell height is determined by the highest cell in the same row

Table 5.10.3.1 LegendLayout tags

Setting Legend Layout:

1
2
3
4
//sets a new Legend object named legend
Legend legend = new Legend(chart);
//sets legend layout
legend.setLayout(LegendLayout.EXPANDED_LAYOUT);

LegendLayout.EXPANDED demonstration

Figure 5.10.3.5 LegendLayout.EXPANDED demonstration

View Source

Scrolling

Legend scrolling is automatically enabled, if Legend is added to the right or left side of DecoratedView or LabeledChart and full height of Legend is bigger than available space.

Wrapping of Text

If there are labels that contain much information, it is possible to wrap text. Wrapping is disabled by default. To enable word wrapping setWordWrappingEnabled() method must be used.

1
2
3
4
//sets a new Legend object
Legend legend = new Legend();
//enables wrapping
legend.setWordWrappingEnabled(true);