1 37 package org.jfree.chart.demo; 38 39 import java.awt.Color ; 40 import java.awt.Font ; 41 42 import org.jfree.chart.ChartFactory; 43 import org.jfree.chart.ChartPanel; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.TextTitle; 46 import org.jfree.chart.axis.ExtendedCategoryAxis; 47 import org.jfree.chart.axis.ValueAxis; 48 import org.jfree.chart.plot.CategoryPlot; 49 import org.jfree.chart.plot.PlotOrientation; 50 import org.jfree.chart.renderer.BarRenderer; 51 import org.jfree.chart.renderer.ItemLabelAnchor; 52 import org.jfree.chart.renderer.ItemLabelPosition; 53 import org.jfree.data.CategoryDataset; 54 import org.jfree.data.DefaultCategoryDataset; 55 import org.jfree.ui.ApplicationFrame; 56 import org.jfree.ui.RefineryUtilities; 57 import org.jfree.ui.TextAnchor; 58 59 64 public class SurveyResultsDemo2 extends ApplicationFrame { 65 66 71 public SurveyResultsDemo2(String title) { 72 73 super(title); 74 75 CategoryDataset dataset = createDataset(); 76 JFreeChart chart = createChart(dataset); 77 78 ChartPanel chartPanel = new ChartPanel(chart); 80 chartPanel.setPreferredSize(new java.awt.Dimension (300, 270)); 81 setContentPane(chartPanel); 82 83 } 84 85 90 private CategoryDataset createDataset() { 91 92 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 93 dataset.addValue(1.32, "Results", "Sm."); 94 dataset.addValue(0.40, "Results", "Med."); 95 dataset.addValue(2.62, "Results", "Lg."); 96 dataset.addValue(1.44, "Results", "All"); 97 return dataset; 98 99 } 100 101 108 private JFreeChart createChart(CategoryDataset dataset) { 109 110 JFreeChart chart = ChartFactory.createBarChart( 111 null, null, null, dataset, PlotOrientation.VERTICAL, 116 false, true, 118 false 119 ); 120 121 chart.setBackgroundPaint(Color.white); 122 chart.getPlot().setOutlinePaint(null); 123 TextTitle title = new TextTitle("Figure 8.5 - Case studies are available"); 124 title.setHorizontalAlignment(TextTitle.LEFT); 125 title.setBackgroundPaint(Color.red); 126 title.setPaint(Color.white); 127 128 chart.setTitle(title); 129 CategoryPlot plot = chart.getCategoryPlot(); 130 131 ValueAxis rangeAxis = plot.getRangeAxis(); 132 rangeAxis.setRange(0.0, 5.0); 133 rangeAxis.setVisible(false); 134 135 ExtendedCategoryAxis domainAxis = new ExtendedCategoryAxis(null); 136 domainAxis.setTickLabelFont(new Font ("SansSerif", Font.BOLD, 12)); 137 domainAxis.setCategoryMargin(0.30); 138 domainAxis.addSubLabel("Sm.", "(10)"); 139 domainAxis.addSubLabel("Med.", "(10)"); 140 domainAxis.addSubLabel("Lg.", "(10)"); 141 domainAxis.addSubLabel("All", "(10)"); 142 plot.setDomainAxis(domainAxis); 143 144 BarRenderer renderer = (BarRenderer) plot.getRenderer(); 145 renderer.setSeriesPaint(0, new Color (0x9C, 0xA4, 0x4A)); 146 renderer.setBaseOutlineStroke(null); 147 148 renderer.setItemLabelsVisible(true); 149 renderer.setItemLabelFont(new Font ("SansSerif", Font.PLAIN, 18)); 150 ItemLabelPosition position = new ItemLabelPosition( 151 ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER, TextAnchor.CENTER, 0.0 152 ); 153 renderer.setPositiveItemLabelPosition(position); 154 renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition()); 155 156 return chart; 157 158 } 159 160 171 176 public static void main(String [] args) { 177 178 SurveyResultsDemo2 demo = new SurveyResultsDemo2("Survey Results Demo 2"); 179 demo.pack(); 180 RefineryUtilities.centerFrameOnScreen(demo); 181 demo.setVisible(true); 182 183 } 184 185 } 186 | Popular Tags |