Stacked Grouped Bar Chart

The most general form of the bar chart is a stacked, grouped bar chart.

The data argument to the constructor for a stacked, grouped bar chart is an nStacks by nGroups by nItems array of doubles. In this example there are two stacks in three groups each containing four items. All of the stacks must contain the same number of groups and all of the groups must contain the same number of items.

The getBarSet(int,int) method returns a BarSet object that is a collection of the BarItems that make up a given stack/group. Here within each group the stacks are set to shades of the same color.

A stacked bar chart, without groups, can be constructed as a stacked-grouped bar chart with one group.

(Download Code)

 

import com.imsl.chart.*;

import java.awt.Color;

 

public class SampleBarGroupStack extends JFrameChart {

    static final Color darkRed = new Color(196,0,0);

    static final Color lightBlue = new Color(196,185,253);

   

    public SampleBarGroupStack() {

        Chart chart = getChart();

        AxisXY axis = new AxisXY(chart);

        // 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).setFillColor(Color.red);

        bar.getBarSet(1,0).setFillColor(darkRed);

        // group 1 - shades of blue

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

        bar.getBarSet(1,1).setFillColor(lightBlue);

        // group 2 - shades of gray

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

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

    }

   

    public static void main(String argv[]) {

        new SampleBarGroupStack().setVisible(true);

    }

}

 



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