CT.10 Changing Legend Properties

When you assign a VisualFrame to a chart element to visually code data, a corresponding legend is created automatically. You can change the appearance of this legend by editing the VisualFrame's LegendSpec.

Consider the following script:

Note: Script that modifies 'graph' should be placed at the element level. See Adding Element-Level Script in Report Scripting and Adding Component Script in Dashboard Scripting for more information.

dataset = [["State", "Quantity"], ["NJ",200], ["NY",300]];

graph = new EGraph();

var elem = new IntervalElement("State", "Quantity");

var frame = new CategoricalColorFrame("State");

elem.setColorFrame(frame);

graph.addElement(elem);

 

This creates a basic bar chart displaying the dimensions 'State' and 'Quantity'. In this script, a CategoricalColorFrame distinguishes the different states by color. Follow the steps below to experiment with modifying the chart's legend:

1. Change the legend border to a blue dotted line. To do this, create a LegendSpec object and assign it to the ColorFrame.

var legend = new LegendSpec();

legend.setBorder(GraphConstants.DOT_LINE);

legend.setBorderColor(java.awt.Color(0x0000ff));

frame.setLegendSpec(legend);

2. Change the legend title to say simply 'State', and make the text bold. To do this, create a TextSpec object and assign it to the LegendSpec.

var tspec = new TextSpec();

tspec.setFont(java.awt.Font('Dialog',

              java.awt.Font.BOLD, 14));

legend.setTitleTextSpec(tspec);

legend.setTitle('State');

3. Change the text inside the legend to display the full state names. To do this, create a TextFrame object and assign it to the LegendSpec.

var tframe = new DefaultTextFrame();

tframe.setText('NJ','New Jersey');

tframe.setText('NY','New York');

legend.setTextFrame(tframe);

4. Place the legend above the chart. Use the Graph's legendLayout property to do this.

graph.setLegendLayout(GraphConstants.TOP);

 

The complete script is shown below.

dataset = [["State", "Quantity"], ["NJ",200], ["NY",300]];

graph = new EGraph();

var elem = new IntervalElement("State", "Quantity");

var frame = new CategoricalColorFrame("State");

elem.setColorFrame(frame);

 

var legend = new LegendSpec();

legend.setBorder(GraphConstants.DOT_LINE);

legend.setBorderColor(java.awt.Color(0x0000ff));

frame.setLegendSpec(legend);

var tspec = new TextSpec();

tspec.setFont(java.awt.Font('Dialog',java.awt.Font.BOLD, 14));

legend.setTitleTextSpec(tspec);

legend.setTitle('State');

var tframe = new DefaultTextFrame();

tframe.setText('NJ','New Jersey');

tframe.setText('NY','New York');

legend.setTextFrame(tframe);

graph.setLegendLayout(GraphConstants.TOP);

 

graph.addElement(elem);

See Also

Representing Multiple Measures, for information on legends for multiple elements.

Representing Data with Shape, Color, Size, for more information on VisualFrames.

<< CT.9 Changing Axis Properties © 1996-2013 InetSoft Technology Corporation (v11.4) CT.11 Changing Chart Labels >>