Setting the legend box layout

Depending on the number of series displayed, a horizontal legend box might advance the boundaries of the chart area, hidding series titles. In such case, it is necessary to reduce the number of columns displayed, stacking series titles one above the other. The Legend class provides three methods to configure the legend box columns layout and the gaps between rows and columns.

The following example displays six line series. The legend layout is configured to display 3 columns, rather than leaving the titles disposed side by side, in which case the legend box would be partially hidden. The last two series titles were split into two lines, by inserting the '\n' character in the proper place.

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

public class Main extends JFrame {

   public Main() { 

        Graph graph=new Graph();
        
        Legend legend=graph.getLegend(0);
        legend.setColumnLayout(3);
        
        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 layout"};

        graph.setTitle(title);

        graph.set3DEnabled(true);

        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 series1");
        ls1.setColor(Color.decode("#ff0000"));
        double[] values1={3543,3000,2890,2991,3000,2500,2300,1900,2200,1800};
        ls1.setValues(values1);
        
        LineSerie ls2=new LineSerie();
        ls2.setTitle("Line series2");
        ls2.setColor(Color.decode("#0000ff"));
        double[] values2={3543,3000,2890,2991,3000,2500,2300,1900,2200,1800};
        ls2.setValues(values2);

        LineSerie ls3=new LineSerie();
        ls3.setTitle("Line series3");
        ls3.setColor(Color.decode("#00ff00"));
        double[] values3={3543,3000,2890,2991,3000,2500,2300,1900,2200,1800};
        ls3.setValues(values3);

        LineSerie ls4=new LineSerie();
        ls4.setTitle("Line series4");
        ls4.setColor(Color.decode("#ffff00"));
        double[] values4={3543,3000,2890,2991,3000,2500,2300,1900,2200,1800};
        ls4.setValues(values4);

        LineSerie ls5=new LineSerie();
        ls5.setTitle("Line\n series5");
        ls5.setColor(Color.decode("#ff00ff"));
        double[] values5={3543,3000,2890,2991,3000,2500,2300,1900,2200,1800};
        ls5.setValues(values5);
	
        LineSerie ls6=new LineSerie();
        ls6.setTitle("Line\n series6");
        ls6.setColor(Color.decode("#00ffff"));
        double[] values6={3543,3000,2890,2991,3000,2500,2300,1900,2200,1800};
        ls6.setValues(values6);
	
        graph.addSerie(ls1);
        graph.addSerie(ls2);
        graph.addSerie(ls3);
        graph.addSerie(ls4);
        graph.addSerie(ls5);
        graph.addSerie(ls6);

        setSize(500,400);

        setVisible(true);


  }

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

}