Legend

The Legend for a bar chart is turned on by setting the Legend's Paint attribute to true and defining the Title attributes for the legend entries. The legend entries are the BarSet objects. The following example is the stacked, grouped bar example with the legend enabled.

(Download Code)

 

import com.imsl.chart.*;

import java.awt.Color;

 

public class SampleBarLegend extends JFrameChart {  

    public SampleBarLegend() {

        Chart chart = getChart();

        AxisXY axis = new AxisXY(chart);

        chart.getLegend().setPaint(true);

        // y is a 2 by 3 by 4 array

        double y[][][] = {

            {{4,2,3,9},{8,4,2,3},{1,5,3,8}},

            {{6,7,5,2},{4,1,7,2},{8,5,6,1}}

        };

        Bar bar = new Bar(axis, y);

        bar.setBarType(Bar.BAR_TYPE_VERTICAL);

        bar.setLabels(new String[]{"A","B","C","D"});

        // group 0 - shades of red

        bar.getBarSet(0,0).setTitle("Red");

        bar.getBarSet(0,0).setFillColor(Color.red);

        bar.getBarSet(1,0).setTitle("Dark Red");

        bar.getBarSet(1,0).setFillColor("DarkRed");

        // group 1 - shades of blue

        bar.getBarSet(0,1).setTitle("Blue");

        bar.getBarSet(0,1).setFillColor(Color.blue);

        bar.getBarSet(1,1).setTitle("Light Blue");

        bar.getBarSet(1,1).setFillColor("LightBlue");

        // group 2 - shades of gray

        bar.getBarSet(0,2).setTitle("Gray");

        bar.getBarSet(0,2).setFillColor(Color.gray);

        bar.getBarSet(1,2).setTitle("Light Gray");

        bar.getBarSet(1,2).setFillColor(Color.lightGray);

    }

   

    public static void main(String argv[]) {

        new SampleBarLegend().setVisible(true);

    }

}



Visual Numerics - Developers of IMSL and PV-WAVE
http://www.vni.com/
PHONE: 713.784.3131
FAX:713.781.9260