1 40 41 package org.jfree.chart.demo; 42 43 import java.awt.Color ; 44 import java.awt.Font ; 45 46 import org.jfree.chart.ChartFactory; 47 import org.jfree.chart.ChartPanel; 48 import org.jfree.chart.JFreeChart; 49 import org.jfree.chart.Spacer; 50 import org.jfree.chart.StandardLegend; 51 import org.jfree.chart.TextTitle; 52 import org.jfree.chart.axis.CategoryAxis; 53 import org.jfree.chart.axis.CategoryLabelPosition; 54 import org.jfree.chart.axis.NumberAxis; 55 import org.jfree.chart.plot.CategoryPlot; 56 import org.jfree.chart.plot.PlotOrientation; 57 import org.jfree.data.CategoryDataset; 58 import org.jfree.data.DatasetUtilities; 59 import org.jfree.text.TextBlockAnchor; 60 import org.jfree.ui.ApplicationFrame; 61 import org.jfree.ui.RectangleAnchor; 62 import org.jfree.ui.RefineryUtilities; 63 import org.jfree.ui.TextAnchor; 64 65 71 public class AreaChartDemo extends ApplicationFrame { 72 73 78 public AreaChartDemo(String title) { 79 80 super(title); 81 82 double[][] data = new double[][] { 84 {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0}, 85 {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0}, 86 {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0} 87 }; 88 89 CategoryDataset dataset = DatasetUtilities.createCategoryDataset( 90 "Series ", "Type ", data 91 ); 92 93 JFreeChart chart = createChart(dataset); 95 96 ChartPanel chartPanel = new ChartPanel(chart); 98 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 99 chartPanel.setEnforceFileExtensions(false); 100 101 setContentPane(chartPanel); 102 103 } 104 105 116 123 private JFreeChart createChart(CategoryDataset dataset) { 124 125 JFreeChart chart = ChartFactory.createAreaChart( 126 "Area Chart", "Category", "Value", dataset, PlotOrientation.VERTICAL, 131 true, true, false ); 135 136 138 StandardLegend legend = (StandardLegend) chart.getLegend(); 140 legend.setAnchor(StandardLegend.SOUTH); 141 142 chart.setBackgroundPaint(Color.white); 143 TextTitle subtitle = new TextTitle("An area chart demonstration."); 144 subtitle.setFont(new Font ("SansSerif", Font.PLAIN, 12)); 145 chart.addSubtitle(subtitle); 146 147 CategoryPlot plot = chart.getCategoryPlot(); 148 plot.setForegroundAlpha(0.5f); 149 150 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 151 plot.setBackgroundPaint(Color.lightGray); 152 plot.setDomainGridlinesVisible(true); 153 plot.setDomainGridlinePaint(Color.white); 154 plot.setRangeGridlinesVisible(true); 155 plot.setRangeGridlinePaint(Color.white); 156 157 CategoryAxis domainAxis = plot.getDomainAxis(); 158 CategoryLabelPosition position = new CategoryLabelPosition( 159 RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 8.0 160 ); 161 domainAxis.setBottomCategoryLabelPosition(position); 162 domainAxis.setLowerMargin(0.0); 163 domainAxis.setUpperMargin(0.0); 164 165 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 166 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 167 rangeAxis.setLabelAngle(0 * Math.PI / 2.0); 168 170 return chart; 171 172 } 173 174 179 public static void main(String [] args) { 180 181 AreaChartDemo demo = new AreaChartDemo("Area Chart Demo"); 182 demo.pack(); 183 RefineryUtilities.centerFrameOnScreen(demo); 184 demo.setVisible(true); 185 186 } 187 188 } 189 | Popular Tags |