Setting the legend box position

The legend box is represented by the Legend class. Multiple legend boxes can be displayed, positioned at different locations and showing the titles of distinct groups of series. For example, a chart displaying two vertical scales can have one legend box to the left and another one to the right, showing the titles of series whose data points are plotted against the left and vertical scales, respectively.
There is no limit on the number of legend boxes that can be added to the chart context. By default, only one legend box is displayed, showing the titles of all series added to the chart context. The default legend box, referred to as the primary legend box, is automatically created. Any additional legend box, referred to as a secondary legend box, has to be explicitly created and added to the chart context.

The only chart contexts that support multiple legend boxes are Graph and ScatterGraph. PieGraph doesn't support.

To create a secondary legend box and add it to the chart context, do as follow:
Graph graph=new Graph();
Legend l=new Legend();
graph.addLegend(l);


To display the title of a series inside a secondary legend box, do as follow:
LineSerie ls=new LineSerie(new double[]{100,80,200,300},"line series");
ls.setColor(Color.red);
Legend l=new Legend();
graph.addLegend(l);

l.addSerie(ls);

Each legend box is assigned an index. The primary one has index 0 and secondary legend boxes are sequentially assigned indexes in the order they are added to the chart context, starting with number 1. These indexes are used to get references to respective legend boxes, as follow:

Legend l=graph.getLegend(0); // returns a reference to the primary legend box.
Legend l2=graph.getLegend(1); // returns a reference to a secondary legend box.


By default, the orientation of a legend box is automatic. If it is placed at the bottom side, series titles are displayed side by side(horizontal orientation), and if it is placed at the left or right sides, series titles are stacked one above the other(vertical orientation).

To disable auto orientation and set orientation manually, use the methods Legend.setAutoOrientationEnabled(boolean isAutoOrientationEnabled) and
Legend.setOrientation(int orientation). Set the former to false and pass one of the constants below to the latter:

The legend box is, by default, automatically centered at the bottom of the chart area. Six constants can be used to specify the position of the legend box, using the method Legend.setPosition(int position):

Additionally, it is possible to dynamically change the position of any legend box position using the mouse.

To dynamically change the legend box position, double click it to toggle chart into a 'moving legend' state. While in this mode, a red thick line surrounds the legend box and the chart does not answer to mouse events, like dragging or resizing, until the legend box is dropped onto the desired position. After toggling chart state, move mouse cursor to the position where the legend box will be moved to and do the following:

To resume from a 'moving legend' state without changing legend position, just double-click the legend box again.

The following example places two legend boxes on the chart, one to the left and the other to the right.


import javax.swing.*;
import java.awt.*;
import com.jinsight.jetchart.*;

public class Main extends JFrame {

   public Main() { 

        Graph graph=new Graph();
	
        String[] labels={"label1","label2","label3","label4"};
        graph.setLabels(labels);
	
        GraphSet graphSet=graph.getGraphSet(0);

        Grid grid=graphSet.getGrid();

        grid.setEnabled(true);
        grid.setColor(Color.gray);

        String[] title={"The JetChart Library","Setting the legend box position"};

        graph.setTitle(title);

        LeftTitle lt=graph.getLeftTitle();
        lt.setText("Left title");

        RightTitle rt=graph.getRightTitle();
        rt.setText("Right title");

        BottomTitle bt=graph.getBottomTitle();
        bt.setText("Bottom title");

        Container ct=getContentPane();

        ct.add("Center",graph);

        LineSerie ls1=new LineSerie();
        ls1.setTitle("Line series 1");
        ls1.setColor(Color.red);
        double[] values1={100,80,90,110};
        ls1.setValues(values1);

        BarSerie bs1=new BarSerie();
        bs1.setTitle("Bar series 1");
        bs1.setColor(Color.blue);
        double[] values2={50,70,85,130};
        bs1.setValues(values2);

        LineSerie ls2=new LineSerie();
        ls2.setTitle("Line series 2");
        ls2.setColor(Color.green);
        double[] values3={40,60,50,80};
        ls2.setValues(values3);
	
        BarSerie bs2=new BarSerie();
        bs2.setTitle("Bar series 1");
        bs2.setColor(Color.yellow);
        double[] values4={130,90,70,40};
        bs2.setValues(values4);

        graph.addSerie(bs1);
        graph.addSerie(bs2);
        graph.addSerie(ls1);
        graph.addSerie(ls2);
        
        // Gets a reference to the primary legend box. The index of the 
        // primary legend box is always 0.
        Legend legend=graph.getLegend(0);
	
        // Sets the position of the primary legend box to RIGHT.
        legend.setPosition(Legend.RIGHT);
	
        // Creates a secondary legend box and adds the two line series to 
        // it. The remaining series, not added to secondary legend boxes,
        // are automatically added to the primary legend box.
        Legend l=new Legend();
        graph.addLegend(l);
        l.addSerie(ls1);
        l.addSerie(ls2);

        // Sets the position of the secondary legend box to LEFT.
        l.setPosition(Legend.LEFT);
        
	

        setSize(600,400);

        setVisible(true);


  }

  public static void main(String[] args) {
        new Main();
  }

}