1 37 38 package org.jfree.chart.demo; 39 40 import org.jfree.chart.ChartFactory; 41 import org.jfree.chart.ChartPanel; 42 import org.jfree.chart.JFreeChart; 43 import org.jfree.chart.plot.PlotOrientation; 44 import org.jfree.data.CategoryDataset; 45 import org.jfree.data.DefaultKeyedValues2DDataset; 46 import org.jfree.data.KeyedValues2DDataset; 47 import org.jfree.ui.ApplicationFrame; 48 import org.jfree.ui.RefineryUtilities; 49 50 55 public class PopulationChartDemo extends ApplicationFrame { 56 57 62 public PopulationChartDemo(String title) { 63 64 super(title); 65 CategoryDataset dataset = createDataset(); 66 67 JFreeChart chart = ChartFactory.createStackedBarChart( 69 "Population Chart Demo", 70 "Age Group", "Population (millions)", dataset, PlotOrientation.HORIZONTAL, 74 true, true, false ); 78 79 ChartPanel chartPanel = new ChartPanel(chart); 81 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 82 setContentPane(chartPanel); 83 84 } 85 86 97 102 private KeyedValues2DDataset createDataset() { 103 104 DefaultKeyedValues2DDataset data = new DefaultKeyedValues2DDataset(); 105 data.addValue(-6.0, "Male", "70+"); 106 data.addValue(-8.0, "Male", "60-69"); 107 data.addValue(-11.0, "Male", "50-59"); 108 data.addValue(-13.0, "Male", "40-49"); 109 data.addValue(-14.0, "Male", "30-39"); 110 data.addValue(-15.0, "Male", "20-29"); 111 data.addValue(-19.0, "Male", "10-19"); 112 data.addValue(-21.0, "Male", "0-9"); 113 data.addValue(10.0, "Female", "70+"); 114 data.addValue(12.0, "Female", "60-69"); 115 data.addValue(13.0, "Female", "50-59"); 116 data.addValue(14.0, "Female", "40-49"); 117 data.addValue(15.0, "Female", "30-39"); 118 data.addValue(17.0, "Female", "20-29"); 119 data.addValue(19.0, "Female", "10-19"); 120 data.addValue(20.0, "Female", "0-9"); 121 return data; 122 123 } 124 125 130 public static void main(String [] args) { 131 132 PopulationChartDemo demo = new PopulationChartDemo("Population Chart Demo"); 133 demo.pack(); 134 RefineryUtilities.centerFrameOnScreen(demo); 135 demo.setVisible(true); 136 137 } 138 139 } 140 | Popular Tags |