1 4 package com.tc.admin.common; 5 6 import org.jfree.chart.ChartFactory; 7 import org.jfree.chart.JFreeChart; 8 import org.jfree.chart.axis.DateAxis; 9 import org.jfree.chart.axis.NumberAxis; 10 import org.jfree.chart.axis.ValueAxis; 11 import org.jfree.chart.plot.PlotOrientation; 12 import org.jfree.chart.plot.XYPlot; 13 import org.jfree.data.RangeType; 14 import org.jfree.data.time.TimeSeries; 15 import org.jfree.data.time.TimeSeriesCollection; 16 import org.jfree.data.xy.IntervalXYDataset; 17 import org.jfree.data.xy.XYDataset; 18 19 import java.text.SimpleDateFormat ; 20 21 public class DemoChartFactory { 22 public static JFreeChart getXYBarChart( 23 String header, 24 String xLabel, 25 String yLabel, 26 TimeSeries ts) 27 { 28 IntervalXYDataset dataset = createTimeSeriesDataset(ts); 29 return createXYBarChart(header, xLabel, yLabel, dataset, false); 30 } 31 32 public static JFreeChart getXYBarChart( 33 String header, 34 String xLabel, 35 String yLabel, 36 TimeSeries[] timeseries) 37 { 38 IntervalXYDataset dataset = createTimeSeriesDataset(timeseries); 39 return createXYBarChart(header, xLabel, yLabel, dataset, true); 40 } 41 42 private static JFreeChart createXYBarChart( 43 String header, 44 String xLabel, 45 String yLabel, 46 IntervalXYDataset dataset, 47 boolean legend) 48 { 49 JFreeChart chart = 50 ChartFactory.createXYBarChart( 51 header, 52 xLabel, 53 true, 54 yLabel, 55 dataset, 56 PlotOrientation.VERTICAL, 57 legend, 58 true, 59 false); 60 61 XYPlot plot = (XYPlot)chart.getPlot(); 62 63 DateAxis axis = (DateAxis)plot.getDomainAxis(); 64 axis.setFixedAutoRange(30000.0); 65 axis.setDateFormatOverride(new SimpleDateFormat ("kk:mm:ss")); 66 67 NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis(); 68 numberAxis.setRangeType(RangeType.POSITIVE); 69 numberAxis.setAutoRangeMinimumSize(50.0); 70 71 return chart; 72 } 73 74 public static JFreeChart getXYLineChart( 75 String header, 76 String xLabel, 77 String yLabel, 78 TimeSeries ts) 79 { 80 XYDataset dataset = createTimeSeriesDataset(ts); 81 return createXYLineChart(header, xLabel, yLabel, dataset, false); 82 } 83 84 public static JFreeChart getXYLineChart( 85 String header, 86 String xLabel, 87 String yLabel, 88 TimeSeries[] timeseries) 89 { 90 XYDataset dataset = createTimeSeriesDataset(timeseries); 91 return createXYLineChart(header, xLabel, yLabel, dataset, true); 92 } 93 94 private static JFreeChart createXYLineChart( 95 String header, 96 String xLabel, 97 String yLabel, 98 XYDataset dataset, 99 boolean legend) 100 { 101 JFreeChart chart = 102 ChartFactory.createTimeSeriesChart(header, 103 xLabel, 104 yLabel, 105 dataset, 106 legend, 107 true, 108 false); 109 110 XYPlot plot = (XYPlot)chart.getPlot(); 111 112 ValueAxis axis = plot.getDomainAxis(); 113 axis.setFixedAutoRange(30000.0); 114 if(axis instanceof DateAxis) { 115 ((DateAxis)axis).setDateFormatOverride(new SimpleDateFormat ("kk:mm:ss")); 116 } 117 118 NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis(); 119 numberAxis.setRangeType(RangeType.POSITIVE); 120 numberAxis.setAutoRangeMinimumSize(50.0); 121 122 return chart; 123 } 124 125 public static JFreeChart getXYStepChart( 126 String header, 127 String xLabel, 128 String yLabel, 129 TimeSeries ts) 130 { 131 XYDataset dataset = createTimeSeriesDataset(ts); 132 return createXYStepChart(header, xLabel, yLabel, dataset, false); 133 } 134 135 public static JFreeChart getXYStepChart( 136 String header, 137 String xLabel, 138 String yLabel, 139 TimeSeries[] timeseries) 140 { 141 XYDataset dataset = createTimeSeriesDataset(timeseries); 142 return createXYStepChart(header, xLabel, yLabel, dataset, true); 143 } 144 145 private static JFreeChart createXYStepChart( 146 String header, 147 String xLabel, 148 String yLabel, 149 XYDataset dataset, 150 boolean legend) 151 { 152 JFreeChart chart = 153 ChartFactory.createXYStepChart(header, 154 xLabel, 155 yLabel, 156 dataset, 157 PlotOrientation.VERTICAL, 158 legend, 159 true, 160 false); 161 162 XYPlot plot = (XYPlot)chart.getPlot(); 163 164 ValueAxis axis = plot.getDomainAxis(); 165 axis.setFixedAutoRange(30000.0); 166 if(axis instanceof DateAxis) { 167 ((DateAxis)axis).setDateFormatOverride(new SimpleDateFormat ("kk:mm:ss")); 168 } 169 170 NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis(); 171 numberAxis.setRangeType(RangeType.POSITIVE); 172 numberAxis.setAutoRangeMinimumSize(50.0); 173 174 return chart; 175 } 176 177 private static TimeSeriesCollection createTimeSeriesDataset(TimeSeries s1) { 178 return createTimeSeriesDataset(new TimeSeries[] {s1}); 179 } 180 181 private static TimeSeriesCollection createTimeSeriesDataset(TimeSeries[] series) { 182 TimeSeriesCollection dataset = new TimeSeriesCollection(); 183 184 for(int i = 0; i < series.length; i++) { 185 dataset.addSeries(series[i]); 186 } 187 188 dataset.setDomainIsPointsInTime(true); 189 190 return dataset; 191 } 192 } 193 | Popular Tags |