IMSL C# Programmer’s Guide
2D Drawing Elements >> Legend  Previous Page  Contents  Next Page

Legend

The legend is used to identify data sets. Data nodes that have their Title attribute defined are automatically included in the legend box.

The Legend node is automatically created by the Chart node as its child. By default, the legend is not drawn because its IsVisible property is set to false.

Simple Legend Example

At a minimum, adding a legend to a chart requires setting the legend’s IsVisible property to true and setting the Title attribute in the Data nodes that are to appear in the legend box. This example shows such a minimal legend.

(Download Code)
using Imsl.Chart2D;
using System.Drawing;

public class SampleSimpleLegend : FrameChart {

    public SampleSimpleLegend() {
        Chart chart = this.Chart;
        AxisXY axis = new AxisXY(chart);
        chart.Legend.IsVisible = true;

        double[] y1 = new double[] {4, 6, 2, 1, 8};
        Data data1 = new Data(axis,  y1);
        data1.DataType = Data.DATA_TYPE_LINE;
        data1.LineColor = Color.Red;
        data1.SetTitle(“Line”);

        double[] y2 = new double[] {7, 3, 4, 5, 2};
        Data data2 = new Data(axis,  y2);
        data2.DataType = Data.DATA_TYPE_MARKER;
        data2.MarkerColor = Color.Blue;
        data2.SetTitle(“Marker”);
    }

    public static void Main(string[] argv) {
        System.Windows.Forms.Application.Run(new SampleSimpleLegend());
    }
}

Legend Example

This example shows more of the attributes that affect a legend. If the legend’s Title attribute is set, then it is used as a header in the legend box.

The text properties for all of the text strings in the legend box are obtained from the Legend node, not from the associated Data nodes (see Text Attributes). Here the TextColor is set to white.

The background of the legend box can be set by changing the fill attributes (see Fill Area Attributes). By default in the Legend node, FillType is set to FILL_TYPE_NONE and FillColor is set to Color.LightGray.

The position of the legend box is controlled by its Viewport attribute. The viewport is the region of the Control, in which the chart is being drawn, that the legend box occupies. The upper left corner is (0,0) and the lower right corner is (1,1). The default value of the legend viewport is [0.83, 0.0] by [0.2, 0.2].

(Download Code)
using Imsl.Chart2D;
using System.Drawing;

public class SampleLegend : FrameChart {

    public SampleLegend() {
        Chart chart = this.Chart;
        AxisXY axis = new AxisXY(chart);

        Legend legend = chart.Legend;
        legend.IsVisible = true;;
        legend.SetTitle(“Legend”);
        legend.TextColor = Color.White;
        legend.FillType = Legend.FILL_TYPE_SOLID;
        legend.SetViewport(0.3, 0.4, 0.1, 0.4);

        double[] y1 = new double[] {4, 6, 2, 1, 8};
        Data data1 = new Data(axis,  y1);
        data1.DataType = Data.DATA_TYPE_LINE;
        data1.LineColor = Color.Red;
        data1.SetTitle(“Line”);

        double[] y2 = new double[] {7, 3, 4, 5, 2};
        Data data2 = new Data(axis,  y2);
        data2.DataType = Data.DATA_TYPE_MARKER;
        data2.MarkerColor = Color.Blue;
        data2.SetTitle(“Marker”);
    }

    public static void Main(string[] argv) {
        System.Windows.Forms.Application.Run(new SampleLegend());
    }
}




©  Visual Numerics, Inc.  All rights reserved.  Previous Page  Contents  Next Page