1 37 package org.jfree.chart.demo; 38 39 import java.awt.Color ; 40 import java.awt.Font ; 41 import java.text.DecimalFormat ; 42 43 import org.jfree.chart.ChartFactory; 44 import org.jfree.chart.ChartPanel; 45 import org.jfree.chart.JFreeChart; 46 import org.jfree.chart.TextTitle; 47 import org.jfree.chart.axis.CategoryLabelPosition; 48 import org.jfree.chart.axis.ExtendedCategoryAxis; 49 import org.jfree.chart.axis.ValueAxis; 50 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 51 import org.jfree.chart.plot.CategoryPlot; 52 import org.jfree.chart.plot.PlotOrientation; 53 import org.jfree.chart.renderer.BarRenderer; 54 import org.jfree.chart.renderer.ItemLabelAnchor; 55 import org.jfree.chart.renderer.ItemLabelPosition; 56 import org.jfree.data.CategoryDataset; 57 import org.jfree.data.DefaultCategoryDataset; 58 import org.jfree.text.TextBlockAnchor; 59 import org.jfree.ui.ApplicationFrame; 60 import org.jfree.ui.RectangleAnchor; 61 import org.jfree.ui.RefineryUtilities; 62 import org.jfree.ui.TextAnchor; 63 64 69 public class SurveyResultsDemo3 extends ApplicationFrame { 70 71 76 public SurveyResultsDemo3(String title) { 77 78 super(title); 79 80 CategoryDataset dataset = createDataset(); 81 JFreeChart chart = createChart(dataset); 82 83 ChartPanel chartPanel = new ChartPanel(chart); 85 chartPanel.setPreferredSize(new java.awt.Dimension (300, 270)); 86 setContentPane(chartPanel); 87 88 } 89 90 95 private CategoryDataset createDataset() { 96 97 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 98 dataset.addValue(2.61, "Results", "Sm."); 99 dataset.addValue(2.70, "Results", "Med."); 100 dataset.addValue(2.90, "Results", "Lg."); 101 dataset.addValue(2.74, "Results", "All"); 102 return dataset; 103 104 } 105 106 113 private JFreeChart createChart(CategoryDataset dataset) { 114 115 JFreeChart chart = ChartFactory.createBarChart( 116 null, null, null, dataset, PlotOrientation.HORIZONTAL, 121 false, true, 123 false 124 ); 125 126 chart.setBackgroundPaint(Color.white); 127 chart.getPlot().setOutlinePaint(null); 128 TextTitle title = new TextTitle("Figure 6 | Overall SEO Rating"); 129 title.setHorizontalAlignment(TextTitle.LEFT); 130 title.setBackgroundPaint(Color.red); 131 title.setPaint(Color.white); 132 133 chart.setTitle(title); 134 CategoryPlot plot = chart.getCategoryPlot(); 135 136 ValueAxis rangeAxis = plot.getRangeAxis(); 137 rangeAxis.setRange(0.0, 4.0); 138 rangeAxis.setVisible(false); 139 140 ExtendedCategoryAxis domainAxis = new ExtendedCategoryAxis(null); 141 domainAxis.setTickLabelFont(new Font ("SansSerif", Font.BOLD, 12)); 142 domainAxis.setCategoryMargin(0.30); 143 domainAxis.addSubLabel("Sm.", "(10)"); 144 domainAxis.addSubLabel("Med.", "(10)"); 145 domainAxis.addSubLabel("Lg.", "(10)"); 146 domainAxis.addSubLabel("All", "(10)"); 147 CategoryLabelPosition p = new CategoryLabelPosition( 148 RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, 0.0 149 ); 150 domainAxis.setLeftCategoryLabelPosition(p); 151 plot.setDomainAxis(domainAxis); 152 153 BarRenderer renderer = (BarRenderer) plot.getRenderer(); 154 renderer.setSeriesPaint(0, new Color (0x9C, 0xA4, 0x4A)); 155 renderer.setBaseOutlineStroke(null); 156 157 StandardCategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator( 158 new DecimalFormat ("0.00") 159 ); 160 renderer.setItemLabelGenerator(generator); 161 renderer.setItemLabelsVisible(true); 162 renderer.setItemLabelFont(new Font ("SansSerif", Font.PLAIN, 18)); 163 ItemLabelPosition position = new ItemLabelPosition( 164 ItemLabelAnchor.INSIDE3, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT, 0.0 165 ); 166 renderer.setPositiveItemLabelPosition(position); 167 renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition()); 168 169 return chart; 170 171 } 172 173 184 189 public static void main(String [] args) { 190 191 SurveyResultsDemo3 demo = new SurveyResultsDemo3("Survey Results Demo 3"); 192 demo.pack(); 193 RefineryUtilities.centerFrameOnScreen(demo); 194 demo.setVisible(true); 195 196 } 197 198 } 199 | Popular Tags |