Example 2: Treemap with unsorted data
A treemap is constructed from business analytics data. The area is proportional to the company's sales volume and the color scale maps to a performance indicator. The raw data are unsorted and must be sorted by the area values before creating the chart.
import com.imsl.chart.*;
public class TreemapEx2 extends javax.swing.JApplet {
public void init() {
Chart chart = new Chart(this);
JPanelChart panel = new JPanelChart(chart);
getContentPane().add(panel, java.awt.BorderLayout.CENTER);
setup(chart);
}
static private void setup(Chart chart) {
AxisXY axis = new AxisXY(chart);
double[] areas = {834, 11359, 1621, 12282, 14646, 8686, 12114, 61402,
582, 9448, 1678};
double[] perf = {-1.75, -.99, -1.4, 0.12, -0.28, -1.14, -0.06, 0.37,
-0.66, 0, 0.75};
String[] labels = {"Amer. It. Pasta", "Campbell Soup", "Dean Foods",
"General Mills", "Heinz", "Hershey Foods", "Kellogg", "Kraft Foods",
"Ralston Purina", "Sara Lee", "Suiza Foods"};
// sort data
double[] s_perf = new double[perf.length];
String[] s_lab = new String[perf.length];
int[] idx = new int[perf.length];
com.imsl.stat.Sort.descending(areas, idx);
for (int i = 0; i < perf.length; i++) {
s_perf[i] = perf[idx[i]];
s_lab[i] = labels[idx[i]];
}
Treemap treemap = new Treemap(axis, areas, s_perf,
Colormap.BLUE_GREEN_RED_YELLOW);
treemap.setLabels(s_lab);
treemap.setTextColor(java.awt.Color.gray);
treemap.getTreemapLegend().setPaint(true);
treemap.getTreemapLegend().setTitle("Scale");
treemap.getTreemapLegend().setTextFormat("0.0");
}
public static void main(String argv[]) throws Exception {
JFrameChart frame = new JFrameChart();
TreemapEx2.setup(frame.getChart());
frame.setVisible(true);
}
}
Output
Link to Java source.