1 39 40 package org.jfree.chart.demo; 41 42 import java.awt.Color ; 43 import java.awt.Font ; 44 import java.text.DecimalFormat ; 45 46 import org.jfree.chart.ChartPanel; 47 import org.jfree.chart.JFreeChart; 48 import org.jfree.chart.axis.CategoryAxis; 49 import org.jfree.chart.axis.NumberAxis; 50 import org.jfree.chart.axis.NumberTickUnit; 51 import org.jfree.chart.plot.CategoryPlot; 52 import org.jfree.chart.plot.DatasetRenderingOrder; 53 import org.jfree.chart.renderer.IntervalBarRenderer; 54 import org.jfree.chart.renderer.LineAndShapeRenderer; 55 import org.jfree.data.CategoryDataset; 56 import org.jfree.data.DatasetUtilities; 57 import org.jfree.data.DefaultIntervalCategoryDataset; 58 import org.jfree.ui.ApplicationFrame; 59 import org.jfree.ui.RefineryUtilities; 60 61 66 public class OverlaidCategoryChartDemo extends ApplicationFrame { 67 68 69 71 72 private static Color [] barColors = null; 73 74 75 private static Color [] dotColors = null; 76 77 78 private static Color [] lineColors = null; 79 80 81 private static Font labelFont = null; 82 83 84 86 87 private static Font titleFont = null; 88 89 90 private JFreeChart chart = null; 91 92 static { 93 barColors = new Color [1]; 94 barColors[0] = new Color (51, 102, 153); 95 dotColors = new Color [1]; 96 dotColors[0] = Color.white; 97 lineColors = new Color [4]; 98 lineColors[0] = Color.red; 99 lineColors[1] = Color.blue; 100 lineColors[2] = Color.yellow; 101 lineColors[3] = Color.magenta; 102 labelFont = new Font ("Helvetica", Font.PLAIN, 10); 103 titleFont = new Font ("Helvetica", Font.BOLD, 14); 105 } 106 107 112 public OverlaidCategoryChartDemo(String title) { 113 114 super(title); 115 DefaultIntervalCategoryDataset barData = null; 116 double[][] lows = {{-.0315, .0159, .0306, .0453, .0557}}; 117 double[][] highs = {{.1931, .1457, .1310, .1163, .1059}}; 118 barData = new DefaultIntervalCategoryDataset(lows, highs); 119 120 double[][] vals = {{0.0808, 0.0808, 0.0808, 0.0808, 0.0808}}; 121 CategoryDataset dotData = DatasetUtilities.createCategoryDataset( 122 "Series ", 123 "Category ", 124 vals 125 ); 126 127 double[][] lineVals = new double[4][5]; 128 for (int i = 0; i < 4; i++) { 129 for (int j = 0; j < 5; j++) { 130 lineVals[i][j] = (Math.random() * 0.56) - 0.18; 131 } 132 } 133 CategoryDataset lineData = DatasetUtilities.createCategoryDataset("Series ", "Category ", 134 lineVals); 135 136 String ctitle = "Strategie Sicherheit"; 137 String xTitle = "Zeitraum (in Jahren)"; 138 String yTitle = "Performance"; 139 CategoryAxis xAxis = new CategoryAxis(xTitle); 140 xAxis.setLabelFont(titleFont); 141 xAxis.setTickLabelFont(labelFont); 142 xAxis.setTickMarksVisible(false); 143 NumberAxis yAxis = new NumberAxis(yTitle); 144 yAxis.setLabelFont(titleFont); 145 yAxis.setTickLabelFont(labelFont); 146 yAxis.setRange(-0.2, 0.4); 147 DecimalFormat formatter = new DecimalFormat ("0.##%"); 148 yAxis.setTickUnit(new NumberTickUnit(0.05, formatter)); 149 150 IntervalBarRenderer barRenderer = new IntervalBarRenderer(); 151 barRenderer.setItemLabelsVisible(Boolean.TRUE); 152 153 CategoryPlot plot = new CategoryPlot(barData, xAxis, yAxis, barRenderer); 154 plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE); 155 156 plot.setBackgroundPaint(Color.lightGray); 157 plot.setOutlinePaint(Color.black); 158 159 LineAndShapeRenderer dotRenderer = new LineAndShapeRenderer(LineAndShapeRenderer.SHAPES); 160 dotRenderer.setItemLabelsVisible(Boolean.TRUE); 161 162 plot.setSecondaryDataset(0, dotData); 163 plot.setSecondaryRenderer(0, dotRenderer); 164 165 LineAndShapeRenderer lineRenderer 166 = new LineAndShapeRenderer(LineAndShapeRenderer.SHAPES_AND_LINES); 167 plot.setSecondaryDataset(1, lineData); 168 plot.setSecondaryRenderer(1, lineRenderer); 169 170 chart = new JFreeChart(ctitle, titleFont, plot, false); 171 chart.setBackgroundPaint(Color.white); 172 173 ChartPanel chartPanel = new ChartPanel(chart); 174 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 175 setContentPane(chartPanel); 176 177 } 178 179 190 195 public static void main(String [] args) { 196 197 OverlaidCategoryChartDemo demo 198 = new OverlaidCategoryChartDemo("Overlaid Category Chart Demo"); 199 demo.pack(); 200 RefineryUtilities.centerFrameOnScreen(demo); 201 demo.setVisible(true); 202 203 } 204 205 } 206 | Popular Tags |