1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| package org.jfree.chart.demo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.DefaultKeyedValues2DDataset;
import org.jfree.data.KeyedValues2DDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A population pyramid demo.
*
* @author David Gilbert
*/
public class PopulationChartDemo extends ApplicationFrame {
/**
* Creates a new demo.
*
* @param title the frame title.
*/
public PopulationChartDemo(String title) {
super(title);
KeyedValues2DDataset dataset = createDataset();
// create the chart...
JFreeChart chart = ChartFactory.createStackedHorizontalBarChart(
"Population Chart Demo",
"Age Group", // domain axis label
"Population (millions)", // range axis label
dataset, // data
true, // include legend
true, // tooltips
false // urls
);
CategoryPlot plot = chart.getCategoryPlot();
// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
*
*/
private KeyedValues2DDataset createDataset() {
DefaultKeyedValues2DDataset data = new DefaultKeyedValues2DDataset();
data.addValue( -6.0, "Male", "70+");
data.addValue( -8.0, "Male", "60-69");
data.addValue(-11.0, "Male", "50-59");
data.addValue(-13.0, "Male", "40-49");
data.addValue(-14.0, "Male", "30-39");
data.addValue(-15.0, "Male", "20-29");
data.addValue(-19.0, "Male", "10-19");
data.addValue(-21.0, "Male", "0-9");
data.addValue(10.0, "Female", "70+");
data.addValue(12.0, "Female", "60-69");
data.addValue(13.0, "Female", "50-59");
data.addValue(14.0, "Female", "40-49");
data.addValue(15.0, "Female", "30-39");
data.addValue(17.0, "Female", "20-29");
data.addValue(19.0, "Female", "10-19");
data.addValue(20.0, "Female", "0-9");
return data;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
PopulationChartDemo demo = new PopulationChartDemo("Population Chart Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
} |
Partager