1 38 39 package org.jfree.chart.demo; 40 41 import java.awt.Color ; 42 43 import org.jfree.chart.ChartPanel; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.axis.CategoryAxis; 46 import org.jfree.chart.axis.NumberAxis; 47 import org.jfree.chart.axis.ValueAxis; 48 import org.jfree.chart.plot.CategoryPlot; 49 import org.jfree.chart.renderer.LayeredBarRenderer; 50 import org.jfree.data.CategoryDataset; 51 import org.jfree.data.DatasetUtilities; 52 import org.jfree.ui.ApplicationFrame; 53 import org.jfree.ui.RefineryUtilities; 54 55 60 public class LayeredBarChartDemo1 extends ApplicationFrame { 61 62 67 public LayeredBarChartDemo1(String title) { 68 69 super(title); 70 71 CategoryDataset dataset = createDataset(); 72 JFreeChart chart = createChart(dataset); 73 ChartPanel chartPanel = new ChartPanel(chart); 74 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 75 setContentPane(chartPanel); 76 77 } 78 79 90 95 private CategoryDataset createDataset() { 96 97 double[][] data = new double[][] { 99 {41.0, 33.0, 22.0, 64.0, 42.0, 62.0, 22.0, 14.0}, 100 {55.0, 63.0, 55.0, 48.0, 54.0, 37.0, 41.0, 39.0}, 101 {57.0, 75.0, 43.0, 33.0, 63.0, 46.0, 57.0, 33.0} 102 }; 103 104 return DatasetUtilities.createCategoryDataset("Series ", "Factor ", data); 105 106 } 107 108 115 private JFreeChart createChart(CategoryDataset dataset) { 116 117 CategoryAxis categoryAxis = new CategoryAxis("Category"); 118 ValueAxis valueAxis = new NumberAxis("Score (%)"); 119 120 121 CategoryPlot plot = new CategoryPlot(dataset, 122 categoryAxis, 123 valueAxis, 124 new LayeredBarRenderer()); 125 126 plot.setOrientation(org.jfree.chart.plot.PlotOrientation.HORIZONTAL); 127 JFreeChart chart = new JFreeChart("Layered Bar Chart", 128 JFreeChart.DEFAULT_TITLE_FONT, plot, true); 129 130 chart.setBackgroundPaint(Color.lightGray); 132 133 LayeredBarRenderer renderer = (LayeredBarRenderer) plot.getRenderer(); 134 135 renderer.setSeriesBarWidth(0, 1.0); 138 renderer.setSeriesBarWidth(1, 0.7); 139 renderer.setSeriesBarWidth(2, 0.4); 140 141 renderer.setItemMargin(0.01); 142 CategoryAxis domainAxis = plot.getDomainAxis(); 143 domainAxis.setCategoryMargin(0.25); 144 domainAxis.setUpperMargin(0.05); 145 domainAxis.setLowerMargin(0.05); 146 147 return chart; 148 149 } 150 151 156 public static void main(String [] args) { 157 158 LayeredBarChartDemo1 demo = new LayeredBarChartDemo1("Layered Bar Chart Demo 1"); 159 demo.pack(); 160 RefineryUtilities.centerFrameOnScreen(demo); 161 demo.setVisible(true); 162 163 } 164 165 } 166 | Popular Tags |